Android M startActivity电池优化

Ast*_*Ast 6 optimization android battery android-intent

我正在开发一个应用程序,如果靠近某个地方,应该提醒用户.当然,如果手机处于闲置状态,也必须这样做.有了DOZE,我现在明白我必须将我的应用程序列入白名单,为此,我看到我可以通过动作请求启动一个意图,感谢Buddy在这篇文章中的回答

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

好吧这应该太容易了...因为谷歌不喜欢这种方法,如果你这样做,你的应用程序应该被禁止从游戏商店...没有评论...好的方式应该是驱动用户电池设置,并在DOZE的白名单中手动添加您的应用程序...是的,这应该是一个很大的攀爬墙...反正似乎是唯一的方式...现在答案是:我可以使用意图以这种方式转到电源使用情况摘要(谢谢Chris):

Intent powerUsageIntent = new Intent(Intent.ACTION_POWER_USAGE_SUMMARY);
    ResolveInfo resolveInfo = getPackageManager().resolveActivity(powerUsageIntent, 0);
// check that the Battery app exists on this device
    if(resolveInfo != null){
        startActivity(powerUsageIntent);
    }  
Run Code Online (Sandbox Code Playgroud)

但是如何直接进入应用程序列表来选择电池优化?

谢谢你的回答.

Gri*_*mmy 16

要打开用于选择电池优化的应用列表,您可以使用以下代码示例:

private void openPowerSettings(Context context) {
    Intent intent = new Intent();
    intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
    context.startActivity(intent);
}
Run Code Online (Sandbox Code Playgroud)

它不需要获得<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>许可,因此可以将其发布到Google Play(有关详细信息,请查看主题并对此问题发表评论).

  • 我收到一个致命致命错误:android.content.ActivityNotFoundException未找到任何处理意图的活动{act = android.settings.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS dat = package:io.demo.example} (3认同)