Repeat alarm everyday accurately (Alarm manager)

Hung Wei Chun picture Hung Wei Chun · Jan 17, 2015 · Viewed 10.3k times · Source

I set an alarm to repeat everyday. but it will have a few seconds or minutes error. How can I make it more accurate?

PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), notificationId, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager)getActivity().getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 12);
calendar.set(Calendar.MINUTE, 30);
long startUpTime = calendar.getTimeInMillis();
if (System.currentTimeMillis() > startUpTime) {
    startUpTime = startUpTime + 24*60*60*1000;
}
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, startUpTime,  24*60*60*1000 , pendingIntent);

Answer

Android Developer picture Android Developer · Jan 17, 2015

try Adding

calendar.set(Calendar.SECOND,00);

and changing

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

to

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
            startUpTime, AlarmManager.INTERVAL_DAY, pendingIntent);