I have a mileage logbook app which does GPS tracking and is able to establish a OBDII connection to a car in background.
Now I want to show a Popup which informs the users if my app is not whitelisted in doze since this may stop my background (actually foreground) services...
I do:
String PACKAGE_NAME = getApplicationContext().getPackageName();
PowerManager pm = (PowerManager) this.getSystemService(Context.POWER_SERVICE);
boolean status = false;
status = pm.isIgnoringBatteryOptimizations(PACKAGE_NAME);
if (!status) {
// show popup
}
but PowerManager.isIgnoringBatteryOptimizations always returns 'true' even if it is removed from 'Not optimized apps' again. Only if I uninstall the app 'false' is returned again... Tested on Galaxy Note 8 (Android 8.0) and Emulator 8.1
Question is simple: Is this a bug? Or how to remove the app from whitelist so that PowerManager.isIgnoringBatteryOptimizations is returnung 'false' again?
I have made simple one activity app that requests
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
and put this code into onCreate
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent();
String packageName = this.getPackageName();
PowerManager pm = (PowerManager) this.getSystemService(Context.POWER_SERVICE);
if (pm.isIgnoringBatteryOptimizations(packageName))
intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
else {
intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + packageName));
}
this.startActivity(intent);
}
I have tested it on 8.1 emulator and I got the same behavior. You can possibly unisntall app or switch its state from optimized to not optimized in all app list in settings. I guess Bob Snyder showed right issue in google tracker.
Same here. Android 9 emulator. Uninstalling the app and installing it again makes the method to work again.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With