Search code examples
androidsynchronizationandroid-syncadapter

Retry mechanism in Android SyncAdapter


I am Starting manual sync using below method

public void startSync(){
        Bundle syncBundle = new Bundle();
        syncBundle.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
        syncBundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
        syncBundle.putInt(Utils.SYNC_TYPE, USER_DATA);

        ContentResolver.requestSync(acts[0], CONTENT_AUTHORITY, syncBundle);

  }

let say sync fails due some reason :

1. Network Failure -- How to start sync again when network comes back

2. Bad response from server -- best way to retry

3. device shut down -- best way to restart sync when device boots up

Is there any mechanism in sync framework of android which handles this.**


Solution

  • For #1 and #2 you can report a "soft error" and then it should automatically retry (with exponential backoff):

    syncResult.stats.numIoExceptions++;
    

    For #4 you could create a broadcast receiver for android.intent.action.BOOT_COMPLETED and then trigger your manual sync from there.