6

I need an intent to "Don't optimize" settings page of ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS .

But when i change it to "Don't optimize and return , The settings are not saved . It will be still in the "Optimized" list

Device :one Plus 5 Os : Android 9.0

Code :

startActivityForResult(new Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS), 0);

Issue : Settings are not saved .

Also is there an intent available to "Battery optimized apps list ?"

Zoe
  • 27,060
  • 21
  • 118
  • 148
Rasheed
  • 542
  • 1
  • 6
  • 14

1 Answers1

8

Try this to request REQUEST_IGNORE_BATTERY_OPTIMIZATIONS.

  • Add this to Manifest

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

  • Use this function to request optimization.

    public static void BatteryOptimization(Context context){
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        Intent intent = new Intent();
        String packageName = context.getPackageName();
        PowerManager pm = (PowerManager) context.getSystemService(POWER_SERVICE);
        if (!pm.isIgnoringBatteryOptimizations(packageName)) {
            intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
            intent.setData(Uri.parse("package:" + "YOUR_PACKAGE_NAME"));
            context.startActivity(intent);
        }
      }
    }
    

It will ask user to exclude your app from battery optimization .

Kishan Thakkar
  • 429
  • 4
  • 11