質問

I have a content observer that should be notified when one of the contacts added by my sync adapter is modified. I register and unregister the observer doing this:

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();
    }
}

The problem is that even when the cursor is empty (getCount returns 0) after I register the observer I get a call to onChange what ever I do in the native address book. Shoudn't the observer be called only when one of the entries in the cursor was modified? The documentation states:

Register an observer that is called when changes happen to the content backing this cursor

What's "the content that is backing this cursor"? I thought it was the list of lookupuri of the contacts in the cursor but it looks like it is enough to have a change in the ContactsContract.RawContacts.CONTENT_URI.

I have also tried to register one observer for each Uri. It does not help. Although the documentation for ContentResolver.registerContentObserver states:

Register an observer class that gets callbacks when data identified by a given content URI changes.

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. 

(I set notifyForDescendents to false but it shouldn't have called the observers in any case).

What's wrong? Thank-you

役に立ちましたか?

解決

It is up to the content provider to decide when to report changes. For complicated content providers (like the contacts provider) it can be very difficult to determine all of the specific URIs that change due to an operation, so they just report a global change when something happens.

他のヒント

Query parameters in your Uri, the Fragment, nor even the Scheme are considered when Observer Uri matching occurs. The only thing that matters is the Uri Authority and the Path Segments. Strict left to right matching occurs. I have not tested "*" in a path segment to denote a wildcard, but I suspect that it will not work.

Your particular Observer is ContactsContract.RawContacts.CONTENT_URI, so any time any contact content changes for any reason, your Observer will fire.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top