我想以编程方式将我的应用中的联系人同步到原生Android联系人应用。请帮我。提前谢谢。
答案 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();
}