ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS : Need Intent to All Apps List
Asked Answered
M

2

8

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 ?"

Merla answered 22/4, 2019 at 12:19 Comment(0)
Z
10

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 .

Zootoxin answered 8/7, 2019 at 9:53 Comment(3)
including the line "intent.setData(Uri.parse("package:" + packageName));" can get your application blocked by google play. use it without this lineInsertion
I thought Google looks at this setting unfavorably now?Skell
@Insertion can you please provide the reason for your suggestion?Sasser
R
0

This is my code, check it, feel free to ask

File AndroidManifest.xml

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

Functions to check(boolean) and to request permission

// to check if the app has the permission or not
fun checkBatteryLifePermission(): Boolean {
    try {
        val pm: PowerManager = getSystemService(Context.POWER_SERVICE) as PowerManager
        val result = pm.isIgnoringBatteryOptimizations(this.packageName)
        return result
    } catch (e: Throwable) {
        println(e)
    }
    return false
}

// Request permission to ignore optimization
@SuppressLint("BatteryLife")
fun requestBatteryOptimizationPermission() {
    try {
        val intent = Intent()
        // intent.action = Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS
        intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
        intent.setData(Uri.parse("package:" + packageName));
        launcher.launch(intent)
    } catch (e: ActivityNotFoundException) {
        println(e)
    }
}

How to use:

if (!checkBatteryLifePermission()) {
    requestBatteryOptimizationPermission()
}
Ringer answered 16/3 at 3:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.