Reputation: 10699
I have a SyncAdapter
that needs to be synced every midnight. I'm executing periodic syncs using ContentResolver.addPeriodicSync
, but I don't know how to trigger it at midnight.
I've thought on a couple of options but none of them seem quite right:
AlarmManager
) that triggers at midnight, fire a Broadcast
with a sync intent and register/unregister receivers in every Activity
.Service
. The service is fired on start up and runs indefinitely.Any ideas on how to do this properly?
Upvotes: 3
Views: 1511
Reputation: 24005
From the Android Sync Adapter guide:
Notice that addPeriodicSync() doesn't run the sync adapter at a particular time of day. To run your sync adapter at roughly the same time every day, use a repeating alarm as a trigger. Repeating alarms are described in more detail in the reference documentation for AlarmManager. If you use the method setInexactRepeating() to set time-of-day triggers that have some variation, you should still randomize the start time to ensure that sync adapter runs from different devices are staggered.
jlhonora your comment is correct:
The spike can be avoided using a random start for the alarm and triggering it with AlarmManager.setInexactRepeating
So you are on the right track with the AlarmManager, but should take into account the random start.
See the Running a Sync Adapter periodically for the full guide I made reference to.
Upvotes: 2