【问题标题】:Android: AlarmManager setExact() not firing event in Android 8Android:AlarmManager setExact() 在 Android 8 中未触发事件
【发布时间】:2018-07-23 15:42:20
【问题描述】:

我有一个 Android 实现,我必须在其中长期安排通知,我创建了 PendingIntent 并使用 AlarmManager#setExact() 方法来做到这一点:

Intent intent = new Intent(mContext, BirthdayReceiver.class);
intent.putExtra(BirthdayReceiver.INTENT_WHO, contact.getName());

PendingIntent pendingIntent = PendingIntent.getBroadcast(
        mContext, id, intent, 0);

mAlarmManager.setExact(
        AlarmManager.RTC,
        notificationCalendar.getTimeInMillis(),
        pendingIntent);

我检查了传递给 setExact() 方法的时间,如果时间很短(几分钟、几小时),则通知正确显示。

但如果我将通知设置为触发,假设第二天 12:00 它不起作用。

我正在使用 Android 8 设备对其进行测试。

有什么帮助吗?

【问题讨论】:

    标签: android


    【解决方案1】:

    似乎没有指定标志。 你能用FLAG_UPDATE_CURRENT 试试吗。

    PendingIntent pendingIntent = PendingIntent.getBroadcast(
            mContext, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    

    另外,为了确认您的 PendingIntent 在系统中是否仍然可用,您可以使用FLAG_NO_CREATE

    PendingIntent broadcastIntent = PendingIntent.getBroadcast(mContext, id,
              intent, PendingIntent.FLAG_NO_CREATE);
    

    如果 broadcastIntent 返回 null 则 PendingIntent 在系统中不可用(可能已取消)。

    根据setexact的文档

    这个方法类似于set(int, long, PendingIntent),但是不允许 操作系统调整交货时间。警报将作为 尽可能接近请求的触发时间。

    因此您可能不会期望警报会立即触发。

    注意:仅限对准确时间有强烈需求的警报 交货(例如在要求的时间响起的闹钟)应 准确安排。强烈反对申请 不必要地使用精确的警报,因为它们会降低操作系统的能力 尽量减少电池使用量。

    所以你真的可以考虑是否真的需要调用这个API

    【讨论】:

    • 对不起,如果我不够清楚,但警报永远不会触发。如果延迟几分钟……甚至几小时,对我来说没问题,但问题是该事件永远丢失了。
    • 感谢您的澄清。我更新了答案。希望对您有所帮助!
    • @svprdga 更新了答案,以了解系统中是否仍然存在待处理的意图。希望对你有帮助。
    猜你喜欢
    • 1970-01-01
    • 2012-08-16
    • 1970-01-01
    • 2019-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-19
    相关资源
    最近更新 更多