Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I had this same problem and ended up implementing it with a combination of 1) a broadcast from the SyncAdapter, and 2) using SharedPreferences to indicate status.</p> <p>In the SyncAdapter, something like this this:</p> <pre><code>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); } </code></pre> <p>In the activity we do two things at resume 1) check the shared preference for whether a sync is currently in progress, 2) register to listen for broadcasts with a receiver.</p> <pre><code>@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(); } } } </code></pre> <p>This seems to work for me. I must admit I have a feeling that there are some potential issues with this due to the asynchronous nature of the broadcasts. Any input on improving my approach would be appreciated!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload