DOZE模式三星设备

JMR*_*JMR 6 android android-6.0-marshmallow android-doze

我正在一个Android 6中测试一个应用程序,我需要通过DOZE,因为我想请求用户接受电池优化的重要性.

在项目 AntiDoze中,但是当我在虚拟设备中运行时我没有任何问题,但是当我传递给三星时,我得到:

FATAL EXCEPTION: main
         Process: com.commonsware.android.antidoze, PID: 21135
       java.lang.RuntimeException: Unable to start activity ComponentInfo{com.commonsware.android.antidoze/com.commonsware.android.antidoze.EventDemoActivity}: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.settings.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS dat=package:com.commonsware.android.antidoze }
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3253)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3349)
      at android.app.ActivityThread.access$1100(ActivityThread.java:221)
Run Code Online (Sandbox Code Playgroud)

知道如何传递这个问题吗?

链接中重要的代码是:

if (Build.VERSION.SDK_INT>Build.VERSION_CODES.LOLLIPOP_MR1) {
  String pkg=getPackageName();
  PowerManager pm=getSystemService(PowerManager.class);

  if (!pm.isIgnoringBatteryOptimizations(pkg)) {
    Intent i=
      new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS)
        .setData(Uri.parse("package:"+pkg));

    startActivity(i);
  }
}
Run Code Online (Sandbox Code Playgroud)

在我的清单中

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

Com*_*are 5

抓住并向ActivityNotFoundException用户显示您自己的用户界面,要求他们转到"设置"应用并将您的应用添加到电池优化白名单中.虽然文档没有特别针对此Intent操作说明这一点,但大多数Settings.ACTION_*值都提到"在某些情况下,匹配的活动可能不存在,因此请确保您防范此操作".