Check if battery optimization is enabled or not for an app

时间:2016-08-31 18:22:17

标签: android powermanager android-powermanager

Android 6 and 7 have some power optimizations (doze mode) which restrict app networking when device is not used.

User may disable optimization mode for any app in Battery settings:

android settings screenshot

Is it possible to check if optimization is enabled for my app or not? I need to ask user to disable optimization for better app functionality, but I don't know how to check it programatically.

4 个答案:

答案 0 :(得分:15)

This one was a bit tricky to track down: here's what you are looking for

PowerManager.isIgnoringBatteryOptimizations()

答案 1 :(得分:11)

<强>科特林

/**
 * return false if in settings "Not optimized" and true if "Optimizing battery use"
 */
fun checkBatteryOptimized(): Boolean {
    val pwrm = applicationContext.getSystemService(Context.POWER_SERVICE) as PowerManager
    val name = applicationContext.packageName
    if (VERSION.SDK_INT >= VERSION_CODES.M) {
        return !pwrm.isIgnoringBatteryOptimizations(name)
    }
    return false
}

这将显示优化活动

fun checkBattery() {
    if (isBatteryOptimized() && VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP_MR1) {
        val name = resources.getString(R.string.app_name)
        Toast.makeText(applicationContext, "Battery optimization -> All apps -> $name -> Don't optimize", Toast.LENGTH_LONG).show()

        val intent = Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS)
        startActivity(intent)
    }
}

答案 2 :(得分:3)

在您的清单中添加此权限。

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

请求白名单/优化已启用您的应用

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

答案 3 :(得分:1)

示例代码:

 PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (pm != null && !pm.isIgnoringBatteryOptimizations(getPackageName())) {
                    askIgnoreOptimization();
                } else {
                    accepted;
                }
            } else {
                accepted;
            }

声明静态变量

private static final int IGNORE_BATTERY_OPTIMIZATION_REQUEST = 1002;

BATTERY_OPTIMIZATIONS的显示对话框

 private void askIgnoreOptimization() {

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
            Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
            intent.setData(Uri.parse("package:" + getPackageName()));
            startActivityForResult(intent, IGNORE_BATTERY_OPTIMIZATION_REQUEST);
        } else {
            openNextActivity();
        }
    }

也许这段代码对您有帮助!