Reputation: 11
When I set an alarm in Using Alarm manager, it would always immediately show my alarm. I realized that this was because the alarm I was actually setting was in the past.
When I use Java's Calendar.getInstance() to sync with the current time, it gives me a time that is 5 hours ahead of my current time zone. The wierd thing is that whatever clock is used to run AlarmManager seems to be ahead by the same amount.
This is evident by the fact that when I create an alarm by taking the current time and adding x number of seconds to it, it goes off at the right time, yet if I set an alarm that, according to my phone's clock, is in the future, it is considered to be in the past.
How can I reset the clock that alarmManager and java cal use, so that they correspond with my current time?
Here's the code I'm using to make the alarms.
public void setAlarmFromCal(Calendar cal,int type) {
Calendar calendar = Calendar.getInstance();
Intent intent = new Intent(mContext, AlarmReceiver.class);
intent.putExtra("type",type);
PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, Utility.getUniqueId(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
Toast.makeText(mContext, "was set", Toast.LENGTH_SHORT).show();
}
And here is the code to create the Cal that the alarm uses.
public Calendar eventAlarmToCal(Alarm alarm){
Calendar cal = Calendar.getInstance();
int hour = Integer.parseInt(alarm.getHour());
int minute = Integer.parseInt(alarm.getMinute());
Calendar now = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY,hour+5);//DISGUSTING!! pLEAse forgive me. I couldn't ifgure out why the time was 5 hours ahead... so id did +5 hours...
cal.set(Calendar.MINUTE,minute);
cal.set(Calendar.SECOND,0);
if(!(alarm.getDay().equals("today"))){
cal.add(Calendar.DATE,1);
}
return cal;
}
The alarm object stores the minute and hour in strings that are given my user input.
Alarm alarm4 = new Alarm("17","0","today","0","0");
The constructor
public Alarm(String hour,String minute,String day,String id,String eventID){
this.hour = hour;
this.minute = minute;
this.day = day;
this.id = id;
this.eventID = eventID;
}
Upvotes: 1
Views: 211