Remove custom account ACCOUNT_TYPE from contacts

1.1k Views Asked by At

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.

2

There are 2 best solutions below

0
Shashidhar Mayannavar On BEST ANSWER

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

5
marmor On

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.