在Android上删除联系人时,其他随机联系人的ID已更改 [英] When deleting a contact on android, other random contacts id's being changed

查看:68
本文介绍了在Android上删除联系人时,其他随机联系人的ID已更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将用户电话号码同步到Firestore.它似乎可以正常工作,但是当我从手机中删除联系人时,似乎其他一些联系人ID已被替换,从而导致不必要的删除和对Firestore的创建.我唯一的线索是,这些联系人大多在ID 120,000左右.正常吗怎么回事?

这是我获取新创建的联系人的方式,lcid是注册到Firestore的最后一个联系人ID:

 私人乐趣getNewContacts():游标?{val投影= arrayOf(ContactsContract.Contacts._ID,ContactsContract.Contacts.DISPLAY_NAME,ContactsContract.Contacts.HAS_PHONE_NUMBER)val selection = ContactsContract.Contacts._ID +>?"val selectionArgs = arrayOf(mFireContactDetails !!.lcid.toString())val sortOrder = ContactsContract.Contacts._ID +"ASC"返回mContentResolver.query(ContactsContract.Contacts.CONTENT_URI,投影,选择,selectionArgs,排序)} 

这是我如何获取已删除的联系人,ldel_ms是注册到Firestore的最后一个删除的时间戳:

 私人乐趣getDeletedContacts():游标?{val投影= arrayOf(ContactsContract.DeletedContacts.CONTACT_ID,ContactsContract.DeletedContacts.CONTACT_DELETED_TIMESTAMP)val selection = ContactsContract.DeletedContacts.CONTACT_DELETED_TIMESTAMP +>?"val selectionArgs = arrayOf(mFireContactDetails !!.ldel_ms.toString())val sortOrder = ContactsContract.DeletedContacts.CONTACT_DELETED_TIMESTAMP +"ASC"返回mContentResolver.query(ContactsContract.DeletedContacts.CONTENT_URI,投影,选择,selectionArgs,排序)} 

接下来是一个日志示例.当我删除联系人时,它会被识别为已删除,但其他随机联系人也会替换其ID(我没有提及日志名称)

 重视检索成功检测到新的联系人新增120797新增120803新增120804新增120805新增120806新增120807新增120808新增120809新增120810同步新联系人成功检测到已删除的联系人删除联系人ID:119576删除联系人ID:120798删除联系人ID:120799删除联系人ID:120800删除联系人ID:120801删除联系人ID:120802删除联系人ID:119762删除联系人ID:119700删除联系人ID:119561删除联系人ID:119613同步删除的联系人成功 

解决方案

从文档和文章看来,android有时可以更改联系人ID,这很奇怪并且出乎意料,但是经过我数周的尝试后才有意义试图解决这个问题.

据我了解,原因是多种多样的,因此在我的情况下,我也可以猜测一次更改或删除联系人可以更改其他联系人ID的情况.

看来,要克服这一点,我需要使用`LOOKUP_KEY,我尚未了解如何使用.

一些文档包括:

检测android联系人中的更改

检查联系人是否已在...上更改

LOOKUP_KEY

希望它会有所帮助,因为android文档很难遵循

I am trying to sync user phone numbers to firestore. It seems to work but when i delete a contact from my phone, it seems like some other contacts id's are being replaced, causing unnecessary deletion and creation to firestore. My only clue is that these contacts mostly around id 120,000. Is it normal? What is goung on?

This is how i get new created contacts, lcid is the last-contact-id registered to firestore:

private fun getNewContacts(): Cursor? {
    val projection = arrayOf(
            ContactsContract.Contacts._ID,
            ContactsContract.Contacts.DISPLAY_NAME,
            ContactsContract.Contacts.HAS_PHONE_NUMBER)

    val selection = ContactsContract.Contacts._ID + "> ?"

    val selectionArgs = arrayOf(mFireContactDetails!!.lcid.toString())

    val sortOrder = ContactsContract.Contacts._ID + " ASC"

    return mContentResolver.query(
            ContactsContract.Contacts.CONTENT_URI,
            projection,
            selection,
            selectionArgs,
            sortOrder)
}

This is how i get the deleted contacts, ldel_ms is the last deleted timestamp registered to firestore:

private fun getDeletedContacts(): Cursor? {
    val projection = arrayOf(
            ContactsContract.DeletedContacts.CONTACT_ID,
            ContactsContract.DeletedContacts.CONTACT_DELETED_TIMESTAMP)

    val selection = ContactsContract.DeletedContacts.CONTACT_DELETED_TIMESTAMP + "> ?"

    val selectionArgs = arrayOf(mFireContactDetails!!.ldel_ms.toString())

    val sortOrder = ContactsContract.DeletedContacts.CONTACT_DELETED_TIMESTAMP + " ASC"

    return mContentResolver.query(
            ContactsContract.DeletedContacts.CONTENT_URI,
            projection,
            selection,
            selectionArgs,
            sortOrder)
}

Next is a log example. When i delete a contact it is recognized as deleted but also other random contacts replacing their ID (i did not mention the log names)

values retrieval success
new contacts detected
adding 120797
adding 120803
adding 120804
adding 120805
adding 120806
adding 120807
adding 120808
adding 120809
adding 120810
sync new contacts success
deleted contacts detected
deleting contact id: 119576
deleting contact id: 120798
deleting contact id: 120799
deleting contact id: 120800
deleting contact id: 120801
deleting contact id: 120802
deleting contact id: 119762
deleting contact id: 119700
deleting contact id: 119561
deleting contact id: 119613
sync deleted contacts success

解决方案

From the documentations and articles it seems that android can sometimes change the contact id, this is very strange and un-expected but makes sense after my many weeks and attempts trying to solve this issue.

From what i can understand, the reasons are quite diverse so i can guess also in my case when one change or delete of contact can change other contact id's.

It seems that to overcome, i need to use the `LOOKUP_KEY, which i yet to understand how.

Some documentations includes:

Detecting changes in android contacts

Check if contact has been changes on...

LOOKUP_KEY

Hopes it helps since the android documentations are so hard to follow

这篇关于在Android上删除联系人时,其他随机联系人的ID已更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆