android - 打瞌睡模式三星设备

标签 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)

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

链接中的重要代码是:

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

在我的 list 中

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

最佳答案

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

关于android - 打瞌睡模式三星设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38057974/

相关文章:

android - 如何区分进入 Doze 维护窗口和退出 Doze 的设备

Android RecyclerView 缺少项目的大差距

Android - 将操作系统升级到 Marshmallow 后,权限如何在已安装的应用程序上运行

android-intent - 通过 Intent : SecurityException 发送电子邮件

android - 之前通过安装授予的权限,更新到使用 SDK 23 编译的新版本时会自动授予吗?

android - 解锁 android 设备在调用 ACTION_CALL Intent 时不工作

android - 如何正确触发 ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS Intent ?

android - 实体名称必须紧跟实体引用中的 '&'

android - 什么是 NoSaveStateFrameLayout?

android - 使用 Firebase 时,GoogleSignIn 被标记为内部登录,不应从应用访问