Can you determine what contact changes w/ registerContentObserver?

6.9k views Asked by At

Just trying to determine if there is an easy way to determine what contact changes when using registerContentObserver, or some other method? I can tell when the database changes, but not sure I want to check all the records each time an update/change happens.

Basically if I have a list of contacts, and one gets updated via manual method (i.e. I change phone number for a contact or add a new contact), or automatic (say ActiveSync photo gets added, changed). I want to be able to A) Know that something changed, and B) Know what contact had some data changed.

I am trying to look for certain users where I may be able to match their photos from a remote service. If more information gets added to a user account, I want to be able to examine it to see if it provides for a match. I don't want to schedule it though, I would like to do it after an update happens.

Chris.

2

There are 2 answers

4
devunwired On BEST ANSWER

I'm not sure which Uri you are currently registering for updates, but when you register your ContentObserver, can't you just register an individual Uri that points directly to the contact you are interested in? Then your observer will only get notified when your interesting contact is modified. The section in the SDK Documentation title "Lookup URI" talks more about this:

http://developer.android.com/resources/articles/contacts.html

The lookup key is unique to each record (row), so I would imagine doing something like this is what you are after?

Uri lookupUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey);
getContentResolver().registerContentObserver(lookupUri, false, myObserver);

And do that for each individual contact in your list.

For Added Contacts

The best way that I can see to do this is to re-query a cursor to the Contacts table each time the ContentObserver changes (re-query is fast, even if you're doing it often with lots of data), and maintain the highest known CONTACT_ID value. Whenever the cursor contains an ID beyond what you have stored, it was just added.

Also, if you haven't seen this conversation yet, it might be a good read. Dianne seems pretty adament that they keep specifics of content changes out of ContentObserver

Cheers.

1
Adrian Aslau On

Since this question was answered, there is a new callback method available in the ContentObserver class that has the uri of the changed content: http://developer.android.com/reference/android/database/ContentObserver.html#onChange(boolean, android.net.Uri)

This is available starting with the Android API Level 16