简体   繁体   English

Android:听取像WhatsApp这样的联系人更改

[英]Android: Listening to contact changes like WhatsApp do

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. 我已经创建了一个Account并在需要时代表此帐户创建RawContacts
And I use SyncAdapter and things are great. 我使用SyncAdapter ,事情很棒。

But I'm still missing some parts of this puzzle: 但我仍然错过了这个难题的一些部分:

I'm trying to implement a behavior similar to WhatsApp. 我正在尝试实现类似于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. 此联系人是否已经在“我的” RawContacts ,他是否刚刚由用户创建。

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. 我知道有ContentObserver但跟踪ContactsContract.Contacts.CONTENT_URI似乎是错误的事情,因为它没有给出具体的更改,加上它被提升到很多次,以及从许多不感兴趣的事件。

I know that WhatsApp are using SyncAdapter but I think they might be doing something more. 我知道WhatsApp正在使用SyncAdapter,但我认为他们可能会做更多的事情。

Any idea would be much appreciated. 任何想法将不胜感激。

ContentObserver tracking ContactsContract.Contacts.CONTENT_URI is indeed the way to go. ContentObserver跟踪ContactsContract.Contacts.CONTENT_URI确实是要走的路。

When you get onChange() you query the raw_contacts table with following condition: 当你使用onChange()时,你可以使用以下条件查询raw_contacts表:

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(). 如果您有兴趣了解更多详细信息 - 您需要订阅ContactsContract.Contacts.CONTENT_URI的原因是联系人提供商目前不会通知onChange()中哪些联系人被修改。 This is because of the applyBatch() transactions where multiple contacts may change. 这是因为applyBatch()事务可能会更改多个联系人。 May be in future there will be a way to subscribe to a subset - but currently there is none. 可能在将来会有一种订阅子集的方法 - 但目前没有。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM