Using AlarmManager in Android to run daily maintenance?

I need to do service every night at midnight. I would like to use AlarmManager for this.

Can you give me some tips on how to make it work correctly?

alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, System.currentTimeMillis(), AlarmManager.INTERNAL_DAY, serviceIntent);

Perhaps I need to use the Calendar object to indicate the time? Thanks for any help!

+3
source share
2 answers

setInexactRepeating(), as the name suggests, is inaccurate. Keep in mind that it can start somewhat before or after midnight.

, , Calendar. getDate(), Date getTime() Date, setInexactRepeating().

+6

, , "setInexactRepeating". : alarmManager.setRepeating(RTC_WAKEUP, ...), Calendar , .

, , , , RTC_WAKEUP RTC. , , RTC_WAKEUP, , , , .

0

Source: https://habr.com/ru/post/1747773/


All Articles