1

我想以编程方式将我的联系人从我的应用程序同步到本机 android 联系人应用程序。请帮我。提前致谢。

4

1 回答 1

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();
}
于 2012-11-22T14:27:59.773 回答