【问题标题】:how to sync phone contacts into gmail in android programmatically using google contacts api如何使用谷歌联系人api以编程方式将手机联系人同步到android中的gmail
【发布时间】:2012-09-29 09:40:05
【问题描述】:

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

【问题讨论】:

标签: android google-api sync android-contacts


【解决方案1】:

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

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

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);
        }
    }
}

【讨论】:

  • 您还需要在清单中添加<uses-permission android:name="android.permission.GET_ACCOUNTS" />
【解决方案2】:

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

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);
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 2011-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多