检查是否为应用程序启用了电池优化

Check if battery optimization is enabled or not for an app

Android 6 和 7 有一些电源优化(打瞌睡模式),在设备不使用时限制应用程序网络。

用户可以在电池设置中为任何应用禁用优化模式:

是否可以检查我的应用程序是否启用了优化?我需要让用户禁用优化以获得更好的应用程序功能,但我不知道如何以编程方式检查它。

这个有点难找:这就是你要找的东西

PowerManager.isIgnoringBatteryOptimizations()

如何用 Kotlin 写这个

  1. 检查是否启用了优化
    /**
     * return true if in App's Battery settings "Not optimized" and false if "Optimizing battery use"
     */
    fun isIgnoringBatteryOptimizations(context: Context): Boolean {
        val pwrm = context.applicationContext.getSystemService(Context.POWER_SERVICE) as PowerManager
        val name = context.applicationContext.packageName
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            return pwrm.isIgnoringBatteryOptimizations(name)
        }
        return true
    }
  1. 检查是否启用了优化,打开优化Activity
    fun checkBattery() {
        if (!isIgnoringBatteryOptimizations() && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            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)
        }
    }

示例代码:

 PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (pm != null && !pm.isIgnoringBatteryOptimizations(getPackageName())) {
                    askIgnoreOptimization();
                } else {
                   // already ignoring battery optimization code here next you want to do
                }
            } else {
           // already ignoring battery optimization code here next you want to do
            }

声明静态变量

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();
        }
    }

还需要添加清单文件的权限,如下所示

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

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

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

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

Request White-list / optimization enabled your app

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);
        }
    }