Android: Enable imported account contacts programmatically

主宰稳场 提交于 2019-11-28 04:47:19

问题


I'm successfully importing external contacts into the newly created account using this tutorial. The account is setup to re-sync programmatically and yet, to be able to see the synced contact, - unless the contact dupe is found - I need to do Contacts->Menu->Display options->Find account->Check "All Contacts". I'm already dreading angry users enabling contact sync and not being able to see anything so the question is: is it possible to set display option for including imported account contacts programmatically? So when user opt for account creation he doesn't need to do anything else to see imported contacts?


回答1:


i had the same problem and solved it with following code via account creation:

mAccountManager.addAccountExplicitly(account, mPassword, null);
// Set contacts sync for this account.
ContentResolver.setSyncAutomatically(account,
ContactsContract.AUTHORITY, true);
ContentProviderClient client = getContentResolver().acquireContentProviderClient(ContactsContract.AUTHORITY_URI);
ContentValues cv = new ContentValues();
cv.put(Groups.ACCOUNT_NAME, account.name);
cv.put(Groups.ACCOUNT_TYPE, account.type);
cv.put(Settings.UNGROUPED_VISIBLE, true);
try {
client.insert(Settings.CONTENT_URI.buildUpon()                  .appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, "true")
.build(), cv);
} catch (RemoteException e) {...}


来源:https://stackoverflow.com/questions/5825577/android-enable-imported-account-contacts-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!