问题
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