我有我自己ContentProvider和SyncAdapter这两者很好地工作.
我将它们设置为自动与ContentResolver.setSyncAutomatically()此作品同步.我还可以使用Dev Tools - > Sync Tester测试同步.
现在,我想从我的应用程序请求同步(如果我们有没有数据),并在完成时得到通知,所以我可以更新的界面(我展示一个进度条与标志,而它的同步).我正在这样做ContentResolver.requestSync(),但我没有找到一种方法在同步完成时得到通知.
有谁知道如何做到这一点?谢谢.
这是一个完整工作的代码片段,其中包含javadocs,适用于任何想要提供解决方案而不必猜测如何将所有内容放在一起的人.它建立在Mark的答案之上.支持监控多个帐户同步.
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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是实施:
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
}
}
Run Code Online (Sandbox Code Playgroud)
addStatusChangeListener() 会在同步完成时通知您,它只是以稍微迂回的方式通知您:调用SyncStatusObserver.onStatusChanged()来通知您状态已更改.然后,您必须调用ContentResolver.isSyncPending()或ContentResolver.isSyncActive()来检查新状态.
...
ContentResolver.addStatusChangeListener(
ContentResolver.SYNC_OBSERVER_TYPE_PENDING
| ContentResolver.SYNC_OBSERVER_TYPE_ACTIVE,
new MySyncStatusObserver());
...
private class MySyncStatusObserver implements SyncStatusObserver {
@Override
public void onStatusChanged(int which) {
if (which == ContentResolver.SYNC_OBSERVER_TYPE_PENDING) {
// 'Pending' state changed.
if (ContentResolver.isSyncPending(mAccount, MY_AUTHORITY)) {
// There is now a pending sync.
} else {
// There is no longer a pending sync.
}
} else if (which == ContentResolver.SYNC_OBSERVER_TYPE_ACTIVE) {
// 'Active' state changed.
if (ContentResolver.isSyncActive(mAccount, MY_AUTHORITY)) {
// There is now an active sync.
} else {
// There is no longer an active sync.
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
还有一点需要注意:在我的测试中,当我请求同步时,我的onStatusChanged()方法被调用了四次:
所以看起来在待处理和活动之间有一个窗口,即使活动同步即将开始,两者都被设置为假.
addStatusChangeListener()当同步为SYNC_OBSERVER_TYPE_ACTIVE或时,使用将为您提供回调SYNC_OBSERVER_TYPE_PENDING。很奇怪的是,没有完成的事件。
这是Felix 建议的解决方法。他建议你应该放弃ContentResolver广播,转而使用广播。
| 归档时间: |
|
| 查看次数: |
4041 次 |
| 最近记录: |