I have my own ContentProvider
and SyncAdapter
which both work fine.
I set them to be automatically synced with ContentResolver.setSyncAutomatically()
an this work. I can also test the sync with Dev Tools -> Sync Tester.
Now I would like to request a sync from my app (if we have no data yet) and be notified when it finishes, so I can updated the interface (I'm showing a progress bar with logo while it's syncing). I'm doing this with ContentResolver.requestSync()
, but I have not found a way to get notified when the sync finishes.
Does anyone know how to do this? Thanks.
Here is a fully working code snippet with javadocs for anyone wanting a drop in solution rather than having to guess how to put everything together. It builds on Mark's answer above. Supports monitoring multiple account syncs.
import android.accounts.Account;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
/**
* Sync status observer that reports back via a callback interface when syncing has begun
* and finished.
*/
public static class MySyncStatusObserver implements SyncStatusObserver {
/**
* Defines the various sync states for an account.
*/
private enum SyncState {
/**
* Indicates a sync is pending.
*/
PENDING,
/**
* Indicates a sync is no longer pending but isn't active yet.
*/
PENDING_ACTIVE,
/**
* Indicates a sync is active.
*/
ACTIVE,
/**
* Indicates syncing is finished.
*/
FINISHED
}
/**
* Lifecycle events.
*/
public interface Callback {
/**
* Indicates syncing of calendars has begun.
*/
void onSyncsStarted();
/**
* Indicates syncing of calendars has finished.
*/
void onSyncsFinished();
}
/**
* The original list of accounts that are being synced.
*/
@NonNull private final List<Account> mAccounts;
/**
* Map of accounts and their current sync states.
*/
private final Map<Account, SyncState> mAccountSyncState =
Collections.synchronizedMap(new HashMap<Account, SyncState>());
/**
* The calendar authority we're listening for syncs on.
*/
@NonNull private final String mCalendarAuthority;
/**
* Callback implementation.
*/
@Nullable private final Callback mCallback;
/**
* {@code true} when a "sync started" callback has been called.
*
* <p>Keeps us from reporting this event more than once.</p>
*/
private boolean mSyncStartedReported;
/**
* Provider handle returned from
* {@link ContentResolver#addStatusChangeListener(int, SyncStatusObserver)} used to
* unregister for sync status changes.
*/
@Nullable private Object mProviderHandle;
/**
* Default constructor.
*
* @param accounts the accounts to monitor syncing for
* @param calendarAuthority the calendar authority for the syncs
* @param callback optional callback interface to receive events
*/
public MySyncStatusObserver(@NonNull final Account[] accounts,
@NonNull final String calendarAuthority, @Nullable final Callback callback) {
mAccounts = Lists.newArrayList(accounts);
mCalendarAuthority = calendarAuthority;
mCallback = callback;
}
/**
* Sets the provider handle to unregister for sync status changes with.
*/
public void setProviderHandle(@Nullable final Object providerHandle) {
mProviderHandle = providerHandle;
}
@Override
public void onStatusChanged(int which) {
for (final Account account : mAccounts) {
if (which == ContentResolver.SYNC_OBSERVER_TYPE_PENDING) {
if (ContentResolver.isSyncPending(account, mCalendarAuthority)) {
// There is now a pending sync.
mAccountSyncState.put(account, SyncState.PENDING);
} else {
// There is no longer a pending sync.
mAccountSyncState.put(account, SyncState.PENDING_ACTIVE);
}
} else if (which == ContentResolver.SYNC_OBSERVER_TYPE_ACTIVE) {
if (ContentResolver.isSyncActive(account, mCalendarAuthority)) {
// There is now an active sync.
mAccountSyncState.put(account, SyncState.ACTIVE);
if (!mSyncStartedReported && mCallback != null) {
mCallback.onSyncsStarted();
mSyncStartedReported = true;
}
} else {
// There is no longer an active sync.
mAccountSyncState.put(account, SyncState.FINISHED);
}
}
}
// We haven't finished processing sync states for all accounts yet
if (mAccounts.size() != mAccountSyncState.size()) return;
// Check if any accounts are not finished syncing yet. If so bail
for (final SyncState syncState : mAccountSyncState.values()) {
if (syncState != SyncState.FINISHED) return;
}
// 1. Unregister for sync status changes
if (mProviderHandle != null) {
ContentResolver.removeStatusChangeListener(mProviderHandle);
}
// 2. Report back that all syncs are finished
if (mCallback != null) {
mCallback.onSyncsFinished();
}
}
}
Here is the implementation:
public class MyActivity extends Activity implements MySyncStatusObserver.Callback {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.some_layout);
// Retrieve your accounts
final Account[] accounts = AccountManager.get(this).getAccountsByType("your_account_type");
// Register for sync status changes
final MySyncStatusObserver observer = new MySyncStatusObserver(accounts, "the sync authority", this);
final Object providerHandle = ContentResolver.addStatusChangeListener(
ContentResolver.SYNC_OBSERVER_TYPE_ACTIVE |
ContentResolver.SYNC_OBSERVER_TYPE_PENDING, observer);
// Pass in the handle so the observer can unregister itself from events when finished.
// You could optionally save this handle at the Activity level but I prefer to
// encapsulate everything in the observer and let it handle everything
observer.setProviderHandle(providerHandle);
for (final Account account : accounts) {
// Request the sync
ContentResolver.requestSync(account, "the sync authority", null);
}
}
@Override
public void onSyncsStarted() {
// Show a refresh indicator if you need
}
@Override
public void onSyncsFinished() {
// Hide the refresh indicator if you need
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With