0

I have created a custom account and i have added few contacts to that account. So now i want to remove the account from those contacts. I'm Googling since few weeks but i'm not able to find anything. If anyone knows how to do then please help me out.

Thanks.

Ram
  • 7
  • 4

2 Answers2

0

Note: You should modify RawContacts, not Contacts. Contacts are simply joined RawContacts, and RawContacts are created on an account.

Not sure it's possible, since RawContacts need to belong to exactly one account. You can try looping over those RawContacts, and change the values at account_type and account_name to some other valid and existing account.

it has a chance of succeeding, but I'm not sure you can count on it for all Android versions / devices.

marmor
  • 27,641
  • 11
  • 107
  • 150
  • If i try to delete **RawContacts** then whole contacts is getting deleted :( – Ram Sep 26 '16 at 07:48
  • are you trying to delete those RawContacts or change their account? You can easily delete them manually by deleting the accounts in phone's settings > accounts > custom account > remove account. – marmor Sep 26 '16 at 08:23
  • I don't want to delete all contacts. I just want to delete few or may be one single contact. – Ram Sep 26 '16 at 09:26
  • are you talking about deleting it manually? if so, simply open that contact in your contacts app, click edit > menu > separate. Now you'll get each RawContact in its own Contact, delete the one you don't want, and re-merge the rest. – marmor Sep 26 '16 at 09:44
  • I solved my problem. Thanks for your response @marmor – Ram Sep 28 '16 at 06:12
0

The below code solved my problem :)

    String selection = ContactsContract.RawContacts._ID+ "=?";
    String selectionargs[] = { String.valueOf(rawContactId) }; //Get rawContactId    

    int deletedRawContacts = context.getContentResolver().delete(ContactsContract.RawContacts.CONTENT_URI.buildUpon()
                                .appendQueryParameter(ContactsContract.RawContacts.ACCOUNT_NAME, ACCOUNT_NAME)
                                .appendQueryParameter(ContactsContract.RawContacts.ACCOUNT_TYPE, ACCOUNT_TYPE)
                                .appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, "true").build(),
                                 selection,selectionargs);
    System.out.println("No. of contacts deleted are " + deletedRawContacts);

Refer : https://stackoverflow.com/a/8692909/3142192

Community
  • 1
  • 1