How to remove a contact programmatically in android

前端 未结 7 1349
梦毁少年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:22

    ContentResolver cr = getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
            null, null, null, null);
    while (cur.moveToNext()) {
        try{
            String lookupKey = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
            Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey);
            System.out.println("The uri is " + uri.toString());
            cr.delete(uri, null, null);
        }
        catch(Exception e)
        {
            System.out.println(e.getStackTrace());
        }
    }
    

    I have used this code to delete contacts.It will delete sim contacts as well phone contacts or only contacts stored in phone storage.

提交回复
热议问题