Set a repeating alarm at a specific time every day

I am trying to use the alarm manager to trigger an alarm at a specific time every day. I am using this code

Intent intent = new Intent(AlarmSettings.this, AlarmService.class); intent.putExtra("i", i); PendingIntent mAlarmSender = PendingIntent.getService(AlarmSettings.this, Id, intent, 0); AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE); am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),Calendar.getInstance().getTimeInMillis()+(24*60*60*1000), mAlarmSender);} 

the problem was if the cal.getTimeInMillis () value was started immediately in the past, I donโ€™t know why, and when the cal.getTimeInMillis () value will work correctly in the future.

I need it to start at a specific time every day.

+4
source share
3 answers

It looks like your challenge

 setRepeating(int type, long triggerAtTime, long interval, PendingIntent operation) 

Try setting the correct triggerAtTime (in the future) - for example

 Calendar.getInstance().getTimeInMillis()+(24*60*60*1000) 

The third parameter (interval), obviously, should be your interval, for example

 24*60*60*1000 
+2
source
 // every day at 9 am Calendar calendar = Calendar.getInstance(); // if it after or equal 9 am schedule for next day if (Calendar.getInstance().get(Calendar.HOUR_OF_DAY) >= 9) { calendar.add(Calendar.DAY_OF_YEAR, 1); // add, not set! } calendar.set(Calendar.HOUR_OF_DAY, 9); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); alarmManager.setInexactRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi); // alarmManager.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), // AlarmManager.INTERVAL_DAY, pi); 
+7
source

It helped me a lot, I had a case where I also used minutes and came up with the following small correction:

 /* Create calendar and set desired time before this*/ // Compare the current time milliseconds with the desired calendar time milliseconds if (java.util.Calendar.getInstance().getTimeInMillis() >= calendar.getTimeInMillis() ) { calendar.add(Calendar.DAY_OF_YEAR, 1); } 
0
source

All Articles