irobotxx
irobotxx

Reputation: 6063

Set Alarms to Specific Date and Time in Android

Please kindly have a look at this. I am trying to set an alarm for a specific time and date. It does not seem to be working and i don't really know why.

             Calendar cal = Calendar.getInstance();

         cal.setTimeInMillis(System.currentTimeMillis());
         cal.set(Calendar.YEAR, mYear);
         cal.set(Calendar.MONTH,mMonth);
         cal.set(Calendar.DAY_OF_MONTH,mDay);
         cal.set(Calendar.HOUR,mHour);
         cal.set(Calendar.MINUTE,mMinutes);

// where mYear, mMonths, mDay, mHour and mMinutes are int values from the Date and Time picker dialogs respectively

     Intent activate = new Intent(this, Alarm.class);
     PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0, activate, 0);
     alarms = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
     alarms.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), alarmIntent);

just to let you know, it works if i replace the cal.getTimeInMillis() with System.CurrentTimeInMillis() in the alarms.set() methods. But thats obviously what i don't want. Thanks

Upvotes: 3

Views: 3106

Answers (1)

irobotxx
irobotxx

Reputation: 6063

Idiot me!! found the solution. All i had to do first, was call the clear method of the Calendar object after getting an instance.

Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
cal.clear();

Upvotes: 5

Related Questions