在我的应用程序中,我需要从电话地址簿中删除特定的 CONTACT ,但我只删除了特定号码而不是整个联系人,所以请帮助我。
提前致谢。
答案 0 :(得分:7)
要删除所有联系人,请使用以下代码;
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
while (cur.moveToNext()) {
try{
String lookupKey = cur.getString(cur.getColumnIndex(
ContactsContract.Contacts.LOOKUP_KEY));
Uri uri = Uri.withAppendedPath(ContactsContract.
Contacts.CONTENT_LOOKUP_URI, lookupKey);
System.out.println("The uri is " + uri.toString());
cr.delete(uri, null, null);
}
catch(Exception e)
{
System.out.println(e.getStackTrace());
}
}
删除任何特定联系人修改查询
cr.delete(uri, null, null);
希望它有所帮助!!
答案 1 :(得分:1)
也可以试试这段代码
private void deletePhoneNumber(Uri peopleUri, String numberToDelete) {
Uri.Builder builder = peopleUri.buildUpon();
builder.appendEncodedPath(People.Phones.CONTENT_DIRECTORY);
Uri phoneNumbersUri = builder.build();
String[] mPhoneNumberProjection = { People.Phones._ID, People.Phones.NUMBER };
Cursor cur = resolver.query(phoneNumbersUri, mPhoneNumberProjection,
null, null, null);
ArrayList<String> idsToDelete = new ArrayList<String>();
if (cur.moveToFirst()) {
final int colId = cur.getColumnIndex(People.Phones._ID);
final int colNumber = cur.getColumnIndex(People.Phones.NUMBER);
do {
String id = cur.getString(colId);
String number = cur.getString(colNumber);
if(number.equals(numberToDelete))
idsToDelete.add(id);
} while (cur.moveToNext());
}
cur.close();
for (String id : idsToDelete) {
builder.encodedPath(People.Phones.CONTENT_DIRECTORY + "/" + id);
phoneNumbersUri = builder.build();
resolver.delete(phoneNumbersUri, "1 = 1", null);
}
}
希望它有所帮助!!