I am trying to create an alarm method in android , it receives a specific date/time and has to to do something at that specific time.
now the first time it works fine,then it triggers immediately even thought the specified time is in the future .
here is my code :
public void CreateAlarmUTC(ScheduleRequest mrequest)
{
AlarmManager alarmMgr;
PendingIntent alarmIntent;
Context context;
int index = mrequest.Index;
context = getApplicationContext();
ScheduledDate date = SchMs.get(mrequest.Index).ScheduledDate;
String dat = date.calendar.getTime().toGMTString();
int uniqueTime = date.Minutes+date.Day*10+date.Year*100+date.Hour*1000+date.Month*10000;
alarmMgr = (AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(ALARM_SERVICE);
intent.putExtra("index", index);
alarmIntent = PendingIntent.getBroadcast(context, uniqueTime, intent, 0);
alarmMgr.set(AlarmManager.RTC_WAKEUP, date.calendar.getTimeInMillis(), alarmIntent);
}
ScheduleRequest is a class that I created , and it has an index refers to the place of an object in an array called SchMs.
ScheduledDate is a class that I created , it has a Calendar object called calendar.
Here is how I create the calendar in ScheduledDate :
Calendar cal = Calendar.getInstance();
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MINUTE, time_timePicker.getCurrentMinute());
cal.set(Calendar.HOUR_OF_DAY, time_timePicker.getCurrentHour());
cal.set(Calendar.DAY_OF_MONTH, time_datePicker.getDayOfMonth());
cal.set(Calendar.MONTH, time_datePicker.getMonth());
cal.set(Calendar.YEAR, time_datePicker.getYear());
time_timePicker is a TimePicker in my activity.
time_datePicker is a DatePicker in my activity.
Any ideas? Do you have a better way of executing a specific task at a specific time only once?
thanks
Finally after debugging , I found out that eclipse for some reason was adding a day more than I want only in the first time. For instance if I choose 14/09/2014 (DD/MM/YYYY) is assumes that I want 15/09/2014 , so I had to make my ScheduledDate.calendar as Today-1 :
cal.set(Calendar.DAY_OF_MONTH, time_datePicker.getDayOfMonth()-1);
After restaring eclipse, and more debugging , I found out that it went back to normal and I should keep it as it was originaly :
cal.set(Calendar.DAY_OF_MONTH, time_datePicker.getDayOfMonth());
so now it works just fine.
** The example above ,now, works just fine.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.