46

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

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

安卓设置截图

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

4

4 回答 4

48

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

<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);
        }
    }
于 2019-03-04T11:13:13.393 回答
36

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

PowerManager.isIgnoringBatteryOptimizations()

于 2016-08-31T18:30:52.240 回答
31

如何在 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)
        }
    }
于 2018-03-04T17:36:11.957 回答
12

示例代码:

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

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

于 2018-12-08T14:08:03.790 回答