14

In the new Android M build, the Battery Optimization feature will stop the app on certain condition to save battery, but for the VOIPapps that need to stay alive all the time, this feature cause troubles for the developer.

Currently there is one way to bypass the optimization, which is set the app to ignore optimizations,but too many steps need to be done.

My question is, is there a way to jump to ignore optimization page of specific App, and how can I know whether my app is excluded from optimization?

TryinHard
  • 4,078
  • 3
  • 28
  • 54
Zheng Wei
  • 141
  • 1
  • 1
  • 3
  • Same for my App.. Any update on this ? – Rupali Jul 20 '15 at 05:25
  • you don't need to stay alive all the time, you can simply send a high priority GCM when you need to do stuff http://stackoverflow.com/questions/31315057/android-m-how-to-send-a-high-priority-gcm – Budius Aug 31 '15 at 09:23

3 Answers3

8

WARNING: It seems Google doesn't allow apps on the Play Store to do this. See the comments below.

Based on the AndroidManifest.xml extracted from Settings.apk in the MPA44I build, it would seem you might be able to do what you want by something like the intent below.

From XML:

<intent
    android:action="android.settings.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"
    android:targetPackage="com.android.settings"
    android:data="package:PUT_YOUR_PACKAGE_NAME_HERE" />

From code:

try {
    Intent intent = new Intent(android.provider.Settings.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
    intent.setData(Uri.parse("package:PUT_YOUR_PACKAGE_NAME_HERE"));
    startActivity(intent);

} catch (ActivityNotFoundException e) {
    e.printStackTrace();
}

Note that I have not had time to verify this in practice myself but the approach is the same as I use in one of my apps to launch app-specific settings menus. Still, I hope it helps.

blunden
  • 429
  • 3
  • 10
  • 8
    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:20
  • 1
    @EladNava I'm sorry to hear that. I hope you were able to get it solved. I'll include a warning in my answer above. – blunden Dec 07 '15 at 00:09
  • 1
    @EladNava Truecaller app pops up this "Ignore Battery Optimizations?" prompt and they are on Google play. (WHY?) – Pawan Sep 17 '16 at 13:45
  • 1
    Maybe they received explicit permission from Google. I don't know, I just wanted to warn everyone as my app got suspended for requesting this, even though the permission was critical to its function. – Elad Nava Sep 17 '16 at 13:51
  • 3
    There are use cases where Google allows it or denies it. Check this: https://developer.android.com/training/monitoring-device-state/doze-standby.html#whitelisting-cases – ᴛʜᴇᴘᴀᴛᴇʟ Feb 02 '17 at 16:07
  • 1
    And you also need, `` permission. It is not a `dangerous` permission so you don't need to ask the user for permission. It is automatically provided to you – ᴛʜᴇᴘᴀᴛᴇʟ Feb 02 '17 at 16:10
  • Google will suspend applications based on not having the "appearance" of necessity. They recently suspended my chat app for the permission, but the documentation provided in the suspension validated having it. The "person" sending the email (if a person at all) is not a developer. You can file an appeal and explain their policies or how their own services work to them in order to get the suspension revoked. It is sad they take such aggressive steps with such a blind automated process. Even Apple takes the time to open a dialogue for such issues. – Abandoned Cart May 08 '17 at 01:00
  • @LoungeKatt who / which e-mail address did you contact at Google to get them to let you add this permission to your app and upload to Google Play? I am in a similar situation (Doze stops GPS in my app working) and I can't find out how to get Google to let me use this permission! Thanks. – David Christopher Reynolds Sep 08 '17 at 13:57
  • @DavidChristopherReynolds I didn't contact them. They sent one of their automated messages to suspend my application and I appealed it. I cited their own documentation and the lack of competence by their review staff, which was only further evident by the month it took to process the appeal. Given, it came long after I had taken the backlash for not using the permission to avoid losing all of my customers to the suspension. – Abandoned Cart Nov 08 '17 at 20:05
  • Check Support for Other Use Cases here https://developer.android.com/training/monitoring-device-state/doze-standby.html#support_for_other_use_cases – Artist404 Mar 08 '18 at 06:50
  • This does not work. REQUEST_IGNORE_BATTERY_OPTIMIZATIONS is a permission, not a field of Settings. And you need this permission in the Manifest to be able to start the request. – neoexpert Jul 22 '19 at 06:51
  • @neoexpert: Not sure what you're talking about? It is both a permission and a field in Settings. https://github.com/LineageOS/android_packages_apps_Settings/blob/lineage-16.0/AndroidManifest.xml#L1139 – blunden Jul 23 '19 at 07:43
4

Google Allow this based on few conditions Check Section "Support For Other Use Cases"

here

Also check White List Conditions

Artist404
  • 395
  • 4
  • 17
0

There's currently no way to effectively opt out of optimizations, as this does not affect doze mode - even if your app is on the whitelist.

See here: https://stackoverflow.com/a/31721398/4301846

and here: https://code.google.com/p/android-developer-preview/issues/detail?id=2225

Community
  • 1
  • 1
sec_aw
  • 1,594
  • 1
  • 15
  • 26