8

如何使用 Google API 将 Android 原生联系人同步到 Google 帐户。提供一些有用的链接。

4

2 回答 2

10

同步会自动发生。您可以以编程方式添加或删除联系人。但是,当且仅当用户在手机设置中启用了“同步联系人”选项时,操作系统才会自动处理同步。

但是,如果用户使用以下方式启用了同步,您可以运行一个可以调用同步过程的同步例程:

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);
        }
    }
}
于 2012-10-09T06:15:01.237 回答
0

以下也可能是一个很好的答案。它与上述类似,但默认设置应用程序使用如下代码:

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);
            }
        }
    }
}
于 2014-03-05T14:38:28.017 回答