Android SyncAdapter Callback

I implemented SyncAdapter, AccountManager and private ContentProvider according to the SimpleSyncAdapter sample project in the SDK. Everything works well.

Now I want to show a message to the user when new lines were downloaded from a remote server with a specific set of flags. I need a callback from the SyncAdapter when the synchronization is complete, so I can execute the request and display a message from the activity. I saw several questions about StackOverflow that discussed this, but no one answered a good answer.

How can I listen to the progress from Android SyncAdapter? says SyncStatusObserver is useless. The mobibob user suggests using ResultReceiver to respond to the user interface from the synchronization stream.

How do I know when synchronization is complete? suggests using Intent in SyncService.

How to signal synchronization with Android SyncManager? suggests using SyncResult. The sample code associated with maxpower47 uses the SyncResult class to report exceptions, but not to actually report the completion of synchronization.

I just don’t know what is the best option, and I have not seen any examples of projects that use any of these solutions.

+8
android synchronization callback android-syncadapter
source share
3 answers

I know this is an old question, but I myself asked for it. What I found as a good solution, especially because I deal with local data like you, is to use the following method from ContentResolver:

registerContentObserver(Uri uri, boolean notifyForDescendents, ContentObserver observer) 

This register registers an observer class that receives a callback when the data identified by this content URI changes. But this can only happen if your ContentProvider sends a notification. For example, if you want to receive a notification on ContentObserver above for all updates made to your database through ContentProvider, your ContentProvider should implement an update similar to this:

 @Override public int update(Uri uri, ContentValues contentValues, String s, String[] strings) { // code goes here this.getContext().getContentResolver().notifyChange(uri, null); return 0; } 

Using notifyForDescendents when you do registerContentObserver can be very useful.

+1
source share

This is an old question, but in recent days I have done some research, and there are not many examples where the syncAdapter processes network requests and notifies the user interface.

You must first use Loaders with contentProvider to make your life easier. You no longer need to register to allow content, and the downloader will do it for you. This means that your user interface is notified of everything that is included in your content provider.

What if nothing changes? everything was up to date, or you had a network error.

  • You can listen to the status of your sync adapter as the Google I / O application searches mSyncStatusObserver in BaseActivity
  • I looked at the default email application for Android and used Singleton with callBacks.
  • You can BroadcastIntents or use eventBus (like Otto's square) to notify your user interface of any behavior.

I like the latter better because it gives you more details about the events that occur in the syncAdapter.

+1
source share

We faced a similar situation and wrote a static Listener interface for the SyncAdapter. The listener is active and performs the necessary actions when the data is available (update the interface). This also works when the synchronization system is called by the system during auto-synchronization, where this listener will be empty and the synchronization process will have in mind his own business.

 class SyncAdapter extends AbstractThreadedSyncAdapter { protected static Listener uiListener = null; public interface Listener { public void onSync(); } public static void setListener(Listener l) { uiListener = l; } public static void clearListener() { uiListener = null; } protected void broadcastSync() { if (uiListener != null) uiListener.onSync(); } public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult) { // call broadcastSync(); } 

Then, in Management, we implement the SyncAdapter.Listener interface.

-2
source share

All Articles