AlarmManager 过去的警报是如何传送的?

发布于 2024-10-07 08:18:26 字数 336 浏览 2 评论 0原文

我有一个关于在 AlarmManager 中设置警报的问题。我在文档中发现了一些我不理解的内容(见下文)。我想设置 10 个闹钟,它们交替触发铃声模式为静音和正常,并且都有不同的触发时间。现在设备进入睡眠状态,并在所有 10 个警报过期后再次激活。 AlarmManager 是否会立即广播警报?只会是第十个(振铃模式怎么样)?

警报意图通过 int 类型的额外数据称为 Intent.EXTRA_ALARM_COUNT 表示 指示过去有多少个警报事件 已经累积成这个意图 播送。重复出现的警报 由于电话未送达 sleep 的计数可能大于 交付时一份。

I've got a question about setting alarms in the AlarmManager. I found something in the docs I didn't understand (see below). I'd like to set 10 alarms which trigger the ringer mode alternately silent and normal, all with a different trigger time. Now the device goes asleep, and becomes active again after all 10 alarms are outdated. Does the AlarmManager then immediately broadcast an alarm? Would it be only the 10th (what about the ringer mode)?

Alarm intents are delivered with a
data extra of type int called
Intent.EXTRA_ALARM_COUNT that
indicates how many past alarm events
have been accumulated into this intent
broadcast. Recurring alarms that have
gone undelivered because the phone was
asleep may have a count greater than
one when delivered.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

因为看清所以看轻 2024-10-14 08:18:26

最不为人知的一件事(主要是因为 Android 文档告诉其 "暂时未使用")是如果requestCode不同则PendingIntent不会被重用。因此,请使用请求代码 0 创建 PI:

    PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0); 

您可以实现一个计数器并执行以下操作:

    PendingIntent pendingIntent = PendingIntent.getService(context, counter, intent, 0); 

我知道这适用于短信发送/发送的通知 PendingIntents,您会遇到同样的问题:如果重用 PendingIntent 并且您有超过 1 条未完成的通知,您将不知道它是哪一条短信。
但很有可能这也适用于突出的警报 PendingIntent。

希望这有帮助。

One thing that is most unknown (mainly because the Android documentation tells that its "not used at the moment") is that the PendingIntent will not be reused if the requestCode differs. So instead create the PI with an request code of 0:

    PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0); 

You could implement a counter and do someting like:

    PendingIntent pendingIntent = PendingIntent.getService(context, counter, intent, 0); 

I know that this will work for SMS delivered/sent notifications PendingIntents, where you have the same problem: If the PendingIntent is reused and you have more than 1 outstanding notification, you will not know for which SMS it was.
But chances are good that this will also work for outstanding alarm PendingIntent.

Hope this helps.

萝莉病 2024-10-14 08:18:26

据我了解,当使用警报管理器安排警报时,您必须提供一个 PendingIntent 实例。

有两种类型的闹钟,一种即使手机处于睡眠状态或锁定状态也会被唤醒并工作,另一种则不会。

另外,如果您要一次安排 10 件事,则 AlarmManager 会将现有的计划挂起意图替换为新的意图,除非您为其指定不同的意图操作。当我使用警报时,我总是使用 sqlite 数据库来对我想要按计划执行的作业进行排队。从那里我会一次安排一个警报,因为当蜂鸣器响起时它们都执行相同的意图。

如果您安排了重复发生的闹钟,并且当用户设备处于睡眠状态时闹钟多次响起,则 EXTRA_ALARM_COUNT 额外功能将发挥作用。当手机唤醒时,它将重播过去排队的任何内容。在这种情况下,您的挂起意图将触发,并具有闹钟被跳过的次数的值,因为它是在调用 set 方法时使用 RTC 或 ELAPSED_REALTIME 作为类型构建的。

这是我通常如何与 AlarmManger 交互的示例

protected void scheduleNext(Context context) {
    AlarmManager alarmManager = getAlarmManager();
    Intent intent = new Intent(MyIntent.ACTION_DO_WORK);
    PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0); 

    String where = Queue.SCHEDULED_DATE + "= (select min(" + Queue.SCHEDULED_DATE + ") from queue where " + Queue.COMPLETED_DATE + " is null)";
    Cursor cursor = context.getContentResolver().query(Queue.CONTENT_URI, Queue.PROJECTION, where, null, null);

    if (cursor.moveToFirst()) {
        int id = cursor.getInt(cursor.getColumnIndex(Queue._ID));
        long when = cursor.getLong(cursor.getColumnIndex(Queue.SCHEDULED_DATE));
        alarmManager.set(AlarmManager.RTC_WAKEUP, when, pendingIntent);
    }   

    cursor.close();
}

From what I understand, when scheduling an alarm with an alarm manager, you have to provide a PendingIntent instance.

There are two Types of alarms ones that will wake up and do work even if the phone is asleep or locked and ones that will not.

Also, If you were to schedule 10 things at one time The AlarmManager will replace the existing Scheduled Pending intent with the new one, unless you were giving it different intent actions. When I use Alarms I have always used a sqlite database to queue up jobs that I wanted to execute on some schedule. From there I would schedule one alarm at a time, because they all executed the same Intent when the buzzer went ding.

The EXTRA_ALARM_COUNT extra would come into play if you had a reoccurring alarm scheduled and it went off multiple times when the users device was asleep. When the phone wakes up it will replay anything that it has queued up in the past. In this case your pending intent will fire off and have the value of how many times your Alarm was skipped because it was constructed with RTC or ELAPSED_REALTIME as the type when calling the set method.

Here is a sample of how I usually interact with the AlarmManger

protected void scheduleNext(Context context) {
    AlarmManager alarmManager = getAlarmManager();
    Intent intent = new Intent(MyIntent.ACTION_DO_WORK);
    PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0); 

    String where = Queue.SCHEDULED_DATE + "= (select min(" + Queue.SCHEDULED_DATE + ") from queue where " + Queue.COMPLETED_DATE + " is null)";
    Cursor cursor = context.getContentResolver().query(Queue.CONTENT_URI, Queue.PROJECTION, where, null, null);

    if (cursor.moveToFirst()) {
        int id = cursor.getInt(cursor.getColumnIndex(Queue._ID));
        long when = cursor.getLong(cursor.getColumnIndex(Queue.SCHEDULED_DATE));
        alarmManager.set(AlarmManager.RTC_WAKEUP, when, pendingIntent);
    }   

    cursor.close();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文