同期アダプターによって追加された連絡先の 1 つが変更されたときに通知する必要があるコンテンツ オブザーバーがあります。これを行うオブザーバーを登録および登録解除します。
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 が呼び出されることです。カーソル内のエントリの 1 つが変更された場合にのみ、オブザーバーを呼び出すべきではありませんか? ドキュメントには次のように記載されています。
このカーソルの背後にあるコンテンツに変更が発生したときに呼び出されるオブザーバーを登録します
「このカーソルを支えているコンテンツ」とは何ですか? カーソル内の連絡先のルックアップのリストだと思っていましたが、ContactsContract.RawContacts.CONTENT_URI を変更するだけで十分のようです。
また、Uri ごとに 1 つのオブザーバーを登録しようとしました。それは役に立ちません。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に設定しましたが、オブザーバーを呼び出すべきではありませんでした)。
どうしたの?ありがとうございました