Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to sync phone contacts into gmail in android programmatically using google contacts api

How to synchronize Android native contacts into Google account using Google API. Provide some useful links.

like image 941
AndroidRaji Avatar asked Oct 09 '12 05:10

AndroidRaji


People also ask

How do I access my Google contacts API?

To access personal contacts: https://www.googleapis.com/auth/contacts. To access directory information: https://www.googleapis.com/auth/directory.readonly.


1 Answers

The syncing happens automatically. You can add or delete contacts programatically. But the syncing is handled by the OS automatically if and only if the user has enabled 'sync conatcts' option in phone settings.

You can, however run a sync routine that can call the syncing process if syncing is enabled by the user using something like this:

private void requestSync()
{
    AccountManager am = AccountManager.get(this);
    Account[] accounts = am.getAccounts();

    for (Account account : accounts)
    {
        int isSyncable = ContentResolver.getIsSyncable(account, ContactsContract.AUTHORITY);

        if (isSyncable > 0)
        {
            Bundle extras = new Bundle();
            extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
            ContentResolver.requestSync(accounts[0], ContactsContract.AUTHORITY, extras);
        }
    }
}
like image 97
Anup Cowkur Avatar answered Sep 18 '22 14:09

Anup Cowkur