4

I am using Android 6 with Cyanogenmod and when I am starting the following intent, then nothing happens!

if(Build.VERSION.SDK_INT >= Constants.IGNORE_BATTERY_OPTIMIZATIONS_MIN_BUILD) {
                    String packageName = getPackageName();
                    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
                    if (!pm.isIgnoringBatteryOptimizations(packageName)) {
                        Intent intent = new Intent(android.provider.Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS).setData(Uri.parse("package:" + packageName));
                        try {
                            startActivity(intent);
    ...

Should the dialog for the Battery optimisation come up? If so, what am I doing wrong?

egmontr
  • 249
  • 3
  • 15
  • Note that it is likely that your app will get removed from the store if you use this functionality – Tim Dec 14 '16 at 12:29
  • 1
    No, they won't, because I only want to show up the dialog and the user does it himself. – egmontr Dec 14 '16 at 16:48
  • Possible duplicate of [ACTION\_REQUEST\_IGNORE\_BATTERY\_OPTIMIZATIONS in not firing](https://stackoverflow.com/questions/40434858/action-request-ignore-battery-optimizations-in-not-firing) – mbo Aug 04 '17 at 07:43

1 Answers1

7

You need to have this line in your manifest:

    <uses-permission-sdk-23 android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
Janne Oksanen
  • 1,524
  • 1
  • 13
  • 14
  • ......This is awesome solution to get alert for battery optimization settings......But I had read some where that if writing the above permission in the manifest file google play store will reject the app from the play store....it is true ? – Himanshu Jan 04 '17 at 07:45
  • I read the same thing and we debated this within out development team before adding this. It is in our Play Store alpha channel and there's been no sign of Google pulling the app so far. I will definitely report back if our app gets pulled out of the store. – Janne Oksanen Jan 04 '17 at 16:15
  • As a followup I'm happy to report that after 8 months in production there are no signs of Google Play objecting to the use of this permission. – Janne Oksanen Sep 11 '17 at 07:56