48

As stated in the documentation:

"An app holding the REQUEST_IGNORE_BATTERY_OPTIMIZATIONS permission can trigger a system dialog to let the user add the app to the whitelist directly, without going to settings. The app fires a ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS Intent to trigger the dialog."

Can someone tell me the proper way to fire this intent?

Floern
  • 33,559
  • 24
  • 104
  • 119
A.Sanchez.SD
  • 1,950
  • 2
  • 18
  • 23
  • 1
    One alternative is doit by adb or make your command line tool inside your app to call something like this [answer](https://stackoverflow.com/a/47685874/3174791) , that work perfectly to me – Edgar May 17 '19 at 16:45

3 Answers3

70
Intent intent = new Intent();
String packageName = context.getPackageName();
PowerManager pm = (PowerManager) context.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));
}
context.startActivity(intent);

Kotlin

val intent = Intent()
val pm : PowerManager = getSystemService(Context.POWER_SERVICE) as PowerManager
if (pm.isIgnoringBatteryOptimizations(context.packageName)) {
    intent.action = Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS
} else {
    intent.action = Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
    intent.data = Uri.parse("package:${context.packageName}")
}
context.startActivity(intent)

See this answer for more information.

murgupluoglu
  • 6,524
  • 4
  • 33
  • 43
Buddy
  • 10,874
  • 5
  • 41
  • 58
  • 32
    Be careful with this - my app just got suspended from Google Play without prior notice for requesting `REQUEST_IGNORE_BATTERY_OPTIMIZATIONS`, even though exempting the app from battery optimizations is crucial to my app's function, and I can't use GCM for technical reasons. – Elad Nava Oct 27 '15 at 15:19
  • @EladNava Did you write to Google about the issue? – milosmns Oct 27 '15 at 16:46
  • 1
    @milosmns I did, and waiting for their reply. My app is currently suspended and inaccessible as I wait for their verdict. I seem to be the first person to have posted about getting suspended for requesting `REQUEST_IGNORE_BATTERY_OPTIMIZATIONS`, for **perfectly legitimate** reasons (crucial to app + GCM technical difficulties). These are the 2 conditions listed in their [docs](https://developer.android.com/training/monitoring-device-state/doze-standby.html#support_for_other_use_cases) for requesting the permission. Will update when they reach their decision. – Elad Nava Oct 27 '15 at 21:45
  • @EladNava Yeah, bummer. Hopefully people will see these comments before using the same approach. – milosmns Oct 28 '15 at 13:00
  • 14
    @milosmns They decided that the only way my app will be reinstated is if I remove the `REQUEST_IGNORE_BATTERY_OPTIMIZATIONS` permission, even though I explained that according to the docs I had every right to request it. I decided to remove it so that I won't risk permanent deletion of the app. It seems that Google is extremely trigger-happy and emotionless about suspending and permanently banning apps on Google Play. – Elad Nava Oct 28 '15 at 20:31
  • late reply, but @Elad: Is your app still removed? What did Google give as justification for suspending your app? I can´t understand Googles behaviour, if an app really needs this, why they nevertheless suspend the app? :( – Opiatefuchs May 20 '16 at 11:01
  • @Opiatefuchs It has been reinstated, after I had sent Google Play Developer Support several e-mails and attempted to appeal the suspension. I also know someone who works at Google whom I asked to help me with this. – Elad Nava May 20 '16 at 11:09
  • 2
    Thanks for reply.....I really don´t get it....I don´t know why Google suddenly acts apple-like :( . If they clear state out which apps are acceptable for beeing on the whitelist and which not, where is the problem if it matches the case? – Opiatefuchs May 20 '16 at 11:34
  • And really, the new Doze mode and App standby is not as user friendly as they think. I read about many complaints from users. A simple thing would be, to give an option in the device settings for the users to enable/disable doze mode and app standby. If they would do it, everybody will be happy. – Opiatefuchs May 20 '16 at 11:36
  • @EladNava so how do we avoid suspension? My app doesn't have internet access but is similar to skype (using external hardware) i.e. has to display incoming calls and is time critical – behelit Jun 22 '16 at 06:07
  • 2
    @behelit I'd attempt to get Google's written approval of your use-case before asking for this permission in your app. Not sure if they have such a process but try to get in touch with them anyway to avoid unexpected suspension. – Elad Nava Jun 22 '16 at 09:15
  • Elad, could you please tell me how to contact them as directly as possible for such a topic? My alarm clock needs two alarms which might be as near as 10-15 minutes apart, and the second one is not fired then. So it seems I need approval on their part. – chksr Oct 06 '16 at 06:45
  • @Buddy i am not able to open any popup or screen using above code.i paste code in onStart, onResume and finally in onCreate but i cant get any things. i just show blink like something open and close same time. i cant understand the situation my target api is 23. – RaRa Oct 24 '16 at 13:18
  • The Intent is not fired for me? – powder366 Nov 19 '16 at 17:50
  • 20
    This will only trigger if you have in your AndroidManifest.xml – Andrew Nov 20 '16 at 23:16
  • @Andrew Thanks it helped. – powder366 Nov 21 '16 at 21:37
  • @powder366 have you published your app with this permission? Did you read the warning in the above comments? (you might get kicked out of Google Play) – Amir Uval Dec 06 '16 at 00:21
  • No I didnt. I guided the user to settings to let them change on their own. – powder366 Dec 06 '16 at 07:41
  • 1
    @EladNava I know that this is an old thread, but in the end how did you do? Removed the permission? Basically not having the permission but letting the user know how to do the Whitelisting manually is an ok case for Google? – Alin Feb 17 '17 at 12:32
  • 4
    @Alin I ended up removing the permission. I manually guide users that complain about the app not working to whitelist the app from battery optimizations. – Elad Nava Feb 17 '17 at 22:15
  • 2
    If already app is ignoringBatteryOptimization, what "intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);" is for? Why you need to open setting page? – Bright Lee May 18 '17 at 20:33
  • @EladNava Thanks for raising this. When you app got banned, did you have the permission in the manifest or only in the code? – Amir Uval Jun 01 '17 at 21:32
  • 4
    @EladNava thanks! As I understand, if *not* declaring this in the manifest, directing users directly to the opt out page (by calling startActivity() with this action and app id as data) is safe. – Amir Uval Jun 02 '17 at 06:13
  • 2
    @auval Yes, that is correct. Google is only against apps allowing users to whitelist battery optimizations from *within the app* via the in-app dialog. Taking the user to the battery optimizations page is fine. – Elad Nava Jun 02 '17 at 10:04
  • The `if` statement in this code seems unnecessary and even counterproductive... I mean, this answer isn't about opening the battery optimization settings, it's about disabling Doze mode. So why does the `if` default to that? – forresthopkinsa Jun 08 '17 at 23:07
  • 1
    Android Studio now will tell you that there are acceptable cases where you can use this permission: https://developer.android.com/training/monitoring-device-state/doze-standby#whitelisting-cases – Mygod Aug 22 '18 at 02:21
  • I noticed that famous app such as Deezer or Spotify are never killed when playing music in background and they don't request ignoring battery optimization. Does anyone know how they achieve that ? – matdev Nov 13 '19 at 15:10
  • Is it possible to have any callback for the user selection? – Keselme Jun 25 '20 at 09:50
  • @AmirUval do you have any code sample where you have guided the user to disable it ? – Siddarth G Jul 22 '20 at 14:59
  • @SiddarthG three years ago I had ;-) – Amir Uval Aug 05 '20 at 07:14
  • isIgnoringBatteryOptimizations is allways false, any Idea why ? – DespeiL Sep 28 '20 at 16:06
  • i can redirect user to setting screen, guide him to select no restrictions from battery saver, is it the same as redirecting user to battery optimization screen? – Moustafa EL-Saghier Sep 29 '21 at 09:13
  • This code makes no sense. It shows two alternative ways of whitelisting the app: directly using a system dialog (requires permission and will probably get the app banned in Google Play) and opening the Settings UI to show the list of all whitelisted/optimized apps. Why would you do one or the other depending on the result of `pm.isIgnoringBatteryOptimizations`? – Mister Smith Aug 12 '22 at 19:54
19

To avoid suspension from Google Play Store, Its Better to Take User to Battery Optimization Settings, for Manually Adding Application to White-list

Intent myIntent = new Intent();
myIntent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
startActivity(myIntent);

Also It doesn't need to have

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

permission in the manifest file

Jamil
  • 5,457
  • 4
  • 26
  • 29
  • 10
    Without the permission startActivity() doesnt do anything – KgaboL Nov 18 '17 at 16:43
  • I agree with @KgaboL that if the android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS is missing from the manifest then starting the activity with the action Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS doesn't do anything. I however do want to take the user to the page for *them* to manually turn it on. – RobbiewOnline Oct 22 '19 at 20:56
  • 2
    Meanwhile it seems funny you can take the user to a page via ActionIgnoreBatteryOptimizationSettings to see you are not optimised. I just want to be taken to the page where I'm optimised so I can disable it – RobbiewOnline Oct 22 '19 at 20:57
  • 1
    It works perfectly without permissions. The permission is only needed for `ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS`. If it doesn't work check that you are not adding the `setData` line to the intent. The intent should only have an action, as shown in this answer. – Mister Smith Aug 12 '22 at 19:59
-4
    Intent myIntent = new Intent();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        myIntent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
        myIntent.setData(Uri.parse("package:" + getActivity().getPackageName()));
    }
    startActivity(myIntent);
Arunmani
  • 103
  • 1
  • 4
  • 2
    Code-only answers are not sufficient, please provide a little explanation on your code – Jesse Apr 04 '18 at 13:07
  • 11
    This makes no sense. You call startActivity with an empty intent if SDK < M. – cambunctious May 02 '19 at 00:30
  • Also as @Jamile mentions above, you need the permission in the manifest for this to work, which I'm seeing a lot of warnings saying your app can be yanked from Google Play... Seems harsh if it results in a dialog being displayed letter the user make a choice. – RobbiewOnline Oct 22 '19 at 20:59