Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set specific App to ignore optimization by code in Android M

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?

like image 839
Zheng Wei Avatar asked Jul 01 '15 06:07

Zheng Wei


2 Answers

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.

like image 174
blunden Avatar answered Sep 21 '22 20:09

blunden


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

here

Also check White List Conditions

like image 25
Artist404 Avatar answered Sep 19 '22 20:09

Artist404