Force your SyncAdapter to sync

Force your SyncAdapter to sync

NOTE: This is not a tutorial of what is or how to create your own SyncAdapter. If you're looking for this, the Android developer doc is your friend!

I've been recently playing with SyncAdapter and I found myself in the situation where I have to trigger a sync on user demand. This seems to be pretty straight forward, ContentResolver has an explicit method that does exactly what I want: ContentResolver.requestSync(Account, String, Bundle), [here](, java.lang.String, android.os.Bundle) you can find the full Javadoc of the method.

This worked well until I randomly disabled synchronization for the entire system. Of course, with sync disabled, my SyncAdapter wouldn't refresh.


The solution is to pass the right extras to the bundle in the requestSync method. In order to force a sync and bypass user preferences (like disabling sync) you shuold use the following bundle and pass it to the requestSync method:

Bundle bundle = new Bundle();
bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
bundle.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);    
ContentResolver.requestSync(account, AUTHORITY, bundle);

SYNC_EXTRAS_MANUAL - forces a manual sync ignoring any settings

SYNC_EXTRAS_EXPEDITED - triggers the sync to start immediately

However, keep in mind that syncing on demand and requesting an immediate sync is a rather inefficient use of SyncAdapter because it bypasses all the network and power use optimisations. This solution should be used with extra care and only in situations when you are sure you want to perform an immediate on demand sync.

Show Comments