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

Nik*_*Nik 19 android powermanager android-powermanager

Android 6和7有一些功耗优化(打盹模式),在不使用设备时限制应用程序网络.

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

android设置截图

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

Fri*_*ail 23

追踪这个有点棘手:这就是你要找的东西

PowerManager.isIgnoringBatteryOptimizations()

  • 如果您希望能够让用户翻转它:https://developer.android.com/reference/android/provider/Settings.html#ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS (2认同)
  • @ m3dw3您的问题有任何更新吗? (2认同)

Sau*_*war 13

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

<uses-permission  android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
Run Code Online (Sandbox Code Playgroud)

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

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);
        }
    }
Run Code Online (Sandbox Code Playgroud)

  • 是的,它工作得很好:D 但唯一的问题是该应用程序将被 Playstore 拒绝,您将失去所有业务! (16认同)
  • @sud007“ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS”是一个危险的权限。谷歌不喜欢应用程序在其代码中的任何地方使用此功能(https://developer.android.com/training/monitoring-device-state/doze-standby)。此权限和操作有点危险,因为如果我们添加此权限,我们将允许应用程序直接显示 Android 弹出窗口,要求用户关闭此应用程序的电池优化。相反,您应该做的是“ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS”,它将用户带到应用程序列表,他必须在其中搜索您的应用程序并手动禁用电池优化。 (8认同)
  • @RohitTP 您能否详细说明为什么在应用程序中添加此检查会使其成为 PlayStore 拒绝的候选者? (4认同)
  • 可能是金钱和裙带关系 (3认同)

Evg*_*bei 12

科特林

/**
 * 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
}
Run Code Online (Sandbox Code Playgroud)

这将显示优化活动

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)
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 为什么"> = VERSION_CODES.LOLLIPOP_MR1"?Android M中添加了ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS. (3认同)
  • 感谢您分享完整的解决方案。奇迹般有效! (2认同)
  • 根据 CommonsWare 的说法,如果您想使用 ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS 来引导用户直接进入特定于您的应用程序和白名单的页面,您需要在清单中请求 REQUEST_IGNORE_BATTERY_OPTIMIZATIONS,但不需要为其使用运行时权限代码。如果您想避免该权限,请改用 ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS。因此,如果您使用上述答案,则不需要 REQUEST_IGNORE_BATTERY_OPTIMIZATIONS 权限。 (2认同)

Yog*_*thi 10

示例代码:

 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
            }
Run Code Online (Sandbox Code Playgroud)

声明静态变量

private static final int IGNORE_BATTERY_OPTIMIZATION_REQUEST = 1002;
Run Code Online (Sandbox Code Playgroud)

显示 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();
        }
    }
Run Code Online (Sandbox Code Playgroud)

还需要为清单文件添加权限,即如下

<uses-permission  android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
Run Code Online (Sandbox Code Playgroud)

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