I try the following code to remove contact with a specified number:
private void removeContact(Context context, String phone) {
//context.getContentResol
This code works perfect for me to remove the contact from its identifier (ContactsContract.Contacts._ID)
Telephone registration for all numbers of that contact must be removed independently
fun deleteContactById(id: String) {
val cr = context.contentResolver
val cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null)
cur?.let {
try {
if (it.moveToFirst()) {
do {
if (cur.getString(cur.getColumnIndex(ContactsContract.PhoneLookup._ID)) == id) {
val lookupKey = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY))
val uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey)
cr.delete(uri, null, null)
break
}
} while (it.moveToNext())
}
} catch (e: Exception) {
println(e.stackTrace)
} finally {
it.close()
}
}
}