How to remove a contact programmatically in android

前端 未结 7 1313
梦毁少年i
梦毁少年i 2020-11-28 08:49

I try the following code to remove contact with a specified number:

private void removeContact(Context context, String phone) {
    //context.getContentResol         


        
相关标签:
7条回答
  • 2020-11-28 09:30

    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()
                    }
                }
    }
    
    0 讨论(0)
提交回复
热议问题