2012-02-07 38 views
4

我有一个内容观察者,当我的同步适配器添加的联系人之一被修改时应该被通知。 我注册和注销观察者这样做:即使光标没有变化,也会调用ContentObserver

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,但它不应该有所谓的在任何情况下观察者)。

怎么了? 谢谢

回答

1

由内容提供商决定何时报告更改。对于复杂的内容提供者(例如联系人提供者),要确定由于操作而发生更改的所有特定URI可能非常困难,因此他们只是在出现问题时报告全局更改。

+0

那么为什么文档会说一些不同的东西? – kingston 2012-02-28 16:40:58

0

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

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

相关问题