10

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);
Tur1ng
  • 745
  • 8
  • 22
Hung Wei Chun
  • 145
  • 1
  • 1
  • 9

3 Answers3

11

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);
Android Developer
  • 9,157
  • 18
  • 82
  • 139
  • From AlarmManager.java: ...as of API 19, all repeating alarms are inexact. If your application needs precise delivery times then it must use one-time exact alarms, rescheduling each time as described above. – einUsername Dec 15 '19 at 12:48
5

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);
Tur1ng
  • 745
  • 8
  • 22
priti
  • 892
  • 9
  • 9
0
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);
Makvin
  • 3,475
  • 27
  • 26