Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AlarmManager at specific date and time

I'd like to show a dialog at specific date and time (25/12/2012 at 12.00) and I am using this code. I set like 11 month (because 0 is gen) but the alarm does not start. What is my mistake?

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);  
like image 535
Pol Hallen Avatar asked Feb 19 '23 02:02

Pol Hallen


1 Answers

Look at below code about how i am going to set the alarm for the December month:

// 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);

You can update the above code with your intent and class and you will get your desire output.

Hope this help you.

like image 161
Shreyash Mahajan Avatar answered Feb 20 '23 16:02

Shreyash Mahajan