Hung Wei Chun
Hung Wei Chun

Reputation: 145

Repeat alarm everyday accurately (Alarm manager)

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);

Upvotes: 10

Views: 14002

Answers (3)

priti
priti

Reputation: 912

1) get time form timepicker basis

calNow = Calendar.getInstance();
calSet = (Calendar) calNow.clone();
calSet.set(Calendar.HOUR_OF_DAY, hourOfDay);
calSet.set(Calendar.MINUTE, minute);
calSet.set(Calendar.SECOND, 0);
calSet.set(Calendar.MILLISECOND, 0);

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

2) Set alarm for Daily basis

Intent intent = new Intent(AddAlarmNewActivity.this, OnAlarmReceive.class);
intent.putExtra("alarmTitle", mTitle.getText().toString());
intent.putExtra("alarmId", insertedId + "");
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), 
                                                         (int)insertedId,
                                                         intent,
                                                         PendingIntent.FLAG_UPDATE_CURRENT);

AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, 
                          targetCal.getTimeInMillis(), 
                          24*60*60*1000, 
                          pendingIntent);

Upvotes: 5

Makvin
Makvin

Reputation: 3629

Intent myIntent = new Intent(ThisActivity.this , NotifyService.class);     
AlarmManager alarmManager = (AlarmManager) Context.getSystemService(Context.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getService(ThisActivity.this, 0, myIntent, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 12);
calendar.set(Calendar.MINUTE, 00);
calendar.set(Calendar.SECOND, 00);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000, pendingIntent);

Upvotes: 0

Android Developer
Android Developer

Reputation: 9643

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);

Upvotes: 11

Related Questions