Android SyncAdapter: retry

2019-08-25 02:18发布

问题:

I'm using an AbstractThreadedSyncAdapter to synchronize some data with my server. I'm using SyncResult to indicate if there's been an error while performing the synchronization:

syncResult.stats.numParseExceptions++;

I initialize the SyncAdapter like this:

Bundle params = new Bundle();
params.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, false);
params.putBoolean(ContentResolver.SYNC_EXTRAS_DO_NOT_RETRY, false);
params.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, false);
ContentResolver.addPeriodicSync(account, CONTENT, params, 3600);
ContentResolver.setSyncAutomatically(account, CONTENT, true);
ContentResolver.requestSync(account, CONTENT, new Bundle());

If the synchronization fails I want it to retry after 5 minutes (periodic sync time is 1 hour). I thought that I had to use syncResult.delayUntil, but the synchronization isn't retried.

How can I do it?

回答1:

Documentation say's: (http://developer.android.com/reference/android/content/ContentResolver.html#addPeriodicSync(android.accounts.Account, java.lang.String, android.os.Bundle, long))

Periodic syncs are not allowed to have any of SYNC_EXTRAS_DO_NOT_RETRY, SYNC_EXTRAS_IGNORE_BACKOFF, SYNC_EXTRAS_IGNORE_SETTINGS, SYNC_EXTRAS_INITIALIZE, SYNC_EXTRAS_FORCE, SYNC_EXTRAS_EXPEDITED, SYNC_EXTRAS_MANUAL set to true. If any are supplied then an IllegalArgumentException will be thrown.

Maybe that's the problem. Try remove this params - may work. Write if it helps.

Or You forgot permissions:

<uses-permission android:name="android.permission.READ_SYNC_STATS" />
<uses-permission android:name="android.permission.READ_SYNC_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" />