Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know when sync is finished?

I have implemented a sync adapter and I want to get a callback when it finishes in my activity. I have tried using ContentResolver.addStatusChangeListener, but I am only getting callbacks when the sync is pending / active. Here's some relevant code from my activity:

@Override protected void onResume() {     super.onResume();     final int mask = ContentResolver.SYNC_OBSERVER_TYPE_ACTIVE | ContentResolver.SYNC_OBSERVER_TYPE_PENDING;     syncObserverHandle = ContentResolver.addStatusChangeListener(mask, syncStatusObserver); }  @Override protected void onPause() {     super.onPause();     if (syncObserverHandle != null) {         ContentResolver.removeStatusChangeListener(syncObserverHandle);         syncObserverHandle = null;     } }  private SyncStatusObserver syncStatusObserver = new SyncStatusObserver() {      @Override     public void onStatusChanged(int which) {         AccountManager am = AccountManager.get(TodosActivity.this);         Account a = am.getAccountsByType(Const.ACCOUNT_TYPE)[0];          Log.d(Const.TAG, "Sync status changed: " + which);          if (!ContentResolver.isSyncActive(a, DataProvider.AUTHORITY) &&                 !ContentResolver.isSyncPending(a, DataProvider.AUTHORITY)) {             Log.d(Const.TAG, "Sync finished, should refresh nao!!");         }     } }; 

However, the if in the onStatusChanged method is never valid. I have taken this example from the JumpNote demo where it works probably because it is also called manually in onResume(), so it's probably never called by the system when the sync is finished. Or is it, and I'm doing something wrong? Here's what I get in logcat:

D/MYAPP (10903): Sync status changed: 2 D/MYAPP (10903): Sync status changed: 2 D/MYAPP (10903): Sync status changed: 4 D/MYAPP (10981): --> DataSyncAdapter.onPerformSync() D/MYAPP (10981): <-- DataSyncAdapter.onPerformSync() D/MYAPP (10903): Sync status changed: 4 

So, it seems that I could rely on the second SYNC_OBSERVER_TYPE_ACTIVE (4) status change to refresh my data, but that seems really ugly. Any ideas?

like image 859
Felix Avatar asked Jul 08 '11 09:07

Felix


People also ask

How can I tell if my computer is syncing?

Sync displays icon overlays on files, folders and the Sync icon on your system tray (Windows) or menu bar (Mac). You'll know when syncing is taking place when the Sync icon on your system tray or menu bar spins.

How long does it take for Google Sync to update?

Give it time But it can take up to more than 24 hours before Google Calendar starts to sync up your data again.

How long does a Google sync take?

Usually it takes no longer than 5 minutes. If it is your first synchronization, the process can take from few minutes to one hour, or even longer. You can cancel the synchronization at any time.


1 Answers

One solution that I have found is to ditch the ContentResolver completely, and implement my own broadcast. Basically, add this in the sync adapter, at the end of onPerformSync:

Intent i = new Intent(SYNC_FINISHED); sendBroadcast(i); 

And this in the activity:

@Override protected void onResume() {     super.onResume();     registerReceiver(syncFinishedReceiver, new IntentFilter(DataSyncService.SYNC_FINISHED)); }  @Override protected void onPause() {     super.onPause();     unregisterReceiver(syncFinishedReceiver); }  private BroadcastReceiver syncFinishedReceiver = new BroadcastReceiver() {      @Override     public void onReceive(Context context, Intent intent) {         Log.d(Const.TAG, "Sync finished, should refresh nao!!");     } }; 

This seems to work just fine, however I was hoping to find something in the SDK that directly notify me when a sync is finished.

like image 164
Felix Avatar answered Oct 06 '22 01:10

Felix