0

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.

Nitish
  • 3,097
  • 13
  • 45
  • 80
  • How about you let the user request the access token manually on sync? – LightYearsBehind Oct 27 '14 at 04:45
  • The app will sync data automatically in background at every 3 hours. How to check if user is using the app, when periodic sync will trigger. Apps like gmail, facebook or twitter etc., never ask user to enter their data after again and again in sync. What methodology do they follow to do this? – Nitish Oct 27 '14 at 04:54
  • Most access tokens come with a refresh token which allows you to get a fresh token. – ianhanniballake Oct 27 '14 at 05:05
  • @ianhanniballake Yes, I have that. Using that only, I am refreshing the access token. My problem is when sync runs periodically, it is not known if access token is valid. So I have posted my solution that I came up with, to deal with this issue in the second half of my post. I am not sure if this is a proper way to do this or there is some more efficient way. – Nitish Oct 27 '14 at 05:14
  • Sorry that my statement "user request the access token **manually**" may have confused you. What I mean is that you don't have to create an alarm to refresh your token, just do it when you are syncing the data using sync adapter. – LightYearsBehind Oct 27 '14 at 05:24
  • @haike00 you mean in `onPerformSync` of sync adapter. – Nitish Oct 27 '14 at 05:28
  • Yes, that's what i mean. – LightYearsBehind Oct 27 '14 at 05:30
  • @haike00 The app has multiple accounts. Sorry, I forgot to update that. For each account, I have to send a sync request. – Nitish Oct 27 '14 at 05:30
  • So I guess removing alarm(s) is a better deal? – LightYearsBehind Oct 27 '14 at 05:36
  • @haike00 got my solution. Will post it soon. – Nitish Oct 27 '14 at 05:50

2 Answers2

2

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.

Community
  • 1
  • 1
Sean
  • 7,562
  • 10
  • 27
  • 29
0

I suggest you to only try to retrieve a new token if you found out that the token you have can not be used anymore. This approach should be preferred as then you are not hardcoding the validity period of the token in your application.

It also handle the cases where the server side might invalidate everyone's token from time to time for whatever reasons before the validity period is up.

Phuah Yee Keat
  • 1,572
  • 1
  • 17
  • 17