如何从电话簿中删除特定联系人

时间:2017-05-30 07:07:55

标签: android

 Uri contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phone));
    Cursor cur = ctx.getContentResolver().query(contactUri, null, null, null, null);
    try {
        if (cur.moveToFirst()) {
            do {
                if (cur.getString(cur.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME)).equalsIgnoreCase(name)) {
                    String lookupKey = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
                    Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey);
                    ctx.getContentResolver().delete(uri, null, null);
                }
            } while (cur.moveToNext());
        }

    } catch (Exception e) {
        System.out.println(e.getStackTrace());
    } finally {
        cur.close();
    }

1 个答案:

答案 0 :(得分:0)

尝试以下方法:

private static final Uri CONTACT_CONTENT_URI = ContactsContract.Contacts.CONTENT_URI;    

public static boolean deleteContact(final Context context, final String contactId) {
    if (null != context) {
        final Uri uri = Uri.withAppendedPath(CONTACT_CONTENT_URI, contactId);
        final ContentResolver contentResolver = context.getContentResolver();
        try {
            final int deletedRowCount = contentResolver.delete(uri, null, null);
            return deletedRowCount > 0;
        } catch (final Exception e) {
            Log.e(TAG, e.toString());
        }
    }
    return false;
}