Custom authentication: how to get custom user data values?

ε祈祈猫儿з 提交于 2020-01-06 19:56:33

问题


I have custom authenticator. And put account information like it shown below:

final AccountManager am = AccountManager.get(AuthActivity.this);
final Bundle result = new Bundle();
final Bundle userData = new Bundle();
userData.putString(KEY_NAME, mName);
userData.putString(KEY_EMAIL, mEmail);

Account account = new Account(mLogin, vcs.getAccountType());

if (am.addAccountExplicitly(account, null, userData)) {
    result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
    result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type);
    result.putString(AccountManager.KEY_AUTHTOKEN, mAccessToken);
    result.putString(KEY_REFRESH_TOKEN, mRefreshToken);
    am.setAuthToken(account, account.type, mAccessToken);
} else {
    result.putString(AccountManager.KEY_ERROR_MESSAGE, "F@ck|n8 authenticator");
}

setAccountAuthenticatorResult(result);
setResult(RESULT_OK);
finish();

But how I can get these custom values when I need it?

I try to get it so:

tvUsername.setText(accountManager.getUserData(account, AuthActivity.KEY_NAME));
tvEmail.setText(accountManager.getUserData(account, AuthActivity.KEY_EMAIL));

What I do wrong?


回答1:


Check out Android AccountManager.getUserData() returns null. There is a known issue with addAccountExplicitly. Instead of passing the values in the Bundle, pass an empty Bundle and set the values after creating the account using setUserData(Account, String, String).

IIRC, the problem is a caching issue in the account manager service code. If you remove an account and recreate the same account right away the user data cache in not populated with the values passed in the bundle. Using setUserData works around this issue.



来源:https://stackoverflow.com/questions/34599808/custom-authentication-how-to-get-custom-user-data-values

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