如何更改 AlarmManager 警报?

发布于 2024-11-14 15:41:59 字数 947 浏览 3 评论 0原文

我在我的应用程序中使用 AlarmManager 在适当的时间设置闹钟。我的应用程序中有多个闹钟,因此每次用户保存闹钟时,我都会找到下次应该播放哪个闹钟,并将该闹钟的 ID 作为意图的额外内容传递。这是我使用的代码:

Intent intent = new Intent(this, AlarmBroadcastReceiver.class);
intent.putExtra("alrmId", finalAlr);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 56, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
alarmManager.cancel(pendingIntent);
alarmManager.set(AlarmManager.RTC_WAKEUP, (System.currentTimeMillis() + (finalAlrDay * 24 * 60 * 60 * 1000) + (finalAlrHr * 60 * 60 * 1000) + (finalAlrMin * 60 * 1000) + (finalAlrSec * 1000)), pendingIntent);

在这里,如果有任何旧的警报设置,我会取消,然后添加新的警报。所有闹钟都会在正确的时间播放,但问题是我在 intent.putExtra 中设置的 alrmId 值始终与我第一次设置时保持相同。

例如,如果我第一次设置闹钟,当时 alrmId 设置为“1”,那么无论我之后输入什么值,它都将始终保持不变。我尝试对其进行调试,并确保 intent.putExtra("alrmId", FinalAlr) 输入正确的值,因此这不是问题。问题是什么?

I am using AlarmManager in my app to set an alarm at an appropriate time. I have multiple alarms in my app so every time the user saves an alarm, I find which alarm should be played next time and pass the ID of that alarm as an intent's extra. Here is the code I use for that:

Intent intent = new Intent(this, AlarmBroadcastReceiver.class);
intent.putExtra("alrmId", finalAlr);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 56, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
alarmManager.cancel(pendingIntent);
alarmManager.set(AlarmManager.RTC_WAKEUP, (System.currentTimeMillis() + (finalAlrDay * 24 * 60 * 60 * 1000) + (finalAlrHr * 60 * 60 * 1000) + (finalAlrMin * 60 * 1000) + (finalAlrSec * 1000)), pendingIntent);

Here, I cancel if there is any old alarm set and then add new one. All the alarms play at the right time but the problem is that the alrmId value that I set in intent.putExtra always remains the same as when I set it for the first time.

For example, if I set an alarm for the first time and at that time alrmId is set to be '1' then it'll always stays the same no matter what value I put in after that. I have tried debugging it and I made sure that intent.putExtra("alrmId", finalAlr) is putting in the right value so that is not the problem. What is the problem?

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

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

发布评论

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

评论(2

栀梦 2024-11-21 15:41:59

在创建 <代码>PendingIntent。

Use FLAG_UPDATE_CURRENT when creating your PendingIntent.

再可℃爱ぅ一点好了 2024-11-21 15:41:59

您还可以使用:

final PendingIntent pendingIntent = PendingIntent.getService(
    contextWeakReference.get(),
    0,
    notificationIntent,
    PendingIntent.FLAG_CANCEL_CURRENT
);

正如文档所述:

   /**
     * Flag indicating that if the described PendingIntent already exists,
     * the current one should be canceled before generating a new one.
     * For use with {@link #getActivity}, {@link #getBroadcast}, and
     * {@link #getService}. <p>You can use
     * this to retrieve a new PendingIntent when you are only changing the
     * extra data in the Intent; by canceling the previous pending intent,
     * this ensures that only entities given the new data will be able to
     * launch it.  If this assurance is not an issue, consider
     * {@link #FLAG_UPDATE_CURRENT}.
     */
    public static final int FLAG_CANCEL_CURRENT = 1<<28;


    /**
     * Flag indicating that if the described PendingIntent already exists,
     * then keep it but replace its extra data with what is in this new
     * Intent. For use with {@link #getActivity}, {@link #getBroadcast}, and
     * {@link #getService}. <p>This can be used if you are creating intents where only the
     * extras change, and don't care that any entities that received your
     * previous PendingIntent will be able to launch it with your new
     * extras even if they are not explicitly given to it.
     */
    public static final int FLAG_UPDATE_CURRENT = 1<<27;

You could also use:

final PendingIntent pendingIntent = PendingIntent.getService(
    contextWeakReference.get(),
    0,
    notificationIntent,
    PendingIntent.FLAG_CANCEL_CURRENT
);

As the documentation says:

   /**
     * Flag indicating that if the described PendingIntent already exists,
     * the current one should be canceled before generating a new one.
     * For use with {@link #getActivity}, {@link #getBroadcast}, and
     * {@link #getService}. <p>You can use
     * this to retrieve a new PendingIntent when you are only changing the
     * extra data in the Intent; by canceling the previous pending intent,
     * this ensures that only entities given the new data will be able to
     * launch it.  If this assurance is not an issue, consider
     * {@link #FLAG_UPDATE_CURRENT}.
     */
    public static final int FLAG_CANCEL_CURRENT = 1<<28;


    /**
     * Flag indicating that if the described PendingIntent already exists,
     * then keep it but replace its extra data with what is in this new
     * Intent. For use with {@link #getActivity}, {@link #getBroadcast}, and
     * {@link #getService}. <p>This can be used if you are creating intents where only the
     * extras change, and don't care that any entities that received your
     * previous PendingIntent will be able to launch it with your new
     * extras even if they are not explicitly given to it.
     */
    public static final int FLAG_UPDATE_CURRENT = 1<<27;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文