Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerManager.isIgnoringBatteryOptimizations always returns true even if is removed from 'Not optimized apps'

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?

like image 753
stefan Avatar asked May 08 '18 10:05

stefan


2 Answers

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.

like image 83
thekekc Avatar answered Nov 12 '22 09:11

thekekc


Same here. Android 9 emulator. Uninstalling the app and installing it again makes the method to work again.

like image 1
Mister Smith Avatar answered Nov 12 '22 09:11

Mister Smith