SyncStatusObserver中的onStatusChanged()被多次调用

时间:2015-03-26 10:20:01

标签: android android-contentprovider

在我的应用中,我需要通过Google日历显示即将举办的活动。我用Calendar Provider完成了它。为了跟踪新事件(在Google日历的网络版中添加的事件),我决定请求同步日历,如this answer中所述。

它运行良好,但我注意到多次调用onStatusChanged(int)方法。以下代码

@Override
public void onStatusChanged(int which) {
    AccountManager am = AccountManager.get(this);
    Account[] acc = am.getAccountsByType("com.google");
    Account account;
    if (acc.length > 0) {
        account = acc[0];
        boolean syncActive = ContentResolver.isSyncActive(
                account, CalendarContract.AUTHORITY);
        Log.d(TAG, "sync done = " + !syncActive);

        // is it right to update view here depending on syncActive value?

    }
}

给我这样的输出:

03-26 13:11:19.650  D/EventsActivity﹕ sync done = false
03-26 13:11:20.919  D/EventsActivity﹕ sync done = true
03-26 13:11:20.924  D/EventsActivity﹕ sync done = false
03-26 13:11:21.651  D/EventsActivity﹕ sync done = true
03-26 13:11:21.656  D/EventsActivity﹕ sync done = false
03-26 13:11:22.253  D/EventsActivity﹕ sync done = true
03-26 13:11:22.256  D/EventsActivity﹕ sync done = false
03-26 13:11:22.761  D/EventsActivity﹕ sync done = true
03-26 13:11:22.777  D/EventsActivity﹕ sync done = false
03-26 13:11:23.359  D/EventsActivity﹕ sync done = true

如何注册观察员:

mContentProviderHandle = ContentResolver.addStatusChangeListener(
        ContentResolver.SYNC_OBSERVER_TYPE_ACTIVE, this);

我想更新我的观点,我将评论放在第一段代码中,但很明显我的观点会多次更新。我错过了什么?

0 个答案:

没有答案