-1

So basically i have a function which deletes the contact from the phone:

String[] args = new String[]{number};
    try {

        ArrayList ops = new ArrayList();
        // if id is raw contact id
        ops.add(ContentProviderOperation.newDelete(ContactsContract.RawContacts.CONTENT_URI).withSelection(ContactsContract.RawContacts._ID + "=?", args).build());
        // if id is contact id
       // ops.add(ContentProviderOperation.newDelete(ContactsContract.RawContacts.CONTENT_URI).withSelection(ContactsContract.RawContacts.CONTACT_ID + "=?", args).build());
        getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
        Toast.makeText(this, "Contact Deleted", Toast.LENGTH_SHORT).show();


    } catch (Exception e) {

        Log.e("$$$$", "ERRORDELETE#133");
    }

I tried both options, if raw contact or if contact id, both doesn't work/

When i run the APP and the function activates the 'Contact Deleted' Shows and in the log:

05-24 15:40:16.444 6540-6540/com.assistme.meirovichomer.assistme E/ViewRootImpl: sendUserActionEvent() mView == null
05-24 15:40:16.464 6540-6679/com.assistme.meirovichomer.assistme V/RenderScript: Application requested CPU execution
05-24 15:40:16.474 6540-6679/com.assistme.meirovichomer.assistme V/RenderScript: 0xa14a7e00 Launching thread(s), CPUs 4

I am not sure if i just don't understand the log or i missed anything but when i enter the contact list on the phone the contact is still no it, therefore wasn't deleted. Would love to get some help, Thank you very much in advance!

OHM
  • 47
  • 1
  • 7
  • post the total log cat – Avinash Roy May 24 '17 at 12:45
  • try using a solutions from this post https://stackoverflow.com/questions/527216/how-to-remove-a-contact-programmatically-in-android – Ivan86 May 24 '17 at 12:50
  • I have checked the other solutions from this and other stackoverflow posts and could not find an answer that would work, the one below indeed helped me, thank you guys for your help. – OHM May 27 '17 at 11:11

1 Answers1

2

try this.

for delete.

public static void deleteContact(ContentResolver contactHelper, String 
number) {
ArrayList<ContentProviderOperation> ops = new 
ArrayList<ContentProviderOperation>();
String[] args = new String[] { String.valueOf(getContactID(contactHelper, 
number))};
ops.add(ContentProviderOperatio.newDelete(RawContacts.CONTENT_URI).withSelecti
  on(RawContacts.CONTACT_ID + "=?", args).build());
 try {
  contactHelper.applyBatch(ContactsContract.AUTHORITY, ops);
  } catch (RemoteException e) {
  e.printStackTrace();
 } catch (OperationApplicationException e) {
e.printStackTrace();
}
}

and getid

private static long getContactID(ContentResolver contactHelper,String 
number) {
Uri contactUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, 
Uri.encode(number));
String[] projection = { PhoneLookup._ID };
Cursor cursor = null;
try {
cursor = contactHelper.query(contactUri, projection, null, null,null);
if (cursor.moveToFirst()) {
int personID = cursor.getColumnIndex(PhoneLookup._ID);
return cursor.getLong(personID);
}
return -1;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cursor != null) {
cursor.close();
cursor = null;
}
}
return -1;
}
Majid Ali
  • 485
  • 6
  • 17