Mister Smith
Mister Smith

Reputation: 28168

Battery optimization dialog not working in Android Oreo

I'm using the following method to request whitelisting for my app:

    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    String packageName = getApplicationContext().getPackageName();


    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (!pm.isIgnoringBatteryOptimizations(packageName)) {

            Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
            intent.setData(Uri.parse("package:" + packageName));
            startActivity(intent);
        }
    }

I've also added the permission in the manifest:

<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>

(Yes, I know this permission will get you banned in Google Play. This code is just for testing purposes).

In Android Oreo, the dialog is shown, and after the user accepts it nothing happens. the app is not added to the whitelist. However calling PackageManager.isIgnoringBatteryOptimizations from there on always returns true as expected.

Is this a bug or have they removed this feature in Oreo?

Upvotes: 3

Views: 949

Answers (1)

Mister Smith
Mister Smith

Reputation: 28168

Well apparently this is an issue in some early ROMs and the new Google Play-enabled emulator. I tested this in the regular Oreo emulator and it works as expected.

Upvotes: 1

Related Questions