我正在尝试从电话中删除特定联系人。我可以删除完整的联系人。如何使用联系人 ID 删除特定联系人。我想删除完整的数据,包括名字、姓氏、电话号码、电子邮件、笔记等...
问问题
5448 次
4 回答
7
如果您有contactId,则不需要使用Contacts.CONTENT_LOOKUP_URI 。事实上,我尝试使用它删除一些联系人的问题。
如果您有contactId,正确的方法是直接使用ContactsContract.Contacts.CONTENT_URI:
Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI,contactId);
int deleted = context.getContentResolver().delete(uri,null,null);
return deleted>0;
于 2016-08-08T14:24:25.867 回答
6
试试下面的代码:
final ArrayList ops = new ArrayList();
final ContentResolver cr = getContentResolver();
ops.add(ContentProviderOperation
.newDelete(ContactsContract.RawContacts.CONTENT_URI)
.withSelection(
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = ?",
new String[] { selected_contact_IDfromlist })
.build());
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Delete This Contact!");
alertDialog.setMessage("Are you Sure you want to delete this contact?");
alertDialog.setButton(getString(R.string.callLog_delDialog_yes), new DialogInterface.OnClickListener() { // DEPRECATED
public void onClick(DialogInterface dialog, int which) {
try {
cr.applyBatch(ContactsContract.AUTHORITY, ops);
background_process();
ops.clear();
} catch (OperationApplicationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RemoteException e) {
// System.out.println(" length :"+i);
}
return;
} });
alertDialog.setButton2(getString(R.string.callLog_delDialog_no), (DialogInterface.OnClickListener)null); // DEPRECATED
try {
alertDialog.show();
}catch(Exception e) {
// Log.e(THIS_FILE, "error while trying to show deletion yes/no dialog");
}
于 2012-10-03T16:13:16.960 回答
2
在清单中添加这个
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
按 ID 码删除联系人
private void deleteContactById(long id) {
Cursor cur = resolver.query(ContactsContract.Contacts.CONTENT_URI, null, ContactsContract.Contacts._ID + "="
+ id, null, null);
if (cur != 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);
resolver.delete(uri, ContactsContract.Contacts._ID + "=" + id, null);
} catch (Exception e) {
Log.e(TAG, "deleteContactById: ", e);
}
}
cur.close();
}
}
于 2012-10-04T01:16:35.680 回答
1
public void deleteContact(Context context, String localContactId)
{
ContentResolver cr = context.getContentResolver();
String rawWhere = ContactsContract.Contacts._ID + " = ? ";
String[] whereArgs1 = new String[]{localContactId};
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, rawWhere, whereArgs1, null);
if(cur != null && cur.getCount() > 0) {
while (cur.moveToNext()) {
try{
String lookupKey = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey);
cr.delete(uri, null, null);
}
catch(Exception e)
{
System.out.println(e.getStackTrace());
}
}
}
if(cur != null)
cur.close();
}
于 2016-07-01T06:32:16.460 回答