How to remove a contact programmatically in android

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

    A better way to delete a contact is by removing all raw contacts using contact id

        final ArrayList ops = new ArrayList();
        final ContentResolver cr = getContentResolver();
        ops.add(ContentProviderOperation
                .newDelete(ContactsContract.RawContacts.CONTENT_URI)
                .withSelection(
                        ContactsContract.RawContacts.CONTACT_ID
                                + " = ?",
                        new String[] { allId.get(i) })
                .build());
    
            try {
                cr.applyBatch(ContactsContract.AUTHORITY, ops);
                int deletecontact = serialList.get(allId.get(i));
    
            } catch (RemoteException e) {
                e.printStackTrace();
            } catch (OperationApplicationException e) {
                e.printStackTrace();
            }
            //background_process();
            ops.clear();
        }
    

    and dont forget to add permissions

            
            
    

提交回复
热议问题