7

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

与项目 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)

知道如何解决这个问题吗?

链接中重要的代码是:

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

在清单中我有

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

1 回答 1

6

捕捉并向ActivityNotFoundException用户显示您自己的 UI,要求他们转到“设置”应用并将您的应用添加到电池优化白名单。虽然文档没有专门针对此Intent操作说明这一点,但大多数Settings.ACTION_*值都提到“在某些情况下,可能不存在匹配的 Activity,因此请确保您防范这种情况”。

于 2016-06-27T15:43:18.940 回答