How to notify calling Activity when SyncAdapter has finished?

In my Android project I use a custom SyncAdapter which downloads data and stores it in local SQLite database.

public class CustomSyncAdapter extends AbstractThreadedSyncAdapter {

    public CustomSyncAdapter(Context context, boolean autoInitialize) {
        super(context, autoInitialize);
    }

    @Override
    public void onPerformSync(Account account,
                              Bundle extras,
                              String authority,
                              ContentProviderClient provider,
                              SyncResult syncResult) {

        // 1) Download data via AsyncTask
        // 2) Store data via ContentProvider
    }
}

I schedule sychronization from the main Activity using the ContentResolver such as …

  • Sending JSON as extra data in an android broadcast via ADB gets incorrectly formatted
  • uid … does not have android.permission.UPDATE_APP_OPS_STATS
  • BPG image format on Android
  • Navigation Drawer Below Toolbar
  • How to disable copy/paste from/to EditText
  • First time user guide-through Android library
  • ContentResolver.requestSync(account, Authentication.AUTHORITY, bundle);
    

    Is there a common way the calling Activity is notified when the synchronization has finished?

    Related posts:

    Android: How to set the colour of a Toast's text
    Combobox in Android
    How to get the homescreen behaviour of drag to bin, in my own application?
    Android browser GREEN border on click
    LibGDX and ScrollPane with multiple widgets
    How to make json api
  • Use C++ with Android ndk/jni
  • Spacing between listView Items Android
  • android Maps API v2 with custom markers
  • Best way to update TextView, every minute, on the minute
  • How to convert string to long
  • need help changing my package name in eclipse
  • 2 Solutions collect form web for “How to notify calling Activity when SyncAdapter has finished?”

    You could do this with with ContentResolver.notifyChange().

    So in your SyncAdapter you would at something like this:

    ...
    @Override
    public void onPerformSync(Account account,
                              Bundle extras,
                              String authority,
                              ContentProviderClient provider,
                              SyncResult syncResult) {
    
        // 1) Download data via AsyncTask
        // 2) Store data via ContentProvider
    
        getContext().getContentResolver().notifyChange(<your_content_uri>, null, false);
    }
    ...
    

    In the Activity you then use ContentResolver.registerContentObserver():

    public class MyActivity extends Activity  {
    
        private ContentObserver mObserver;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            ...
            mObserver = new ContentObserver(new Handler(Looper.getMainLooper())) {
                public void onChange(boolean selfChange) {
                    // Do something.
                }
            };
            getContentResolver().registerContentObserver(<your_content_uri>, mObserver);
        }
    
        @Override
        public void onDestroy() {
            getContentResolver().unregisterContentObserver(mObserver);
        }
    }
    

    In your activity you can add:

    @Override
    public void onResume() {
        super.onResume();
        mSyncMonitor = ContentResolver.addStatusChangeListener(
                ContentResolver.SYNC_OBSERVER_TYPE_ACTIVE
                        | ContentResolver.SYNC_OBSERVER_TYPE_PENDING,
                this
        );
    }
    
    @Override
    public void onPause() {
        ContentResolver.removeStatusChangeListener(mSyncMonitor);
        super.onPause();
    }
    
    @Override
    public final void onStatusChanged(int which) {
        // TODO update activity data
    }
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.