Search code examples
androidandroid-contactscontactscontractandroid-accountrawcontacts

Remove custom account ACCOUNT_TYPE from contacts


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.


Solution

  • 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