是否可以在android中使用此代码放置多个日期闹钟?
在我的应用程序中,我想在不同的日期设置不同的闹钟。 那么这段代码是否可能:
//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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,不是。您将只有 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.