Android: Listening to contact changes like WhatsApp do
Asked Answered
A

1

10

I'm building an App which relies heavily on the user's contacts.

I've created an Account and create RawContacts on behalf of this account when needed.
And I use SyncAdapter and things are great.

But I'm still missing some parts of this puzzle:

I'm trying to implement a behavior similar to WhatsApp. Such that-

When a change happens in the name or phone of any contact - it would be sent to the server for evaluation.

Whether this contact was already in "my" RawContacts, whether he was just created now by the user.

I know there's the ContentObserver but tracking ContactsContract.Contacts.CONTENT_URI seems like the wrong thing to do because it doesn't give the specific change, plus it gets raised to many times, and from to many events that don't interest me.

I know that WhatsApp are using SyncAdapter but I think they might be doing something more.

Any idea would be much appreciated.

Assai answered 28/6, 2015 at 18:2 Comment(3)
@Hagai...hello any update about how you tackled this ?? I have used CONTACT_LAST_UPDATED_TIMESTAMP in my code to get the latest updated contacts but this stamp does not change if you update any field in a contact which has only email id with it.Depredate
@Hagai... hello... it would be great if you could share your findings by answering your own question if you have succeeded in solving it.Tyndall
Hi, years have passed... and as far as I remember - I didn't get an answer that was good enough for what I've needed.Assai
C
1

ContentObserver tracking ContactsContract.Contacts.CONTENT_URI is indeed the way to go.

When you get onChange() you query the raw_contacts table with following condition:

String where = "(rawcontacts.dirty = true or rawcontacts.deleted = 1) and rawcontacts.account_type = <your_custom_type>"

The result will give you contacts added, updated or deleted.

If you are interested in knowing more details - the reason why you need to subscribe to ContactsContract.Contacts.CONTENT_URI is that the contacts provider currently does not notify which contacts were modified in onChange(). This is because of the applyBatch() transactions where multiple contacts may change. May be in future there will be a way to subscribe to a subset - but currently there is none.

Couple answered 13/11, 2015 at 8:10 Comment(1)
It would be great if you could post a full example of how to do the query in onChange().Tyndall

© 2022 - 2024 — McMap. All rights reserved.