Has anyone implemented (or received more information) on Android SyncObserver

I am developing an Android application that performs background synchronization with the server (using SyncAdapter and authentication, etc.).

When the foreground application (with the user interface) is running, it is possible that the background is synchronizing, or maybe it can be launched through the user interface button.

I need a way to “connect” the current background synchronization (regardless of whether it is started by the system or periodic synchronization settings or the user interface) and show that it works in the foreground.

The ContentResolver documentation (http://developer.android.com/reference/android/content/ContentResolver.html) mentions the cryptic "SyncObserver" which does not have a link to javadoc and is not documented (which I can find).

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

Has anyone realized this beast?

If not, does anyone have some sample code or recommendations for tracking background sync progress in the foreground?

+5
source share
2 answers

, 1) SyncAdapter 2) SharedPreferences .

SyncAdapter - :

public static final String START_SYNC = "com.whatever.sync.start";
public static final String STOP_SYNC = "com.whatever.sync.stop";
public static final String SYNC_PROGRESS = "syncProgress";


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

        // Add an integer to the shared settings to indicate the status
        SharedPreferences settings = mContext.getSharedPreferences(Constants.PREFS, 0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putInt(SyncAdapter.SYNC_PROGRESS, 0);
        editor.commit();

        Intent intent = new Intent();
        intent.setAction(START_SYNC);
        mContext.sendBroadcast(intent);


        //... do some stuff, setting SYNC_PROGRESS to other values and
        // sending more broadcasts as the state changes

        // When we are done, remove the "in progress" setting and store some
        // other data
        editor.putString(SyncAdapter.LAST_UPDATED, new Date().toString());
        editor.remove(SyncAdapter.SYNC_PROGRESS);
        editor.commit();

        Intent stopIntent = new Intent();
        stopIntent.setAction(STOP_SYNC);
        mContext.sendBroadcast(stopIntent); 
      }

1) , , 2) .

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
// .. do some UI stuff

    mReceiver = new SyncReceiver(this);
}

@Override
public void onResume() {
    super.onResume();

    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(SyncAdapter.START_SYNC);
    intentFilter.addAction(SyncAdapter.STOP_SYNC);
    registerReceiver(mReceiver, intentFilter);  

    showProgress();
}

public void showProgress() {
    SharedPreferences settings = getSharedPreferences(Constants.PREFS, 0);
    if (settings.contains(SyncAdapter.SYNC_PROGRESS)) {
        // ... set the UI to show that a sync is in progress
    } else {
        // ... set the UI to show that a sync is NOT in progress
    }
}

private class SyncReceiver extends BroadcastReceiver {

    private MyActivity mActivity;

    public SyncReceiver(MyActivity activity) {
        mActivity = activity;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(SyncAdapter.START_SYNC)) {
            Log.i("@string/app_name", "Started sync");
            mActivity.showProgress();
        }
        else if (intent.getAction().equals(SyncAdapter.STOP_SYNC)) {
            Log.i("@string/app_name", "Started sync");
            mActivity.showProgress();
        }
    }
}

, . , , - . !

+3

.

- () , , .

, SyncObserver, SyncStatusObserver / /.

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

, (, ..),

ContentResolver.isSyncActive();

API Android , , , , - , , .

+5

All Articles