AlarmManager在特定日期和时间

时间:2012-12-22 12:07:09

标签: android alarmmanager

我想在特定的日期和时间(2012年12月25日12点)显示一个对话框,我正在使用此代码。我设置为11个月(因为0是gen),但闹钟没有开始。我的错是什么?

Calendar cal=Calendar.getInstance();
    cal.set(Calendar.MONTH,11);
    cal.set(Calendar.YEAR,2012);
    cal.set(Calendar.DAY_OF_MONTH,25);
    cal.set(Calendar.HOUR_OF_DAY,12);
    cal.set(Calendar.MINUTE,00);
    cal.set(Calendar.SECOND,0);

Intent _myIntent = new Intent(context, Notify.class);
    PendingIntent _myPendingIntent = PendingIntent.getBroadcast(context, 123, _myIntent, PendingIntent.FLAG_UPDATE_CURRENT|  Intent.FILL_IN_DATA);

AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), _myPendingIntent);  

1 个答案:

答案 0 :(得分:7)

请看以下代码,了解我将如何设置12月份的闹钟:

// for Alarm 25/12/2012 at 12.00   
Calendar myAlarmDate = Calendar.getInstance();
myAlarmDate.setTimeInMillis(System.currentTimeMillis());
myAlarmDate.set(2012, 11, 25, 12, 00, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

Intent _myIntent = new Intent(context, AlarmReceiverNotificationForEveryMonth.class);
_myIntent.putExtra("MyMessage","HERE I AM PASSING THEPERTICULAR MESSAGE WHICH SHOULD BE SHOW ON RECEIVER OF ALARM");
PendingIntent _myPendingIntent = PendingIntent.getBroadcast(context, 123, _myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP, myAlarmDate.getTimeInMillis(),_myPendingIntent);

您可以根据自己的意图和课程更新上述代码,并获得您想要的输出。

希望这对你有所帮助。