AlarmManager 设置为相同的 BroadcastReceiver 不起作用

发布于 2024-11-24 04:42:37 字数 1304 浏览 1 评论 0原文

我希望能够将两个警报注册到同一个广播接收器。然而,第一个警报永远不会被触发。我怎样才能做到这一点?

Calendar now = Calendar.getInstance();
    now.set(Calendar.SECOND, now.get(Calendar.SECOND) + 5);
    long trigger1 = now.getTimeInMillis();
    now.set(Calendar.SECOND, now.get(Calendar.SECOND) + 10);
    long trigger2 = now.getTimeInMillis();

    Intent startIntent = new Intent(AlarmStartReceiver.START_ALARM);
    startIntent.putExtra(AlarmStartReceiver.EXTRA_ALARM_ID, 4);
    PendingIntent startPIntent = PendingIntent.getBroadcast(context, 0, startIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    Intent startIntent2 = new Intent(AlarmStartReceiver.START_ALARM);
    startIntent2.putExtra(AlarmStartReceiver.EXTRA_ALARM_ID, 5);
    PendingIntent startPIntent2 = PendingIntent.getBroadcast(context, 0, startIntent2, PendingIntent.FLAG_UPDATE_CURRENT);


    AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    alarm.set(AlarmManager.RTC_WAKEUP, trigger1, startPIntent);
    alarm.set(AlarmManager.RTC_WAKEUP, trigger2, startPIntent2);

只有第二个熄灭了。我怎样才能让它们都消失呢?

编辑答案:将 requestCode 设置为唯一的值。 PendingIntent.getBroadcast) 方法的第二个参数 android 待处理意图通知问题

I want to be able to register two alarms to the same BroadcastReceiver. However, the first alarm never gets fired. How can I make this work?

Calendar now = Calendar.getInstance();
    now.set(Calendar.SECOND, now.get(Calendar.SECOND) + 5);
    long trigger1 = now.getTimeInMillis();
    now.set(Calendar.SECOND, now.get(Calendar.SECOND) + 10);
    long trigger2 = now.getTimeInMillis();

    Intent startIntent = new Intent(AlarmStartReceiver.START_ALARM);
    startIntent.putExtra(AlarmStartReceiver.EXTRA_ALARM_ID, 4);
    PendingIntent startPIntent = PendingIntent.getBroadcast(context, 0, startIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    Intent startIntent2 = new Intent(AlarmStartReceiver.START_ALARM);
    startIntent2.putExtra(AlarmStartReceiver.EXTRA_ALARM_ID, 5);
    PendingIntent startPIntent2 = PendingIntent.getBroadcast(context, 0, startIntent2, PendingIntent.FLAG_UPDATE_CURRENT);


    AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    alarm.set(AlarmManager.RTC_WAKEUP, trigger1, startPIntent);
    alarm.set(AlarmManager.RTC_WAKEUP, trigger2, startPIntent2);

Only the second one goes off. How can I make them both go off?

EDIT FOR ANSWER: Set the requestCode to something unique. The second param of the PendingIntent.getBroadcast) method
android pending intent notification problem

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

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

发布评论

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

评论(2

萤火眠眠 2024-12-01 04:42:37

将 requestCode 设置为唯一的值。 PendingIntent.getBroadcast)方法的第二个参数android挂起意图通知问题

Set the requestCode to something unique. The second param of the PendingIntent.getBroadcast) method android pending intent notification problem

笔芯 2024-12-01 04:42:37

您是否正在为您设置的每个闹钟寻找单独的通知事件?或者它必须是与状态栏图标上显示的警报事件数量相同的通知?

查看这篇文章,了解如何使用“setData()”来创建单独的警报。

警报管理器 - 安排多个非重复事件

Are you looking for separate notification event for each alarm you are setting? Or it has to be same notification with number of alarm events showing on the status bar icon?

Look at this post on how you can use "setData()" to the intent to create separate alarms.

Alarm Manager - Scheduling multiple Non-repeating events

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