Search code examples
androidandroid-syncadapter

Android: Design consideration for periodic syncing with Sync Adapter


I am using sync adapter in my app. The app should perform a sync at every 3 hours. This can be done with ContentResolver.addPeriodicSync. But, prior to that, with every sync request I need to send the access token of the user. The token's expiry time is of 2 hours. So, before any sync request it needs to have a valid access token. If the access token have expired, a fresh token needs to be updated for the user.

One solution, I came up is to set a repeating alarm with using AlarmManager. When the alarm triggers, an IntentService will startup. and refreshes the access token for user. After new token is obtained, the sync request will be sent to the SyncAdapter, using ContentResolver.requestSync. I am not sure if this is a good way to do or any other efficient approach is available.


Solution

  • You should be able to refresh your token in OnPerformSync using the AccountManager.

    Checkout this demo application that uses a custom sync adapter to sync Google Task data tied to a Google Account using a custom content provider.

    https://github.com/sschendel/SyncManagerAndroid-DemoGoogleTasks

    The demo app uses GoogleAuthUtil.getTokenWithNotification in OnPerformSync, but (in theory) you should be able to do the same thing using AccountManager.getAuthToken on the Account passed into OnPerformSync.

    From Android docs AccountManager.getAuthToken:

    Gets an auth token of the specified type for a particular account, optionally raising a notification if the user must enter credentials. This method is intended for background tasks and services where the user should not be immediately interrupted with a password prompt.

    If a previously generated auth token is cached for this account and type, then it is returned. Otherwise, if a saved password is available, it is sent to the server to generate a new auth token. Otherwise, an Intent is returned which, when started, will prompt the user for a password. If the notifyAuthFailure parameter is set, a status bar notification is also created with the same Intent, alerting the user that they need to enter a password at some point.