Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update Android contact company?

I recently created a Sync adapter for my app, It will sync contacts I am getting via a web request with the contacts in the phone. I have no problem adding the contact, however I cannot get the contact information to correctly update when contact information has changed. For example the Company Name field on the contact. Here is some example queries I have tried that did not work or only partially worked(ie - some contacts updated but not correctly):

        ContentValues values = new ContentValues();
    values.put(ContactsContract.CommonDataKinds.Organization.COMPANY, "New Company");
context.getContentResolver().update(Uri.parse("content://com.android.contacts/data/"), values, BaseColumns._ID + "=?", new String[] { String.valueOf(id) } );

I have also tried doing this in batch as suggested by the android documentation:

    builder = ContentProviderOperation
            .newUpdate(ContactsContract.Data.CONTENT_URI);
    builder.withSelection(BaseColumns._ID + " =?", new String[]{String.valueOf(id)});

    builder.withValue(
            ContactsContract.CommonDataKinds.Organization.COMPANY,
            "New Company Name!");
    operationList.add(builder.build());

I have read the ContactContracts Documentation and originally was following this tutorial. I also checked into the AuthenticatorActivity example in the api's to no avail. Any help is greatly appreciated.

like image 600
ninjasense Avatar asked Jan 20 '23 01:01

ninjasense


1 Answers

After spending an exhausting amount of time trying to figure out the correct query, I believe I have found the answer. It looks like i needed to change the BaseColumns._ID to ContactsContract.Data.CONTACT_ID and for each update I made, I also had to supply the mime-type also I did not see this anywhere in the android documentation. Much help was found on this write-up: Working With Android Contacts

        String orgWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?"; 
    String[] orgWhereParams = new String[]{String.valueOf(id), 
        ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE}; 
    operationList
    .add(ContentProviderOperation
            .newUpdate(ContactsContract.Data.CONTENT_URI)
            .withSelection(orgWhere, orgWhereParams)
            .withValue(
                    ContactsContract.CommonDataKinds.Organization.DATA,
                    guCon.getCompany()).build());
like image 74
ninjasense Avatar answered Jan 31 '23 02:01

ninjasense