How To Delete A Particular Contact Using Contact Id?
I am trying to delete a particular contact from phone. I can delete the full contact. How to delete a particular contact using contact id. I want to delete the full datas including
Solution 1:
Using Contacts.CONTENT_LOOKUP_URI is not needed if you have contactId. In fact I experimented problems deleting some contacts using it.
The correct way if you have contactId is to directly use ContactsContract.Contacts.CONTENT_URI:
Uriuri= Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI,contactId);
intdeleted= context.getContentResolver().delete(uri,null,null);
return deleted>0;
Solution 2:
try the following code:
finalArrayListops=newArrayList();
finalContentResolvercr= getContentResolver();
ops.add(ContentProviderOperation
.newDelete(ContactsContract.RawContacts.CONTENT_URI)
.withSelection(
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = ?",
newString[] { selected_contact_IDfromlist })
.build());
AlertDialogalertDialog=newAlertDialog.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), newDialogInterface.OnClickListener() { // DEPRECATEDpublicvoidonClick(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); // DEPRECATEDtry {
alertDialog.show();
}catch(Exception e) {
// Log.e(THIS_FILE, "error while trying to show deletion yes/no dialog");
}
Solution 3:
Add this in manifest
<uses-permissionandroid:name="android.permission.WRITE_CONTACTS" />
Delete contact by Id code
privatevoiddeleteContactById(long id) {
Cursorcur= resolver.query(ContactsContract.Contacts.CONTENT_URI, null, ContactsContract.Contacts._ID + "="
+ id, null, null);
if (cur != null) {
while (cur.moveToNext()) {
try {
StringlookupKey= cur.getString(cur
.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
Uriuri= 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();
}
}
Solution 4:
publicvoiddeleteContact(Context context, String localContactId)
{
ContentResolver cr = context.getContentResolver();
String rawWhere = ContactsContract.Contacts._ID + " = ? ";
String[] whereArgs1 = newString[]{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();
}
Post a Comment for "How To Delete A Particular Contact Using Contact Id?"