3

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.

pb2q
  • 58,613
  • 19
  • 146
  • 147
The VOYOU
  • 522
  • 4
  • 14

1 Answers1

2

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, "xxxxx@gmail.com")
        .build());

Where, com.google is type of account and xxxxx@gmail.com is your account id you want to sync with.

Thank you

Shashanth
  • 4,995
  • 7
  • 41
  • 51
The VOYOU
  • 522
  • 4
  • 14
  • If I want to sync contacts under custom account then?? I have used same coding but my added contacts are not showing under my account.. :( – SweetWisher ツ Sep 02 '13 at 09:14
  • what do you mean by custom account ? – The VOYOU Jan 09 '14 at 12:37
  • Custom account means : if I have installed ma app name XXX, group will b created as XXX in contacts and all the contacts from the server will be added to this group only. – SweetWisher ツ Jan 09 '14 at 12:43
  • So you just want to fetch contact information from your own application server and want to add in a group named as your application's name. right? – The VOYOU Jan 10 '14 at 07:42
  • please ask a question for this. i can't provide you details here just because the lack of space and it has nothing to do with this thread. – The VOYOU Jan 10 '14 at 08:46
  • @TheVOYOU how can I add google contact with it's `googleid`? – Kasim Rangwala Feb 08 '18 at 06:59
  • @SweetWisherツ I have created new post about sync with other apps. https://stackoverflow.com/questions/61454841/android-programmatically-updated-contact-is-not-syncing-with-other-apps – user8462556 Apr 27 '20 at 09:12