【问题标题】:Android - Trouble deleting only contacts with a specified noteAndroid - 无法仅删除具有指定备注的联系人
【发布时间】:2015-08-30 09:30:44
【问题描述】:

我正在开发一个创建随机联系人的项目,但我无法实现删除功能。我已经通过

标记了每个生成的联系人
        ops.add(ContentProviderOperation
            .newInsert(ContactsContract.Data.CONTENT_URI)
            .withValueBackReference(Data.RAW_CONTACT_ID,
                    rawContactInsertIndex)
            .withValue(Data.MIMETYPE, Note.CONTENT_ITEM_TYPE)
            .withValue(Note.NOTE, note)
            .build());

现在要删除我需要使用类似于我在这里找到的联系人的联系人

Uri contactUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
                           Uri.encode(phoneNumber));
       Cursor cur = ctx.getContentResolver().query(contactUri, null, null,
                       null, null);
       try {
           if (cur.moveToFirst()) {
               do {
                  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());
       }
       return false;
}

我相信上面的代码和我正在搜索的代码之间的唯一区别是上面的代码使用电话号码删除联系人,但出于我的目的,我需要通过联系人的备注字段删除。

【问题讨论】:

标签: android contacts android-contacts contactscontract


【解决方案1】:

解决了我的问题..我对 java 太陌生,并尝试使用 '==' 而不是 stringObject.equals() 比较字符串。

这里有一个小功能,可以根据联系人的字符串删除联系人,以防有人需要类似的东西。

public void deleteContacts(String contactNote){
    ContentResolver cr = getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
            null, null, null, null);
    String note;
    while (cur.moveToNext()) {
        note = "";
        try{

            String lookupKey = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
            String noteWhere = ContactsContract.Contacts.LOOKUP_KEY + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
            String[] noteWhereParams = new String[]{lookupKey,
                    ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE};
            Cursor noteCur = cr.query(ContactsContract.Data.CONTENT_URI, null, noteWhere, noteWhereParams, null);
            if (noteCur.moveToFirst()) {
                note = noteCur.getString(noteCur.getColumnIndex(ContactsContract.CommonDataKinds.Note.NOTE));
                if(note.equals(contactNote)) {
                        Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey);
                        cr.delete(uri, null, null);
                }
            }
            noteCur.close();
        }
        catch(Exception e)
        {
            System.out.println(e.getStackTrace());
        }
    }
    cur.close();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-30
    • 1970-01-01
    • 2016-05-12
    • 2014-02-14
    相关资源
    最近更新 更多