Switch to polling if battery optimizations enabled on Android 12+

This commit is contained in:
M66B
2021-09-23 09:27:15 +02:00
parent 957301bf43
commit 54daa402e4
4 changed files with 39 additions and 12 deletions

View File

@@ -34,6 +34,7 @@ import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
@@ -432,6 +433,22 @@ public class Helper {
return pm.isIgnoringBatteryOptimizations(BuildConfig.APPLICATION_ID);
}
private static Integer targetSdk = null;
static boolean isTarget(Context context, int sdk) {
if (targetSdk == null)
try {
PackageManager pm = context.getPackageManager();
ApplicationInfo ai = pm.getApplicationInfo(BuildConfig.APPLICATION_ID, 0);
targetSdk = ai.targetSdkVersion;
} catch (Throwable ex) {
Log.e(ex);
targetSdk = Build.VERSION.SDK_INT;
}
return (targetSdk >= sdk);
}
static Integer getBatteryLevel(Context context) {
try {
BatteryManager bm = (BatteryManager) context.getSystemService(Context.BATTERY_SERVICE);