everybody.
I'm implementing an account authenticator using AbstractAccountAuthenticator and I need call an asynchronous method in the function getAuthToken, to authenticate a user. My code is like this:
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;
}
...
}
According to Google's documentation for the 'getAuthToken' method:
it returns a Bundle result or null if the result is to be returned via the response.
The result will contain either:
• AccountManager.KEY_INTENT
, or
• AccountManager.KEY_ACCOUNT_NAME
, AccountManager.KEY_ACCOUNT_TYPE
, and AccountManager.KEY_AUTHTOKEN
, or
• AccountManager.KEY_ERROR_CODE
and AccountManager.KEY_ERROR_MESSAGE
to indicate an error
And I need to return null because the authenticator method is asynchronous, but how I return the Bundle via the 'response' parameter, according to the documentation?
Thanks for all, and sorry my english.
Yes, I found the solution. You must use the 'response' parameter to return the results. Below is the source that I use in my application. I hope this can help.