如何从联系人列表中永久删除联系人号码?

时间:2015-04-08 12:12:18

标签: android cursor

我想从联系人列表中永久删除联系人。 我正在使用以下代码。

public boolean deleteContact(String phone, String name) {
    System.out.println("name :: "+name +"  "+phone);
    Uri contactUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
            Uri.encode(phone));
    Cursor cur = context.getContentResolver().query(contactUri, null, null,
            null, null);
    try {
        if (cur.moveToFirst()) {
            do {
                if (cur.getString(
                        cur.getColumnIndex(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);
                    int i = context.getContentResolver().delete(uri, null, null);
                    System.out.println("i :::: "+i);
                    return true;
                }

            } while (cur.moveToNext());
        }

    } catch (Exception e) {
        System.out.println(e.getStackTrace());
    }
    return false;
}

在我的移动联系人中,我有一个名为“SIRI”的联系人,其中有两个手机号码。上面的代码删除了这两个数字。我想只删除选中的数字而不是两个数字。

1 个答案:

答案 0 :(得分:0)

如果您只想删除所选的电话号码,请尝试以下操作:

Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phone));
resolver.delete(uri, null, null);

更容易一气呵成:

Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;

String selections =  ContactsContract.CommonDataKinds.Phone.NUMBER + " = ' + phone + ' AND " +  ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME.toLowerCase() + " = ' + name.toLowerCase() + '";

int res = contentResolver.delete(uri, selections, null);