Search code examples
javaandroidnotificationsalarmmanager

setRepeating() doesn't repeat properly


I'm using the AlarmManager to fire a notification daily in specific time which is selected via time picker. The notification fired correctly in the same day but doesn't repeat correctly every day!!!

This is the method for setting the notification using setRepeating():

public void witer_reminder(View view)
{
    am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

    Intent intent = new Intent(this, Witer_Notification.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
                                    intent, PendingIntent.FLAG_CANCEL_CURRENT);

    Calendar cal = Calendar.getInstance();
    Calendar calSet = (Calendar) cal.clone();

    calSet.set(Calendar.HOUR_OF_DAY, picker.getCurrentHour());
    calSet.set(Calendar.MINUTE, picker.getCurrentMinute());
    calSet.set(Calendar.SECOND, 0);
    calSet.set(Calendar.MILLISECOND, 0);

    if(calSet.compareTo(cal) <= 0)
    {
        // Today Set time passed, count to tomorrow
        calSet.add(Calendar.DATE, 1);
    }

    am.setRepeating(AlarmManager.RTC_WAKEUP, calSet.getTimeInMillis(),
                                            24 * 60 * 60 * 1000, pendingIntent);
}

and this is the BroadcastReciver class:

public class Witer_Notification extends BroadcastReceiver
{
    NotificationManager nm;

    @Override
    public void onReceive(Context context, Intent intent)
    {
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
                                    new Intent(context, MainActivity.class), 0);

        PendingIntent actiontIntent = PendingIntent.getActivity(context, 0,
                                    new Intent(context, Suggestion.class), 0);

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(context)
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setContentTitle("")
                    .setStyle(new NotificationCompat.BigTextStyle().bigText(""));

        mBuilder.setContentIntent(contentIntent);
        mBuilder.setDefaults(Notification.DEFAULT_SOUND);
        // mBuilder.setStyle(new NotificationCompat.InboxStyle());

        NotificationManager mNotificationManager = (NotificationManager)
                        context.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(1, mBuilder.build());
    }
}

BTW, the application target is SDK 19.


Solution

  • I found that the setExact() replacing to set()

    Correct.

    it's not applicable for intervalAtMilis as a parameter

    Not directly. But, when you get control in your BroadcastReceiver from the setExact() event, call setExact() again to schedule the next event.

    I didn't find anything for setRepeating()

    There is no simple solution, because Google is trying to point out to you that this is bad for the battery. Using setExact() as described above is your only option for exact repeating.