The VOYOU
The VOYOU

Reputation: 524

Android :Programmatically created contact is not syncing with Gmail Account

I can create a new contact with in My App using:

ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
int rawContactInsertIndex = ops.size();
ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
        .withValue(RawContacts.ACCOUNT_TYPE, null)
        .withValue(RawContacts.ACCOUNT_NAME, null).build());
ops.add(ContentProviderOperation
        .newInsert(ContactsContract.Data.CONTENT_URI)
        .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID,
                rawContactInsertIndex)
        .withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
        .withValue(Phone.NUMBER, "9X-XXXXXXXXX").build());
ops.add(ContentProviderOperation
        .newInsert(Data.CONTENT_URI)
        .withValueBackReference(Data.RAW_CONTACT_ID,
                rawContactInsertIndex)
        .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
        .withValue(StructuredName.DISPLAY_NAME, "Mike Sullivan")
        .build());
try {
    ContentProviderResult[] res = getContentResolver().applyBatch(
            ContactsContract.AUTHORITY, ops);
} catch (RemoteException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (OperationApplicationException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

It Works fine and I can see contact is been created but when I sync my contacts with my Gmail Account, I can't see that there in Gmail Contacts.

note : If I creates a contact using device's native contact app and sync with Gmail, I can see that contact with in Gmail.

Upvotes: 3

Views: 4223

Answers (1)

The VOYOU
The VOYOU

Reputation: 524

Some how I figured it out ...

You could use

ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
        .withValue(RawContacts.ACCOUNT_TYPE, "com.google")
        .withValue(RawContacts.ACCOUNT_NAME, "[email protected]")
        .build());

Where, com.google is type of account and [email protected] is your account id you want to sync with.

Thank you

Upvotes: 2

Related Questions