Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Has anyone implemented (or got more info) on Android SyncObserver

I am developing an app in Android that performs a background sync with a server (using SyncAdapter and authentication etc).

When the foreground app (with UI) is started, there maybe a background sync in progress, or optionally it may start one via a UI button.

I would like a way to "plug into" an on-going background sync (whether started by the system, or the periodic sync setting or the UI) and show it's progress in the foreground activity.

The ContentResolver documentation (http://developer.android.com/reference/android/content/ContentResolver.html) mentions a mysterious "SyncObserver" that has no link to javadoc and is not documented (that I can find).

There are some other pages around that mention it (http://www.chinaup.org/docs/migrating/m5-0.9/changes/android.content.ContentResolver.html) but I can't find out more about it.

Has anyone implemented this beast?

If not, does anyone have example code or recommendations on tracking the progress of a background sync in a foreground Activity?

like image 277
Andrew Mackenzie Avatar asked Mar 31 '11 19:03

Andrew Mackenzie


1 Answers

Thanks for the answer.

Due to the async nature of the background sync your App (activity) could be started with a background sync already underway, which you detect with the status stored in a preference.

What I have done is to implement a SyncObserver class that implements the SyncStatusObserver interface and create/destroy on suspend/resume.

syncObserverHandle = ContentResolver.addStatusChangeListener( ContentResolver.SYNC_OBSERVER_TYPE_ACTIVE, new SyncObserver() );

This gets informed of any event related to sync (pending, started, etc) and I also check for status using

ContentResolver.isSyncActive();

The Android APIs for this are pretty basic, and you have to respect rules about what is done on the UI thread and what is not, but if anyone want to see my implementation just post a question and point me to it and I will answer with pleasure.

like image 130
Andrew Mackenzie Avatar answered Oct 31 '22 15:10

Andrew Mackenzie