【问题标题】:How to remove duplicate entries in Android Contacts App which gets added when adding new contacts programatically?如何删除以编程方式添加新联系人时添加的 Android 联系人应用程序中的重复条目?
【发布时间】:2020-09-02 07:40:40
【问题描述】:

我有一个应用程序,它使用以下代码在电话簿中插入/更新联系人,该代码有效,但我面临的问题是,如果有类似的联系人使用相同的电话号码,例如。一个 WhatsApp、dou、Viber 等联系人,然后这些应用程序联系人接管我的联系人 DisplayName 并合并在一起,但由于这个原因,我们在某些版本的 android 中存在多个重复条目,如(三星、LG、MI A5 股票 android)等。

但适用于一些手机,如 MI Max 和其他一些手机,是否有人有解决此问题的方法,或者我是否缺少一些需要提供以避免重复联系人的字段。

private fun insertContact(contact: Contact): Boolean {
    try {
        val operations = ArrayList<ContentProviderOperation>()
            ContentProviderOperation.newInsert(RawContacts.CONTENT_URI).apply {
                withValue(RawContacts.ACCOUNT_NAME, "abcd@gmail.com")
                withValue(RawContacts.ACCOUNT_TYPE, "google.com")
                operations.add(build())
            }

        ContentProviderOperation.newInsert(Data.CONTENT_URI).apply {
            withValueBackReference(Data.RAW_CONTACT_ID, 0)
            withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
            withValue(StructuredName.GIVEN_NAME, contact.firstName)
            withValue(StructuredName.FAMILY_NAME, contact.lastName)
            withValue(StructuredName.SUFFIX,  "AppName")
            operations.add(build())
        }

        addUpdatePhone(operations, contact.phoneNumbers)
        //similar function for other fields email, address, birthday, profilePic etc

        val results = context.contentResolver.applyBatch(ContactsContract.AUTHORITY, operations)
        return true
    } catch (e: Exception) {
        LOG.e( "Error inserting contact")
        return false
    }
}

private fun updateContact(contact: contact, rawContactId: String): Boolean {
    try {
        val operations = ArrayList<ContentProviderOperation>()
        ContentProviderOperation.newUpdate(Data.CONTENT_URI).apply {
            val selection = "${Data.RAW_CONTACT_ID} = ? AND ${Data.MIMETYPE} = ?"
            val selectionArgs = arrayOf(rawContactId, StructuredName.CONTENT_ITEM_TYPE)
            withSelection(selection, selectionArgs)
            withValue(StructuredName.GIVEN_NAME, contact.firstName)
            withValue(StructuredName.FAMILY_NAME, contact.lastName)
            withValue(StructuredName.SUFFIX, "AppName")
            operations.add(build())
        }

        addUpdatePhone(operations, contact.phoneNumbers, true, rawContactId)
        //similar function for other fields email, address, birthday, profilePic etc

        context.contentResolver.applyBatch(ContactsContract.AUTHORITY, operations)
        return true
    } catch (e: Exception) {
        LOG.e("Error updating contact")
        return false
    }
}

private fun addUpdatePhone(operations: ArrayList<ContentProviderOperation>, phoneNumbers: List<PhoneNumber>, isUpdate: Boolean = false, rawContactId: String = "") {
    if(isUpdate) {
        //delete old data with the given raw_contact_id
        ContentProviderOperation.newDelete(Data.CONTENT_URI).apply {
            val selection = "${Data.RAW_CONTACT_ID} = ? AND ${Data.MIMETYPE} = ? "
            val selectionArgs = arrayOf(rawContactId, Phone.CONTENT_ITEM_TYPE)
            withSelection(selection, selectionArgs)
            operations.add(build())
        }
    }

    phoneNumbers.forEach {
        //add new rows of phone number for the given raw_contact_id
        ContentProviderOperation.newInsert(Data.CONTENT_URI).apply {
            if(isUpdate) {
                withValue(Data.RAW_CONTACT_ID, rawContactId)
            } else {
                withValueBackReference(Data.RAW_CONTACT_ID, 0)
            }

            withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
            withValue(Phone.TYPE, it.type)
            withValue(Phone.LABEL, it.label)
            withValue(Phone.NUMBER, it.value)
            withValue(Phone.NORMALIZED_NUMBER, it.value.normalizeNumber())
            operations.add(build())
        }
    }
}

//similar functions as above for other fields
private fun otherUpdateFunAsAbove() {}

【问题讨论】:

    标签: android kotlin android-contentprovider android-contacts


    【解决方案1】:

    添加新的 RawContact 后,Android 需要判断这是一个全新的联系人,还是已经存在代表同一个人的联系人。

    Android has an algorithm,随着操作系统版本的不同而变化和发展,可能已经被三星等制造商调整过,但通常它会寻找与其他项目(如电话或电子邮件)非常相似的名称,或者相同或非常接近。

    在这种情况下,它将使用RawContact Aggregation 合并两个联系人。 应用程序可以通过AggregationExceptions 控制此过程,其中应用程序可以声明“将这两个 RawContacts 分开”或“将这两个 RawContacts 合并”,而不管该算法如何。

    所以你解释的是正常行为,你原来的名字应该仍然保留在 RawContacts 中。

    无论如何,我都不建议尝试使用 ID + 名称来查找 RawContact(即在您的选择子句中),而应该只使用 RawContact ID。 如果您找不到 RawContact ID,这仍然不意味着您应该创建一个新 ID,而是使用您需要存储在您的应用程序中的 RawContact 的 lookupUri,并从中获取一个可能的新 ID,在方式similar to the approach suggested for Contacts

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多