How to synchronize phone contacts in gmail in android programmatically using google contacts api

How to sync native Android contacts with your Google Account using the Google APIs. Provide some useful links.

+8
android google-api sync android-contacts
source share
2 answers

Synchronization happens automatically. You can add or remove contacts programmatically. But synchronization is processed by the OS automatically, if and only if the user has enabled the option "sync conatcts" in the phone settings.

However, you can start the synchronization procedure, which can trigger the synchronization process if the user can use synchronization using the following:

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); } } } 
+9
source share

The following may also be a good answer. Its similar to the above, but the default settings application uses the code something like this:

 private void requestSyncForAccounts() { SyncAdapterType[] syncAdapters = ContentResolver.getSyncAdapterTypes(); Bundle extras = new Bundle(); extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true); Account[] accounts = AccountManager.get(PeopleActivity.this).getAccounts(); for (Account account : accounts) { for (int j = 0; j < syncAdapters.length; j++) { SyncAdapterType sa = syncAdapters[j]; if (ContentResolver.getSyncAutomatically(account, sa.authority)) { ContentResolver.requestSync(account, sa.authority, extras); } } } } 
0
source share

All Articles