【问题标题】:How to delete a contact using the lookup_key of the contact如何使用联系人的lookup_key删除联系人
【发布时间】:2016-09-19 06:58:24
【问题描述】:

根据我的阅读,使用 lookup_key 是删除联系人的最佳方式。每个联系人都有一个唯一的查找键,即使联系人已被编辑,而例如按姓名或号码等删除...您可能会删除多个条目。

如果我的电话簿或 SIM 卡中有联系人的查找键:

String thelookupkey = 1393i2f.3789r504-2927292F4D4D2F35274949374B.2537i1272844629.1728i108

如何从我的电话簿中删除此联系人。我知道它类似于下面的内容,但不确定确切的语法(另外,不想在试验时毁掉我的电话簿)

public void deletecontactbutton(View view) {
    context.getContentResolver().delete(ContactsContract.Contacts.CONTENT_LOOKUP_URI,
          null, thelookupkey);

}

【问题讨论】:

  • 感谢 Rafal,但在询问之前检查了这一点。我发现一些答案非常令人困惑,并且不确定在我的电话簿上冒险。我用手机测试我的应用程序。我实际上在回答一个问题后留下了评论。
  • @Rafal 看看我的回答。

标签: android android-contacts


【解决方案1】:

我很自豪地说我写了(是的,我!!!)一个子程序,它完全符合我的要求:也就是说,使用 LOOKUP_KEY 删除一个联系人(电话、号码、与该联系人相关的所有详细信息)价值:

public void deletecontactbutton(View view) {

        String thelookupkey;
        thelookupkey = "1885r1471-29373D3D572943292D4333";

//      in our cursor query let's focus on the LOOKUP_KEY column
//      this will give us all the strings in that column
        String [] PROJECTION = new String [] {  ContactsContract.Contacts.LOOKUP_KEY };

//      we're going to query all the LOOKUP_KEY strings ; that is, the unique ids of all our contacts
//      which we can find in the LOOKUP_KEY column of the CONTENT_URI table
        Cursor cur = getContentResolver().query
                (ContactsContract.Contacts.CONTENT_URI, PROJECTION, null, null, null);

        try {
            if (cur.moveToFirst()) {
                do {
                    if
//               If a LOOKUP_KEY value is equal to our look up key string..
                 (cur.getString(cur.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY)).equalsIgnoreCase(thelookupkey)) {
//               then delete that LOOKUP_KEY value, including all associated details, like number, name etc...
                        Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, thelookupkey);
                        getContentResolver().delete(uri, null, null);
                    }

                } while (cur.moveToNext());
            }
//      deal with any errors, should they arise
        } catch (Exception e) {
            System.out.println(e.getStackTrace());
        } finally {
//            finally, close the cursor
            cur.close();
        }

    }

【讨论】:

  • 干得好。我将您的代码简化为一行:context.getContentResolver().delete(Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey), null, null);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-28
  • 2014-02-14
  • 1970-01-01
  • 2023-03-23
  • 2018-10-14
相关资源
最近更新 更多