T.K
T.K

Reputation: 67

android notification weekly

I try to set up an Android App. This have to notificate me every week Friday at 15.00 / 3.00 pm o'clock. To set up the notification every day at this time was no problem, but even not on the specific day and time.

I hope you find my mistake and can give me some hints. Here my code:

private void alarmMethod(){
    Intent myIntent = new Intent(this , NotifyService.class);
    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
    pendingIntent = PendingIntent.getService(this, 0, myIntent, 0);

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.HOUR, 3);
    calendar.set(Calendar.AM_PM, Calendar.PM);
    calendar.add(Calendar.DAY_OF_MONTH, 1);
    calendar.set(Calendar.DAY_OF_WEEK, 6);

    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * 60 * 60 * 24, pendingIntent);

Thanks in regards

Tim

Upvotes: 1

Views: 1249

Answers (1)

kb0
kb0

Reputation: 1153

For repeating alarm (setRepeating, setInexactRepeating) you should setup time in the future (if the trigger time you specify is in the past, the alarm triggers immediately). So if you need alarm at every Friday first of all you shoud calculate next friday:

private void alarmMethod() {
    Intent myIntent = new Intent(this , NotifyService.class);
    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
    pendingIntent = PendingIntent.getService(this, 0, myIntent, 0);

    // get current time
    Calendar currentTime = Calendar.getInstance();  

    // setup time for alarm
    Calendar alarmTime = Calendar.getInstance();

    // set time-part of alarm
    alarmTime.set(Calendar.SECOND, 0);
    alarmTime.set(Calendar.MINUTE, 0);
    alarmTime.set(Calendar.HOUR, 3);    
    alarmTime.set(Calendar.AM_PM, Calendar.PM);
    alarmTime.set(Calendar.DAY_OF_WEEK, Calendar.Friday);

    // check if it in the future
    if (currentTime.getTimeInMillis() <  alarmTime.getTimeInMillis()) {
        // nothing to do - time of alarm in the future
    } else {            
        int dayDiffBetweenClosestFriday = (7 + alarmaTime.get(Calendar.DAY_OF_WEEK) - calendar.get(Calendar.DAY_OF_WEEK)) % 7;

        if (dayDiffBetweenClosestFriday == 0) {
            // Today is Friday, but current time after 3pm, so schedule for the next Friday
            dayDiffBetweenClosestFriday = 7;
        }

        alarmTime.add(Calendar.DAY_OF_MONTH, dayDiffBetweenClosestFriday);
    }

    // calculate interval (7 days) in ms
    int interval = 1000 * 60 * 60 * 24 * 7;

    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, alarmTime.getTimeInMillis(), interval, pendingIntent);
}

Also you should automatically restart a repeating alarm if the user reboots the device (by default, all alarms are canceled when a device shuts down):

  • Set the RECEIVE_BOOT_COMPLETED permission in your application's manifest
  • Implement a BroadcastReceiver to receive the broadcast android.intent.action.BOOT_COMPLETED
  • Add the receiver to your app's manifest file with an intent filter that filters on the ACTION_BOOT_COMPLETED action

Upvotes: 1

Related Questions