android以编程方式删除特定联系人

前端之家收集整理的这篇文章主要介绍了android以编程方式删除特定联系人前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
现在,这段代码正在运行
ops.add(ContentProviderOperation .newDelete(ContactsContract.Data.CONTENT_URI).withSelection(ContactsContract.Data.RAW_CONTACT_ID“=?”,new String [] {sid}).build());

但它创建了未知记录,似乎是已删除的联系人.我需要一些能让它正常工作的东西吗?

解决方法

我这样做了:
public static boolean deleteContact(Context ctx,String phone,String name) {
    Uri contactUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,Uri.encode(phone));
    Cursor cur = ctx.getContentResolver().query(contactUri,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);
                    ctx.getContentResolver().delete(uri,null);
                    return true;
                }

            } while (cur.moveToNext());
        }

    } catch (Exception e) {
        System.out.println(e.getStackTrace());
    }
    return false;
}
原文链接:https://www.f2er.com/android/310141.html

猜你在找的Android相关文章