【发布时间】:2014-11-26 07:32:14
【问题描述】:
大家。
我正在使用 AbstractAccountAuthenticator 实现一个帐户身份验证器,我需要在函数 getAuthToken 中调用一个异步方法来对用户进行身份验证。 我的代码是这样的:
public class AccountAuthenticator extends AbstractAccountAuthenticator {
...
@Override
public Bundle getAuthToken( final AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options )
throws NetworkErrorException
{
final AccountManager accountManager = AccountManager.get(context);
String authToken = accountManager.peekAuthToken( account, authTokenType );
// !!!!
if( TextUtils.isEmpty(authToken) ) {
<<call asynchronous method to acquire token>>
return null;
}
// !!!!
final Bundle result = new Bundle();
result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type);
result.putString(AccountManager.KEY_AUTHTOKEN, authToken);
return result;
}
...
}
根据 Google 的“getAuthToken”方法文档:
如果要通过响应返回结果,则返回 Bundle 结果或 null。
结果将包含:
• AccountManager.KEY_INTENT,或
• AccountManager.KEY_ACCOUNT_NAME、AccountManager.KEY_ACCOUNT_TYPE 和 AccountManager.KEY_AUTHTOKEN,或
• AccountManager.KEY_ERROR_CODE 和AccountManager.KEY_ERROR_MESSAGE 表示错误
而且我需要返回 null,因为身份验证器方法是异步的,但是根据文档,我如何通过 'response' 参数返回 Bundle?
谢谢大家,对不起我的英语。
【问题讨论】:
标签: android asynchronous token android-volley accountmanager