How to delete a particular contact using contact i

2019-01-26 02:28发布

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 including firstname, last name ,phone number, email, notes, etc...

4条回答
我欲成王,谁敢阻挡
2楼-- · 2019-01-26 02:52

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;
查看更多
成全新的幸福
3楼-- · 2019-01-26 03:06

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");
        }
查看更多
一纸荒年 Trace。
4楼-- · 2019-01-26 03:11

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();
    }
}
查看更多
Bombasti
5楼-- · 2019-01-26 03:11
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();
}
查看更多
登录 后发表回答