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.
In apps that use phone authentication like whatsapp: When any user opens to check his/her contacts, whatsapp will show only the contacts from your phone that are using whatsapp (like they filter your contacts).
When you use contact upload and grant WhatsApp access to your device address book, WhatsApp will access and upload the phone numbers in your address book typically daily, but this depends on various factors including how often a user uses WhatsApp, including those of WhatsApp users and your other contacts.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With