How do I properly fire ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS intent?

30,087

Solution 1

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.

Solution 2

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

Share:
30,087
A.Sanchez.SD
Author by

A.Sanchez.SD

Updated on July 11, 2022

Comments

  • A.Sanchez.SD
    A.Sanchez.SD almost 2 years

    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?

  • Elad Nava
    Elad Nava over 8 years
    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.
  • milosmns
    milosmns over 8 years
    @EladNava Did you write to Google about the issue?
  • Elad Nava
    Elad Nava over 8 years
    @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 for requesting the permission. Will update when they reach their decision.
  • milosmns
    milosmns over 8 years
    @EladNava Yeah, bummer. Hopefully people will see these comments before using the same approach.
  • Elad Nava
    Elad Nava over 8 years
    @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.
  • Opiatefuchs
    Opiatefuchs about 8 years
    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? :(
  • Elad Nava
    Elad Nava about 8 years
    @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.
  • Opiatefuchs
    Opiatefuchs about 8 years
    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
    Opiatefuchs about 8 years
    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.
  • behelit
    behelit about 8 years
    @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
  • Elad Nava
    Elad Nava about 8 years
    @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.
  • chksr
    chksr over 7 years
    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.
  • RaRa
    RaRa over 7 years
    @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.
  • powder366
    powder366 over 7 years
    The Intent is not fired for me?
  • Andrew
    Andrew over 7 years
    This will only trigger if you have <uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTI‌​MIZATIONS"/> in your AndroidManifest.xml
  • powder366
    powder366 over 7 years
    @Andrew Thanks it helped.
  • Amir Uval
    Amir Uval over 7 years
    @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)
  • powder366
    powder366 over 7 years
    No I didnt. I guided the user to settings to let them change on their own.
  • Alin
    Alin over 7 years
    @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?
  • Elad Nava
    Elad Nava over 7 years
    @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.
  • Bright Lee
    Bright Lee about 7 years
    If already app is ignoringBatteryOptimization, what "intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATIO‌​N_SETTINGS);" is for? Why you need to open setting page?
  • Amir Uval
    Amir Uval about 7 years
    @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
    Amir Uval about 7 years
    @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.
  • Elad Nava
    Elad Nava about 7 years
    @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.
  • forresthopkinsa
    forresthopkinsa about 7 years
    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?
  • KgaboL
    KgaboL over 6 years
    Without the permission startActivity() doesnt do anything
  • Jesse
    Jesse about 6 years
    Code-only answers are not sufficient, please provide a little explanation on your code
  • Mygod
    Mygod almost 6 years
    Android Studio now will tell you that there are acceptable cases where you can use this permission: developer.android.com/training/monitoring-device-state/…
  • cambunctious
    cambunctious about 5 years
    This makes no sense. You call startActivity with an empty intent if SDK < M.
  • RobbiewOnline
    RobbiewOnline over 4 years
    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
    RobbiewOnline over 4 years
    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
    RobbiewOnline over 4 years
    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.
  • matdev
    matdev over 4 years
    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 ?
  • Keselme
    Keselme about 4 years
    Is it possible to have any callback for the user selection?
  • Siddarth G
    Siddarth G almost 4 years
    @AmirUval do you have any code sample where you have guided the user to disable it ?
  • Amir Uval
    Amir Uval almost 4 years
    @SiddarthG three years ago I had ;-)
  • DespeiL
    DespeiL almost 4 years
    isIgnoringBatteryOptimizations is allways false, any Idea why ?
  • Moustafa EL-Saghier
    Moustafa EL-Saghier almost 3 years
    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?