是否可以在android中使用此代码放置多个日期闹钟?

发布于 2024-12-23 06:52:11 字数 2972 浏览 3 评论 0原文

在我的应用程序中,我想在不同的日期设置不同的闹钟。 那么这段代码是否可能:

//For Broadcast Alarm
                alarmManager = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
                Intent in = new Intent(this, AlarmReceiverNotificationForTwoMonth.class);
                pendingIntentOfTwoMonth = PendingIntent.getBroadcast(this, 0, in, 0);

                // for the GST 20 June 2011
                Calendar calendar_GST_18_June_2011 = Calendar.getInstance();
                calendar_GST_18_June_2011.setTimeInMillis(System.currentTimeMillis());
                calendar_GST_18_June_2011.set(2011, 5, 18, mHour, mMinute, 0);
                alarmManager.set(AlarmManager.RTC_WAKEUP,   calendar_GST_18_June_2011.getTimeInMillis(), pendingIntentOfTwoMonth); 

                // for the GST 17 August 2011
                Calendar calendar_GST_17_August_2011 = Calendar.getInstance();
                calendar_GST_17_August_2011.setTimeInMillis(System.currentTimeMillis());
                calendar_GST_17_August_2011.set(2011, 7, 17, mHour, mMinute, 0);
                alarmManager.set(AlarmManager.RTC_WAKEUP,  calendar_GST_17_August_2011.getTimeInMillis(), pendingIntentOfTwoMonth);

                // for the GST 19 October 2011
                Calendar calendar_GST_19_October_2011 = Calendar.getInstance();
                calendar_GST_19_October_2011.setTimeInMillis(System.currentTimeMillis());
                calendar_GST_19_October_2011.set(2011, 9, 19, mHour, mMinute, 0);
                alarmManager.set(AlarmManager.RTC_WAKEUP,  calendar_GST_19_October_2011.getTimeInMillis(), pendingIntentOfTwoMonth);

                // for the GST 17 December 2011
                Calendar calendar_GST_17_December_2011 = Calendar.getInstance();
                calendar_GST_17_December_2011.setTimeInMillis(System.currentTimeMillis());
                calendar_GST_17_December_2011.set(2011, 11, 17, mHour, mMinute, 0);
                alarmManager.set(AlarmManager.RTC_WAKEUP,  calendar_GST_17_December_2011.getTimeInMillis(), pendingIntentOfTwoMonth);

                // for the GST 18 February 2012
                Calendar calendar_GST_18_February_2012 = Calendar.getInstance();
                calendar_GST_18_February_2012.setTimeInMillis(System.currentTimeMillis());
                calendar_GST_18_February_2012.set(2012, 1, 18, mHour, mMinute, 0);
                alarmManager.set(AlarmManager.RTC_WAKEUP,  calendar_GST_18_February_2012.getTimeInMillis(), pendingIntentOfTwoMonth);

                // for the GST 27 April 2012
                Calendar calendar_GST_27_April_2012 = Calendar.getInstance();
                calendar_GST_27_April_2012.setTimeInMillis(System.currentTimeMillis());
                calendar_GST_27_April_2012.set(2011, 3, 27, mHour, mMinute, 0);
                alarmManager.set(AlarmManager.RTC_WAKEUP,  calendar_GST_27_April_2012.getTimeInMillis(), pendingIntentOfTwoMonth);

谢谢。

In my Application i want to set the Different alarm on defferent date.
So is it possible with this code:

//For Broadcast Alarm
                alarmManager = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
                Intent in = new Intent(this, AlarmReceiverNotificationForTwoMonth.class);
                pendingIntentOfTwoMonth = PendingIntent.getBroadcast(this, 0, in, 0);

                // for the GST 20 June 2011
                Calendar calendar_GST_18_June_2011 = Calendar.getInstance();
                calendar_GST_18_June_2011.setTimeInMillis(System.currentTimeMillis());
                calendar_GST_18_June_2011.set(2011, 5, 18, mHour, mMinute, 0);
                alarmManager.set(AlarmManager.RTC_WAKEUP,   calendar_GST_18_June_2011.getTimeInMillis(), pendingIntentOfTwoMonth); 

                // for the GST 17 August 2011
                Calendar calendar_GST_17_August_2011 = Calendar.getInstance();
                calendar_GST_17_August_2011.setTimeInMillis(System.currentTimeMillis());
                calendar_GST_17_August_2011.set(2011, 7, 17, mHour, mMinute, 0);
                alarmManager.set(AlarmManager.RTC_WAKEUP,  calendar_GST_17_August_2011.getTimeInMillis(), pendingIntentOfTwoMonth);

                // for the GST 19 October 2011
                Calendar calendar_GST_19_October_2011 = Calendar.getInstance();
                calendar_GST_19_October_2011.setTimeInMillis(System.currentTimeMillis());
                calendar_GST_19_October_2011.set(2011, 9, 19, mHour, mMinute, 0);
                alarmManager.set(AlarmManager.RTC_WAKEUP,  calendar_GST_19_October_2011.getTimeInMillis(), pendingIntentOfTwoMonth);

                // for the GST 17 December 2011
                Calendar calendar_GST_17_December_2011 = Calendar.getInstance();
                calendar_GST_17_December_2011.setTimeInMillis(System.currentTimeMillis());
                calendar_GST_17_December_2011.set(2011, 11, 17, mHour, mMinute, 0);
                alarmManager.set(AlarmManager.RTC_WAKEUP,  calendar_GST_17_December_2011.getTimeInMillis(), pendingIntentOfTwoMonth);

                // for the GST 18 February 2012
                Calendar calendar_GST_18_February_2012 = Calendar.getInstance();
                calendar_GST_18_February_2012.setTimeInMillis(System.currentTimeMillis());
                calendar_GST_18_February_2012.set(2012, 1, 18, mHour, mMinute, 0);
                alarmManager.set(AlarmManager.RTC_WAKEUP,  calendar_GST_18_February_2012.getTimeInMillis(), pendingIntentOfTwoMonth);

                // for the GST 27 April 2012
                Calendar calendar_GST_27_April_2012 = Calendar.getInstance();
                calendar_GST_27_April_2012.setTimeInMillis(System.currentTimeMillis());
                calendar_GST_27_April_2012.set(2011, 3, 27, mHour, mMinute, 0);
                alarmManager.set(AlarmManager.RTC_WAKEUP,  calendar_GST_27_April_2012.getTimeInMillis(), pendingIntentOfTwoMonth);

Thanks.

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

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

发布评论

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

评论(1

要走干脆点 2024-12-30 06:52:11

不,不是。您将只有 1 个闹钟,即您创建的最后一个。这样做的原因是因为 PendingIntent 是相同的。您需要使用唯一的 PendingIntents 才能获取多个警报。我建议阅读有关 AlarmManager 和通知的 API 文档。

No it is not. You will only have 1 alarm, the last one you created. The reason for this is because the PendingIntent is the same. You need to use unique PendingIntents in order to get multiple alarms. I suggest reading the API docs on AlarmManager and Notifications.

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