Im using kotlin to write a function that updates SIM contact phone number, but I'm unable to update it. I always get "Contact not found or update failed". The name, oldPhoneNumber and newPhoneNumber are all correct.
fun updateSimContact(contentResolver: ContentResolver, name: String, oldPhoneNumber: String, newPhoneNumber: String) {
val values = ContentValues().apply {
put("number", newPhoneNumber)
}
val selection = "tag=? AND number=?"
val selectionArgs = arrayOf(name,oldPhoneNumber)
val updatedRows = contentResolver.update(Uri.parse("content://icc/adn"), values, selection, selectionArgs)
if (updatedRows > 0) {
// Contact updated successfully
Log.d("SimContactUpdate", "Updated $updatedRows rows")
} else {
// Contact not found or update failed
Log.d("SimContactUpdate", "Contact not found or update failed")
}
contentResolver.notifyChange(ContactsContract.Contacts.CONTENT_URI, null);
contentResolver.notifyChange(ContactsContract.RawContacts.CONTENT_URI, null);
contentResolver.notifyChange(ContactsContract.Data.CONTENT_URI, null);
contentResolver.notifyChange(Uri.parse("content://icc/adn"), null);
}
What am I missing? Why is it not updating.
I have another code that successfully updates the contact in the device, but after restarting my PHONE original old number that was stored inside the SIM card is loaded.
fun updatePhoneNumber(contentResolver: ContentResolver, contactId: String, oldPhoneNumber: String, newPhoneNumber: String, name:String) {
val selection =
"${ContactsContract.Data.CONTACT_ID} = ? AND ${ContactsContract.Data.MIMETYPE} = ? AND ${ContactsContract.CommonDataKinds.Phone.NUMBER} = ?"
val selectionArgs = arrayOf(contactId, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE, oldPhoneNumber)
// Create a ContentValues object to store the new phone number
val values = ContentValues().apply {
put(ContactsContract.CommonDataKinds.Phone.NUMBER, newPhoneNumber)
}
// Update the phone number in the Data content URI
contentResolver.update(
ContactsContract.Data.CONTENT_URI,
values,
selection,
selectionArgs
)
updateSimContact(contentResolver, name, oldPhoneNumber, newPhoneNumber)
}