Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/211.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何从联系人列表中永久删除联系人号码?_Android_Cursor - Fatal编程技术网

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

Android 如何从联系人列表中永久删除联系人号码?,android,cursor,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)); C

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

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”的联系人,有两个手机号码。上述代码将删除这两个数字。我只想删除选定的号码,而不是两个号码。

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

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);

如果该名称有两个数字,则表示我要删除选定的数字,而不是名称和其他数字。谢谢你的回复。