我使用lookup_key读取的内容是删除联系人的最佳方式。即使编辑了联系人,每个联系人都有一个唯一的查找密钥,而例如按姓名或号码删除等...您可能正在删除多个条目。
假设我的电话簿或SIM卡中有联系人的lookup_key:
String thelookupkey = 1393i2f.3789r504-2927292F4D4D2F35274949374B.2537i1272844629.1728i108
如何从电话簿中删除此联系人。我知道它类似于下面的内容,但不确定确切的语法(同样,也不想在试验时破坏我的电话簿)
public void deletecontactbutton(View view) {
context.getContentResolver().delete(ContactsContract.Contacts.CONTENT_LOOKUP_URI,
null, thelookupkey);
}
答案 0 :(得分: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();
}
}