samedi 18 juin 2016

Setting Alarm to repeat weekdays at different times

So I'm creating an Alarm app for android but I am having some trouble setting the alarm. If Today is Saturday and I set an alarm to ring at 17:15 today and tomorrow, it should ring two times, but it doesn't ring at all. Also If no day is selected, the alarm should ring the next time the clock matches it, then turn off. I have searched for this question here and found many solutions, but none of them seem to work for me. Here is the AlarmReceiver code.

public class AlarmService extends IntentService {

public static final String CREATE = "CREATE";
public static final String CANCEL = "CANCEL";
private IntentFilter matcher;

public AlarmService() {
    super("AlarmClockService");
    matcher = new IntentFilter();
    matcher.addAction(CREATE);
    matcher.addAction(CANCEL);
}

@Override
public void onHandleIntent(Intent intent) {
    Log.d("AlarmService", "onHandleIntent ");
    String action = intent.getAction();
    int[] repeat = intent.getIntArrayExtra("repeat");//This array contains the day number of the week. repeat containing 1,2 means monday and tuesday.
    String notificationId = intent.getStringExtra("notificationId");
    Date time = (Date) intent.getSerializableExtra("time"); //This Date object contains the time that the alarm should ring at.

    if (matcher.matchAction(action)) {
        execute(action, notificationId, time.getTime(), repeat);
    }
}

private void execute(String action, String notificationId, long time, int[] repeat) {
    AlarmManager am = (AlarmManager) MainActivity.CONTEXT.getSystemService(Context.ALARM_SERVICE);
    Intent i = new Intent(MainActivity.CONTEXT, AlarmReceiver.class);
    PendingIntent pi = PendingIntent.getBroadcast(MainActivity.CONTEXT, 0, i,
            PendingIntent.FLAG_UPDATE_CURRENT);

    if (CREATE.equals(action)) {
        if (repeat.length == 0) {
            am.set(AlarmManager.RTC_WAKEUP, time, pi);
            Log.d("Alarm Service", " No day selected, setting ms as " + time);
        } else {
            for (int index = 0; index < repeat.length; index++) {
                switch (repeat[index]) {
                    case 0:
                        setAlarmSpecificDay(0, am, pi);

                    case 1:
                        setAlarmSpecificDay(1, am, pi);
                        break;
                    case 2:
                        setAlarmSpecificDay(2, am, pi);
                        break;
                    case 3:
                        setAlarmSpecificDay(3, am, pi);
                        break;
                    case 4:
                        setAlarmSpecificDay(4, am, pi);
                        break;
                    case 5:
                        setAlarmSpecificDay(5, am, pi);
                        break;
                    case 6:
                        setAlarmSpecificDay(6, am, pi);
                        break;
                    case 7:
                        setAlarmSpecificDay(7, am, pi);
                        break;
                }
            }
        }

    } else if (CANCEL.equals(action)) {
        Log.d("Alarm Service", " Alarm canceld.");
        am.cancel(pi);
    }
}

public void setAlarmSpecificDay(int dayOfWeek, AlarmManager am, PendingIntent pi) {
    Calendar cal1 = Calendar.getInstance();
    int today = cal1.get(Calendar.DAY_OF_WEEK);
    int numberOfDaysToAdd = dayOfWeek - today;
    if (dayOfWeek < today) {
        // Desired day is earlier in the week than today, add 7 days to
        //  ensure it is in the future
        numberOfDaysToAdd += 7;
    }
    cal1.add(Calendar.DAY_OF_WEEK, numberOfDaysToAdd + 1);
    Long alarmTime = cal1.getTimeInMillis();
    am.setRepeating(AlarmManager.RTC_WAKEUP, alarmTime, 7 * 24 * 60 * 60 * 1000, pi);
    Log.d("Alarm Service", "Alarm set for day " + dayOfWeek + " at ms: " + alarmTime);
}

If I use this code and set the alarm to ring on friday, saturday and sunday, the last log will say:

And then the alarm rings about 1 minute after. It must have to do with the way I'm getting the time from Calendar but I just can't figure it out. Been at this and googling for hours.

 Alarm set for day 5 at ms: 1466789400000
Alarm set for day 6 at ms: 1466875800000
Alarm set for day 7 at ms: 1466357400000

This is the MS the alarm should fire at when I sat the clock to 19:30 FSS and now the clock is 19:38 and if I convert the MS to date at a website the numbers are correct!

1466789400000 = Fri Jun 24 2016 19:30:00 GMT+0200
1466875800000 = Sat Jun 25 2016 19:30:00 GMT+0200
1466357400000 = Sun Jun 19 2016 19:30:00 GMT+0200

Aucun commentaire:

Enregistrer un commentaire