一定时间后,警报管理器未设置或未在棉花糖上触发 [英] AlarmManager not set or not firing on Marshmallow after certain time

查看:161
本文介绍了一定时间后,警报管理器未设置或未在棉花糖上触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成功使用以下结构在Android 5之前的某些应用中启动AlarmManager:

I have been successfully using the following construct to start an AlarmManager in some of my apps upto 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);

但是由于棉花糖的缘故,AlarmManager要么未设置,要么在经过一段空闲时间后不再触发。似乎当前正在运行的警报会再触发一次,但不会设置新的警报。

But since Marshmallow, the AlarmManager is either not set or not firing any more after some idle time. It seems like the currently running alarm fires one more time, but then no new alarm will be set.

我阅读了一些文档,这很可能是关于棉花糖打ze睡的。因此,我实现了以下内容(并检查了它是否实际上已在执行):

I read some documentation and it's most probably about Marshmallow Doze. So I implemented the following (and checked that it's actually being executed):

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

它什么都不会改变。

即使经过一段空闲时间后,即使在棉花糖上,也有可靠的方法来设置和触发警报吗?

Is there a reliable way to set and fire alarms even on Marshmallow after some idle time?

推荐答案

当然,请尝试:

       setAlarmClock(AlarmManager.AlarmClockInfo info, PendingIntent operation)

如果您以此方式设置警报,打ze模式就不会启动。我在控制台上进行了测试。

Doze mode dont start if you set the alarm this way.I tested this on console.

别忘了AlarmClock Info的 PendingIntent setAlarmclock 不同。

Dont forget that AlarmClock Info have a different PendingIntentthat setAlarmclock.

更新

如果要制作简单的闹钟(不是闹钟)。

If you want to make a simple alarm(not alarmclock).

这必须工作

    setExactAndAllowWhileIdle(int type, long triggerAtMillis, PendingIntent operation)

但是我现在没有。所以我开始寻找和谷歌,我发现了这一点。添加应用以打入白名单的一种可能方法。也许 setExactAndAllowWhileIdle 有效。

But i now that it doesn't. So i started looking and google and i found this. A possible way to add an app to doze whitelist. Maybe setExactAndAllowWhileIdle works.

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.

应用程序可以触发ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS目的将用户直接带到电池优化,在那里他们可以添加应用。
拥有REQUEST_IGNORE_BATTERY_OPTIMIZATIONS权限的应用程序可以触发系统对话框,使用户可以直接将应用程序添加到白名单中,而无需进行设置。该应用会触发ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS意图来触发对话框。
用户可以根据需要从白名单中手动删除应用程序。
在要求用户将您的应用程序添加到白名单之前,请确保该应用程序符合白名单可接受的用例。

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.

这篇关于一定时间后,警报管理器未设置或未在棉花糖上触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆