我有一个内容观察者,当我的同步适配器添加的联系人之一被修改时,应该通知它。我注册和注销观察者这样做:
private static final Uri MYAPP_CONTENT_URI = ContactsContract.RawContacts.CONTENT_URI.buildUpon().appendQueryParameter(RawContacts.ACCOUNT_NAME, SyncAdapter.MYAPP_ACCOUNT_NAME).appendQueryParameter(RawContacts.ACCOUNT_TYPE, MY_APP_ACCOUNT_TYPE).build();
public static void registerContentObserver() {
ContentResolver resolver = MyApplication.getAppContext().getContentResolver();
cursorContacts = resolver.query(MYAPP_CONTENT_URI, null, RawContacts.DELETED + "=0", null, null);
cursorContacts.registerContentObserver(MYAPP_URI_OBSERVER);
}
public static void unregisterContentObserver() {
if (cursorContacts != null) {
cursorContacts.unregisterContentObserver(MYAPP_URI_OBSERVER);
cursorContacts.close();
}
}
问题是,即使在我注册观察者后光标为空(getCount 返回 0)时,我也会调用 onChange 我在本机通讯簿中所做的任何事情。不应该仅在修改光标中的条目之一时调用观察者吗?该文档指出:
注册一个观察者,当支持此游标的内容发生更改时调用
什么是“支持此光标的内容”?我认为它是光标中联系人的lookupuri列表,但看起来它足以改变ContactsContract.RawContacts.CONTENT_URI。
我还尝试为每个 Uri 注册一个观察者。它没有帮助。虽然 ContentResolver.registerContentObserver 的文档指出:
注册一个观察者类,当给定内容 URI 标识的数据发生变化时,该类会获得回调。
Parameters
uri The URI to watch for changes. This can be a specific row URI, or a base URI for a whole class of content.
notifyForDescendents If true changes to URIs beginning with uri will also cause notifications to be sent. If false only changes to the exact URI specified by uri will cause notifications to be sent. If true, than any URI values at or below the specified URI will also trigger a match.
(我将 notifyForDescendents 设置为 false,但无论如何它都不应该调用观察者)。
怎么了?谢谢