2012-02-07 32 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都會觸發。

相關問題