Android alarm sets off immediately - android

Why does the alarm set off immediately instead of the specified time?
Intent myIntent = new Intent(Notepad.this, MyAlarmService.class);
PendingIntent pendingIntent = PendingIntent.getService(Notepad.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.YEAR, 2012);
calendar.set(Calendar.MONTH, 4);
calendar.set(Calendar.DAY_OF_MONTH, 5);
calendar.set(Calendar.HOUR_OF_DAY, 10);
calendar.set(Calendar.MINUTE, 30);
calendar.set(Calendar.SECOND, 4);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
If I use only calendar.add(Calendar.SECOND, 5);, it sets off after 5 seconds.
SOLUTION based on Nicholas' answer
Date dat = new Date();//initializes to now
Calendar cal_alarm = Calendar.getInstance();
Calendar cal_now = Calendar.getInstance();
cal_now.setTime(dat);
cal_alarm.setTime(dat);
cal_alarm.set(Calendar.HOUR_OF_DAY,Integer.valueOf(h));
cal_alarm.set(Calendar.MINUTE, Integer.valueOf(m));
cal_alarm.set(Calendar.SECOND,0);
if(cal_alarm.before(cal_now)){
cal_alarm.add(Calendar.DATE,1);
}

To me it seems that you are setting the calendar time and day to the alarm time. Maybe set up two different calendars and have one for the current time and another for the alarm time. Does that make sense?

Related

How can I set next day if my alarm's time is past? [duplicate]

The reference for Alarm Manager says that
If the stated trigger time is in the past, the alarm will be triggered
immediately.
I am facing this problem in my application. Here is my alarm manager code :
Intent myIntent = new Intent(getActivity(), DinnerAlarmReceiver.class);
pendingDinnerIntent = PendingIntent.getBroadcast(getActivity(), 0, myIntent, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
alarmManager.set(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), pendingDinnerIntent);
Is there any workaround to this problem?
-----EDIT------
I have written some code to estimate if the set time for the alarm is before the current time . Here is the above portion with corresponding changes :
Calendar calendar = Calendar.getInstance();
long currentTime = calendar.getTimeInMillis();
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
long setTime = calendar.getTimeInMillis();
Timestamp setTimestamp = new Timestamp(setTime);
Timestamp currentTimestamp = new Timestamp(currentTime);
if (setTimestamp.after(currentTimestamp))
{
alarmManager.set(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), pendingDinnerIntent);
}
else
{
}
What should I the alarmManager to in case setTimestamp is before currentTimestamp ?
You don't need to create Timestamps. You can do it with your Calendar.
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
if(calendar.before(Calendar.getInstance())) {
calendar.add(Calendar.DATE, 1);
}
alarmManager.set(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), pendingDinnerIntent);
I would also mention that as of KitKat, if your targetSdkVersion is 19 or above, the AlarmManager#set() method is not exact. If you want your alarm to fire at an exact time, you'll need to use a setExact*() method.

Android: Alarm Manager: Set repeating triggers alarm if set time already has passed [duplicate]

The reference for Alarm Manager says that
If the stated trigger time is in the past, the alarm will be triggered
immediately.
I am facing this problem in my application. Here is my alarm manager code :
Intent myIntent = new Intent(getActivity(), DinnerAlarmReceiver.class);
pendingDinnerIntent = PendingIntent.getBroadcast(getActivity(), 0, myIntent, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
alarmManager.set(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), pendingDinnerIntent);
Is there any workaround to this problem?
-----EDIT------
I have written some code to estimate if the set time for the alarm is before the current time . Here is the above portion with corresponding changes :
Calendar calendar = Calendar.getInstance();
long currentTime = calendar.getTimeInMillis();
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
long setTime = calendar.getTimeInMillis();
Timestamp setTimestamp = new Timestamp(setTime);
Timestamp currentTimestamp = new Timestamp(currentTime);
if (setTimestamp.after(currentTimestamp))
{
alarmManager.set(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), pendingDinnerIntent);
}
else
{
}
What should I the alarmManager to in case setTimestamp is before currentTimestamp ?
You don't need to create Timestamps. You can do it with your Calendar.
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
if(calendar.before(Calendar.getInstance())) {
calendar.add(Calendar.DATE, 1);
}
alarmManager.set(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), pendingDinnerIntent);
I would also mention that as of KitKat, if your targetSdkVersion is 19 or above, the AlarmManager#set() method is not exact. If you want your alarm to fire at an exact time, you'll need to use a setExact*() method.

Alarm is setting earlier time for example current time is 3:00 pm if I set 2:00 pm it is going to be ring within a minute [duplicate]

The reference for Alarm Manager says that
If the stated trigger time is in the past, the alarm will be triggered
immediately.
I am facing this problem in my application. Here is my alarm manager code :
Intent myIntent = new Intent(getActivity(), DinnerAlarmReceiver.class);
pendingDinnerIntent = PendingIntent.getBroadcast(getActivity(), 0, myIntent, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
alarmManager.set(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), pendingDinnerIntent);
Is there any workaround to this problem?
-----EDIT------
I have written some code to estimate if the set time for the alarm is before the current time . Here is the above portion with corresponding changes :
Calendar calendar = Calendar.getInstance();
long currentTime = calendar.getTimeInMillis();
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
long setTime = calendar.getTimeInMillis();
Timestamp setTimestamp = new Timestamp(setTime);
Timestamp currentTimestamp = new Timestamp(currentTime);
if (setTimestamp.after(currentTimestamp))
{
alarmManager.set(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), pendingDinnerIntent);
}
else
{
}
What should I the alarmManager to in case setTimestamp is before currentTimestamp ?
You don't need to create Timestamps. You can do it with your Calendar.
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
if(calendar.before(Calendar.getInstance())) {
calendar.add(Calendar.DATE, 1);
}
alarmManager.set(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), pendingDinnerIntent);
I would also mention that as of KitKat, if your targetSdkVersion is 19 or above, the AlarmManager#set() method is not exact. If you want your alarm to fire at an exact time, you'll need to use a setExact*() method.

android prevent immediate trigger of alarm service if alarm time has passed for the day

The reference for Alarm Manager says that
If the stated trigger time is in the past, the alarm will be triggered
immediately.
I am facing this problem in my application. Here is my alarm manager code :
Intent myIntent = new Intent(getActivity(), DinnerAlarmReceiver.class);
pendingDinnerIntent = PendingIntent.getBroadcast(getActivity(), 0, myIntent, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
alarmManager.set(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), pendingDinnerIntent);
Is there any workaround to this problem?
-----EDIT------
I have written some code to estimate if the set time for the alarm is before the current time . Here is the above portion with corresponding changes :
Calendar calendar = Calendar.getInstance();
long currentTime = calendar.getTimeInMillis();
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
long setTime = calendar.getTimeInMillis();
Timestamp setTimestamp = new Timestamp(setTime);
Timestamp currentTimestamp = new Timestamp(currentTime);
if (setTimestamp.after(currentTimestamp))
{
alarmManager.set(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), pendingDinnerIntent);
}
else
{
}
What should I the alarmManager to in case setTimestamp is before currentTimestamp ?
You don't need to create Timestamps. You can do it with your Calendar.
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
if(calendar.before(Calendar.getInstance())) {
calendar.add(Calendar.DATE, 1);
}
alarmManager.set(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), pendingDinnerIntent);
I would also mention that as of KitKat, if your targetSdkVersion is 19 or above, the AlarmManager#set() method is not exact. If you want your alarm to fire at an exact time, you'll need to use a setExact*() method.

Implementing an alarm every 5 days, code correct?

I am trying to set an alarm every 5th day of the week and the 24th hour of that day.
Here is the code i am using. Ive been reading over the Calendar and AlarmManager docs, a
and here is what i have came up with.
String alarm = Context.ALARM_SERVICE;
//Alert for game covers
am = (AlarmManager)context.getSystemService(alarm);
calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_WEEK, 5);
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
Intent Aintent = new Intent("REFRESH_THIS");
PendingIntent pi = PendingIntent.getBroadcast(context, 0, Aintent, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis() , AlarmManager.INTERVAL_DAY, pi);
Is this correct for what i want to do?
To get a Calendar instance, that points to a date 5 days in the future, you take the current date and add 5 days like this:
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, 5);
Then you set your alarm:
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
pendingIntent);

Categories

Resources