android - AlarmManager 未设置或在一定时间后未在 Marshmallow 上触发

标签 android alarmmanager android-6.0-marshmallow android-alarms android-doze

我已经成功地使用以下构造在我的一些应用程序中启动了一个 AlarmManager,直到 Android 5:

    Intent serviceIntent = new Intent(context, MyService.class);
    PendingIntent pi = PendingIntent.getService(context, alarmId, serviceIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    DateTime inMinutes = (new DateTime()).plusMinutes(60);


    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

    am.set(AlarmManager.RTC_WAKEUP, inMinutes.getMillis(), pi);

但是自 Marshmallow 以来,AlarmManager 要么未设置,要么在空闲时间后不再触发。似乎当前正在运行的警报再次触发,但不会设置新的警报。

我阅读了一些文档,很可能是关于 Marshmallow Doze 的。所以我实现了以下(并检查它是否正在执行):

    Intent serviceIntent = new Intent(context, MyService.class);
    PendingIntent pi = PendingIntent.getService(context, alarmId, serviceIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    DateTime inMinutes = (new DateTime()).plusMinutes(minutes);


    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

    if(Build.VERSION.SDK_INT >= 23)
        am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, inMinutes.getMillis(), pi);
    else {
        if(Build.VERSION.SDK_INT >= 19) {
            am.setExact(AlarmManager.RTC_WAKEUP, inMinutes.getMillis(), pi);
        } else {
            am.set(AlarmManager.RTC_WAKEUP, inMinutes.getMillis(), pi);
        }
    }

它不会改变任何东西。

有没有一种可靠的方法可以在 Marshmallow 上设置和触发警报,即使是在闲置一段时间后?

最佳答案

当然,试试:

       setAlarmClock(AlarmManager.AlarmClockInfo info, PendingIntent operation)

如果您以这种方式设置闹钟,则打瞌睡模式不会启动。我在控制台上对此进行了测试。

不要忘记 AlarmClock Info 有一个与 setAlarmclock 不同的 PendingIntent

更新

如果你想做一个简单的闹钟(不是闹钟)。

这必须工作

    setExactAndAllowWhileIdle(int type, long triggerAtMillis, PendingIntent operation)

但我现在知道它没有。所以我开始寻找和谷歌,我发现了这个。一种将应用程序添加到打瞌睡白名单的可能方法。也许 setExactAndAllowWhileIdle 有效。

https://developer.android.com/training/monitoring-device-state/doze-standby.html?hl=es

Users can manually configure the whitelist in Settings > Battery > Battery Optimization. Alternatively, the system provides ways for apps to ask users to whitelist them.

An app can fire the ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS intent to take the user directly to the Battery Optimization, where they can add the app. 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. The user can manually remove apps from the whitelist as needed. Before asking the user to add your app to the whitelist, make sure the app matches the acceptable use cases for whitelisting.

关于android - AlarmManager 未设置或在一定时间后未在 Marshmallow 上触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37607329/

相关文章:

android - 应用程序安装大小在手机上有何不同?

android - 警报通知立即触发。安卓

Android M 写入 SD 卡 - 权限被拒绝

android - 如果要向Android Debounce添加条件,该怎么办?

android - ViewPager 在某些 Android 设备上不工作

android - 带有 BaseAdapter 的 Android ListView 中的 ImageButton

android - AlarmManager 没有启动 Activity(当它存在时)

android - AlarmManager的RTC和RTC_WAKEUP有什么区别

android - 如何使用我的应用程序更改 facebook 权限?

android - 我可以跳过询问棉花糖的运行时权限吗?