如何知道同步何时完成? [英] How to know when sync is finished?

查看:24
本文介绍了如何知道同步何时完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了一个同步适配器,我想在它在我的活动中完成时得到一个回调.我曾尝试使用 ContentResolver.addStatusChangeListener,但我只在同步挂起/活动时收到回调.这是我的活动中的一些相关代码:

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 方法中的 if 永远不会有效.我从 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆