Kmel
Kmel

Reputation: 285

Notification and AlarmManager

I know that this question has been asked several times but I really don't understand why my code doesn't work. I based upon a lot of examples (from stackoverflow answers) to write my code and I need help to understand where is the problem.

The context

The user can, trough preferences, choose a day to be notify. I get a number from 2 (monday) 3...4...5... to 1 (sunday).

public Calendar getCalendar(){
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.DAY_OF_WEEK,getDayForNotification());
    calendar.set(Calendar.HOUR_OF_DAY, 8);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);
    calendar.set(Calendar.AM_PM,Calendar.AM);

    return calendar;
}

Then I want to repeat the alarm once a week on the day that the user has chosen.

public void setNotificationDate() {

    alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(this, myAlarmReceiver.class);
    alarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0);

    DateNotificationPreference dateNotificationPreference = new DateNotificationPreference(this);

    alarmMgr.setRepeating(AlarmManager.RTC, dateNotificationPreference.getCalendar().getTimeInMillis(),AlarmManager.INTERVAL_DAY * 7, alarmIntent);

}

My broadcast receiver

public class myAlarmReceiver extends BroadcastReceiver
{

  @Override
  public void onReceive(Context context, Intent intent)
   {
    Intent service1 = new Intent(context, AlarmService.class);
    context.startService(service1);

   }
}

In my service, I have juste one method :

public void createNotification(){
    final NotificationManager mNotification = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    final Intent launchNotifiactionIntent = new Intent(this, mySilhouette.class);
    final PendingIntent pendingIntent = PendingIntent.getActivity(this,
            0, launchNotifiactionIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    Notification.Builder builder = new Notification.Builder(this)
            .setWhen(System.currentTimeMillis())
            .setSmallIcon(R.drawable.icon_notification)
            .setContentTitle("my notification")
            .setContentText("my first notification")
            .setContentIntent(pendingIntent);

    mNotification.notify(0, builder.build());
}

When I choose the current day, the notification appears at the good time, but when I relaunch the app at 8:05 am for example, I have again a notification. After that, if I choose another day and go back to the activity, I have again a notification...There is something strange and I don't know what can be the problem. I need your help :)

Thanks a lot !

Kmel

Upvotes: 2

Views: 665

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007534

set() on Calendar does just that: it sets the value. As a result, the date may be in the past, given your existing code.

For example, where I am, right now it is Friday. So, the object returned by Calendar.getInstance() is set for now, which means its day-of-week will be Friday. If you call set() to change that to Tuesday, now you may be setting a date in the past. Similarly, even if you were to set the day-of-week to Friday, it is after 8am where I am, and therefore you would be creating a date that is in the past.

You need to check your calculated Calendar object and see if it is in the past. If it is, add 7 days to move it to the next week's time for the alarm to go off.

Also, note that an RTC alarm will not wake up the device out of sleep mode. That may be fine for your purposes, but I just wanted to make sure that you were aware of this.

Upvotes: 3

Related Questions