saman0suke
saman0suke

Reputation: 762

Why my alarmManager starts immediately if I setup for a previous day?

I have an AlarmManager application in Android, it's working correctly except for one thing, if today is Thursday and I set it for Wednesday, after I finish the configuration, it starts immediately, it only happens if I try to set it for a previous day than today, this is my code:

public void setupAlarm(View v){
    Calendar cal = Calendar.getInstance();

    AlarmManager alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);

    //Setting up for Monday, as an example
    cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
    cal.set(Calendar.HOUR_OF_DAY, tPicker.getCurrentHour()); //tPicker is a TimePicker
    cal.set(Calendar.MINUTE, tPicker.getCurrentMinute());
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);

    alarmMgr.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pIntent(this, Calendar.MONDAY));
}

public static pIntent testThis(Context context, int dayId){
    Intent intent = new Intent();
    intent.setAction("myIntent.intent.action.CLOCK");

    return PendingIntent.getBroadcast(context, dayId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}

As I said it works great for today and the days after, but why does it starts immediately if I setup a previous day? I will appreciate your help, thank you!

EDIT: Ok, this is what I have based on suggestions, and seems like working fine: I have a checkbox for each of the days in the week, for example Monday:

Calendar cal = Calendar.getInstance();
Calendar calMon = Calendar.getInstance();

calMon.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);

calMon.set(Calendar.HOUR_OF_DAY, tPicker.getCurrentHour());
calMon.set(Calendar.MINUTE, tPicker.getCurrentMinute());
calMon.set(Calendar.SECOND, 0);
calMon.set(Calendar.MILLISECOND, 0);

if((cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) || 
   (cal.get(Calendar.DAY_OF_WEEK) > Calendar.MONDAY) || 
  ((cal.get(Calendar.DAY_OF_WEEK) == Calendar.MONDAY) && (calMon.getTimeInMillis() <       System.currentTimeMillis()))){
    calMon.add(Calendar.WEEK_OF_MONTH, 1);
}

Basically, if day is Sunday, add a week to the calendar instance, also if the day is greater than Monday, do the same, and if the current day is the same as the day that you are setting the alarm, but the time for the alarm setup is lower than the current time, add the week.

It works fine, just to mention that, for Sunday the add(WEEK_OF_MONTH, 1) it's a MUST because according to the calendar instance, Sunday is always one week ahead. Thanks for the help, appreciate it!

Upvotes: 1

Views: 578

Answers (2)

anup
anup

Reputation: 1

I face the same problem and this is the solution i used. Do let me know if u have a better solution.

 Calendar now = Calendar.getInstance();
 Calendar calSet = Calendar.getInstance();
            calSet.set(Calendar.DAY_OF_WEEK, day);
            calSet.set(Calendar.HOUR_OF_DAY, hour);
            calSet.set(Calendar.MINUTE, min);

if (now.after(calSet)) {
                    calSet.add(Calendar.DAY_OF_MONTH, 7);
                    Log.i("location1234", "Set for Next week");
                }

                alarmManager.set(AlarmManager.RTC_WAKEUP,
                        calSet.getTimeInMillis(), pendingIntent);

Upvotes: 0

Phant&#244;maxx
Phant&#244;maxx

Reputation: 38098

Because the time is already passed, and it must inform you, even lately.
This also happens when you set the alarm to fire at a previous time in the same day.

Upvotes: 2

Related Questions