ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS:需要意图所有应用程序列表

问题描述 投票:0回答:2

我需要意图“不优化”ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS 设置页面。

但是当我将其更改为“不优化并返回”时,设置不会保存。 它将仍然在“优化”列表中

设备:一加5 操作系统:Android 9.0

代码:

startActivityForResult(new Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS), 0);

问题: 设置未保存。

还有“电池优化应用程序列表”可用的意图吗

android android-intent android-9.0-pie battery-saver
2个回答
9
投票

尝试此请求REQUEST_IGNORE_BATTERY_OPTIMIZATIONS。

  • 将此添加到清单中

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

  • 使用此功能请求优化。

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

它会要求用户将您的应用程序从电池优化中排除。


0
投票

这是我的代码,检查一下,随时询问

文件AndroidManifest.xml

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

检查(布尔值)和请求权限的函数

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

使用方法:

if (!checkBatteryLifePermission()) {
    requestBatteryOptimizationPermission()
}
© www.soinside.com 2019 - 2024. All rights reserved.