如何以编程方式将您的应用程序联系人同步到Android上的本地联系人应用

时间:2012-09-24 08:46:11

标签: android sync android-contacts

我想以编程方式将我的应用中的联系人同步到原生Android联系人应用。请帮我。提前谢谢。

1 个答案:

答案 0 :(得分:3)

您可以尝试以下代码进行帐户同步

public static void requestSyncNow(final Context context) {

    new Thread(new Runnable() {
        @Override
        public void run() {
            AccountManager accountManager = AccountManager.get(context);
            Account[] accounts = accountManager.getAccounts();
            boolean isMasterSyncOn = ContentResolver.getMasterSyncAutomatically();


            for (Account account : accounts) {
                Log.d(TAG, "account=" + account);

                int isSyncable = ContentResolver.getIsSyncable(account,
                        ContactsContract.AUTHORITY);
                boolean isSyncOn = ContentResolver.getSyncAutomatically(account,
                        ContactsContract.AUTHORITY);
                Log.d(TAG, "Syncable=" + isSyncable + " SyncOn=" + isSyncOn);
                if (isSyncable > 0 /* && isSyncOn */) {
                    Log.d(TAG, "request Sync");
                    Bundle bundle = new Bundle();
                    bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
                    ContentResolver.requestSync(account, ContactsContract.AUTHORITY, bundle);
                }
            }

        }
    }, "SyncLauncher").start();
}