4

我有一个内容观察者,当我的同步适配器添加的联系人之一被修改时,应该通知它。我注册和注销观察者这样做:

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,但无论如何它都不应该调用观察者)。

怎么了?谢谢

4

2 回答 2

1

由内容提供者决定何时报告更改。对于复杂的内容提供者(如联系人提供者),可能很难确定由于操作而发生变化的所有特定 URI,因此它们只会在发生某些事情时报告全局变化。

于 2012-02-25T03:03:36.983 回答
0

发生 Observer Uri 匹配时,会考虑 Uri、Fragment 甚至 Scheme 中的查询参数。唯一重要的是 Uri 权限和路径段。发生严格的从左到右匹配。我没有在路径段中测试“*”来表示通配符,但我怀疑它不起作用。

您的特定观察者是 ContactsContract.RawContacts.CONTENT_URI,因此任何时候任何联系人内容因任何原因发生更改时,您的观察者都会触发。

于 2014-03-15T02:06:58.210 回答