如何正确触发 ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS 意图? [英] How do I properly fire ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS intent?

查看:47
本文介绍了如何正确触发 ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS 意图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如文档中所述:

拥有 REQUEST_IGNORE_BATTERY_OPTIMIZATIONS 权限的应用可以触发系统对话框,让用户直接将应用添加到白名单,而无需进行设置.应用会触发 ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS Intent 以触发对话框."

"An app holding the REQUEST_IGNORE_BATTERY_OPTIMIZATIONS permission can trigger a system dialog to let the user add the app to the whitelist directly, without going to settings. The app fires a ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS Intent to trigger the dialog."

有人能告诉我激发这个意图的正确方法吗?

Can someone tell me the proper way to fire this intent?

推荐答案

Intent intent = new Intent();
String packageName = context.getPackageName();
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
if (pm.isIgnoringBatteryOptimizations(packageName))
    intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
else {
    intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
    intent.setData(Uri.parse("package:" + packageName));
}
context.startActivity(intent);

有关详细信息,请参阅此答案.

See this answer for more information.

这篇关于如何正确触发 ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS 意图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆