如何知道什么时候同步完成? [英] How to know when sync is finished?
问题描述
我实现了一个同步适配器,我想,当它完成我的活动来获得一个回调。我曾试过用<一个href="http://developer.android.com/reference/android/content/ContentResolver.html#addStatusChangeListener%28int,%20android.content.SyncStatusObserver%29"><$c$c>ContentResolver.addStatusChangeListener$c$c>,但我只得到回调时同步挂起/主动。下面是我的活动有一定相关code:
I have implemented a sync adapter and I want to get a callback when it finishes in my activity. I have tried using ContentResolver.addStatusChangeListener
, but I am only getting callbacks when the sync is pending / active. Here's some relevant code from my activity:
@Override
protected void onResume() {
super.onResume();
final int mask = ContentResolver.SYNC_OBSERVER_TYPE_ACTIVE | ContentResolver.SYNC_OBSERVER_TYPE_PENDING;
syncObserverHandle = ContentResolver.addStatusChangeListener(mask, syncStatusObserver);
}
@Override
protected void onPause() {
super.onPause();
if (syncObserverHandle != null) {
ContentResolver.removeStatusChangeListener(syncObserverHandle);
syncObserverHandle = null;
}
}
private SyncStatusObserver syncStatusObserver = new SyncStatusObserver() {
@Override
public void onStatusChanged(int which) {
AccountManager am = AccountManager.get(TodosActivity.this);
Account a = am.getAccountsByType(Const.ACCOUNT_TYPE)[0];
Log.d(Const.TAG, "Sync status changed: " + which);
if (!ContentResolver.isSyncActive(a, DataProvider.AUTHORITY) &&
!ContentResolver.isSyncPending(a, DataProvider.AUTHORITY)) {
Log.d(Const.TAG, "Sync finished, should refresh nao!!");
}
}
};
不过,如果
在 onStatusChanged
的方法是永远有效的。我已经采取了这个例子从<一个href="http://$c$c.google.com/p/jumpnote/source/browse/trunk/JumpNoteAndroid/src/com/example/jumpnote/android/NotesList.java#266">JumpNote演示它的工作原理可能是因为它是手动 onResume()
,因此它可能是从来没有所谓的由系统的时候也被称为同步完成。或者是它,我做错了什么?下面是我得到的logcat:
However, the if
in the onStatusChanged
method is never valid. I have taken this example from the JumpNote demo where it works probably because it is also called manually in onResume()
, so it's probably never called by the system when the sync is finished. Or is it, and I'm doing something wrong? Here's what I get in logcat:
D/MYAPP (10903): Sync status changed: 2
D/MYAPP (10903): Sync status changed: 2
D/MYAPP (10903): Sync status changed: 4
D/MYAPP (10981): --> DataSyncAdapter.onPerformSync()
D/MYAPP (10981): <-- DataSyncAdapter.onPerformSync()
D/MYAPP (10903): Sync status changed: 4
所以,看来我的可以的依靠第二 SYNC_OBSERVER_TYPE_ACTIVE
(4)状态改变刷新我的数据,但似乎<强>真的难看。任何想法?
So, it seems that I could rely on the second SYNC_OBSERVER_TYPE_ACTIVE
(4) status change to refresh my data, but that seems really ugly. Any ideas?
推荐答案
我已经找到一种解决方案是沟 ContentResolver的
完全,并实现自己的广播。基本上,在同步适配器添加此,在年底 onPerformSync
:
One solution that I have found is to ditch the ContentResolver
completely, and implement my own broadcast. Basically, add this in the sync adapter, at the end of onPerformSync
:
Intent i = new Intent(SYNC_FINISHED);
sendBroadcast(i);
这在活动:
@Override
protected void onResume() {
super.onResume();
registerReceiver(syncFinishedReceiver, new IntentFilter(DataSyncService.SYNC_FINISHED));
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(syncFinishedReceiver);
}
private BroadcastReceiver syncFinishedReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d(Const.TAG, "Sync finished, should refresh nao!!");
}
};
这似乎工作得很好,但我希望能找到一些在SDK时,同步完成后直接通知我。
This seems to work just fine, however I was hoping to find something in the SDK that directly notify me when a sync is finished.
这篇关于如何知道什么时候同步完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!