How to delete a particular contact using contact id?

前端 未结 4 359
迷失自我
迷失自我 2020-12-17 06:39

I am trying to delete a particular contact from phone. I can delete the full contact. How to delete a particular contact using contact id. I want to delete the full datas in

相关标签:
4条回答
  • 2020-12-17 06:54
    public void deleteContact(Context context, String localContactId)
    {
        ContentResolver cr = context.getContentResolver();
        String rawWhere = ContactsContract.Contacts._ID + " = ? ";
        String[] whereArgs1 = new String[]{localContactId};
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
                null, rawWhere, whereArgs1, null);
    
        if(cur != null && cur.getCount() > 0) {
            while (cur.moveToNext()) {
                try{
                    String lookupKey = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
                    Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey);
                    cr.delete(uri, null, null);
    
                }
                catch(Exception e)
                {
                    System.out.println(e.getStackTrace());
                }
            }
        }
        if(cur != null)
            cur.close();
    }
    
    0 讨论(0)
  • 2020-12-17 06:58

    Using Contacts.CONTENT_LOOKUP_URI is not needed if you have contactId. In fact I experimented problems deleting some contacts using it.

    The correct way if you have contactId is to directly use ContactsContract.Contacts.CONTENT_URI:

        Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI,contactId);
        int deleted = context.getContentResolver().delete(uri,null,null);
        return deleted>0;
    
    0 讨论(0)
  • 2020-12-17 06:58

    Add this in manifest

    <uses-permission android:name="android.permission.WRITE_CONTACTS" />
    

    Delete contact by Id code

    private void deleteContactById(long id) {
        Cursor cur = resolver.query(ContactsContract.Contacts.CONTENT_URI, null, ContactsContract.Contacts._ID + "="
                + id, null, null);
        if (cur != 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);
                    resolver.delete(uri, ContactsContract.Contacts._ID + "=" + id, null);
                } catch (Exception e) {
                    Log.e(TAG, "deleteContactById: ", e);
                }
            }
            cur.close();
        }
    }
    
    0 讨论(0)
  • 2020-12-17 07:10

    try the following code:

                final ArrayList ops = new ArrayList();
            final ContentResolver cr = getContentResolver();
            ops.add(ContentProviderOperation
                    .newDelete(ContactsContract.RawContacts.CONTENT_URI)
                    .withSelection(
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                    + " = ?",
                            new String[] { selected_contact_IDfromlist })
                    .build());
            AlertDialog alertDialog = new AlertDialog.Builder(this).create();
            alertDialog.setTitle("Delete This Contact!");
            alertDialog.setMessage("Are you Sure you want to delete this contact?");
            alertDialog.setButton(getString(R.string.callLog_delDialog_yes), new DialogInterface.OnClickListener() {    // DEPRECATED
              public void onClick(DialogInterface dialog, int which) {
                try {
                    cr.applyBatch(ContactsContract.AUTHORITY, ops);
                    background_process();
                    ops.clear();
                } catch (OperationApplicationException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
    
                } catch (RemoteException e) {
                    // System.out.println(" length :"+i);
                }
                    return;
            } }); 
            alertDialog.setButton2(getString(R.string.callLog_delDialog_no), (DialogInterface.OnClickListener)null);    // DEPRECATED
            try {
                alertDialog.show();
            }catch(Exception e) {
                //              Log.e(THIS_FILE, "error while trying to show deletion yes/no dialog");
            }
    
    0 讨论(0)
提交回复
热议问题