1

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!

Vini.g.fer
  • 11,639
  • 16
  • 61
  • 90
saman0suke
  • 762
  • 1
  • 8
  • 24

2 Answers2

2

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.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • It kinda makes sense; Is there any way to make it work? I mean, the users might want to set the alarm for next monday for example. Thanks! – saman0suke Feb 13 '14 at 13:57
  • Yes, I think you have to thinker with the Calendar object a bit: set year, month and daynumber for next Monday. – Phantômaxx Feb 13 '14 at 13:59
  • Any example? I'm still learning about the Calendar object, thanks! – saman0suke Feb 13 '14 at 14:05
  • See [this](http://stackoverflow.com/a/10978620/2649012). Just set the DAY_OF_MONTH after calculating next Monday. – Phantômaxx Feb 13 '14 at 14:06
  • 1
    cal.set(Calendar.MONTH, 1); // February cal.set(Calendar.YEAR, 2014); // current year cal.set(Calendar.DAY_OF_MONTH, 17); // Next Monday – Phantômaxx Feb 13 '14 at 15:23
  • Sorry, there's no "new line" in comments. Of course, the values for the calendar must be calculated on "next Monday" – Phantômaxx Feb 13 '14 at 15:23
0

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);
anup
  • 1