【发布时间】: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