I created my own Android account authenticator by extending AbstractAccountAuthenticator
and implementing addAccount()
and getAuthToken()
. Some of the methods in it are called by AccountManager
, but others are not.
This works great:
AccountManager accountManager = AccountManager.get(activity);
accountManager.addAccount(MyAccountAuthenticator.ACCOUNT_TYPE,
MyAccountAuthenticator.AUTHTOKEN_TYPE_FULL_ACCESS, null, null,
activity, callback, null);
The problem happens when I call AccountManager#getAuthToken()
in my Activity
. The AccountManager does not call the getAuthToken()
method I define in my AccountAuthenticator
. It calls some other default method that only checks for existence of an authToken
before starting the AuthenticatorActivity
.
This does not work. It does not call my getAuthToken()
method:
AccountManager accountManager = AccountManager.get(activity);
accountManager.getAuthToken(
mAccount, MyAccountAuthenticator.AUTHTOKEN_TYPE_FULL_ACCESS, null,
activity, callback, handler);
AuthenticatorService
I created my service and defined onBind()
. addAccount()
should not work otherwise.
public IBinder onBind(Intent intent) {
return intent.getAction().equals(ACTION_AUTHENTICATOR_INTENT) ? new MyAccountAuthenticator(this).getIBinder() : null;
}
EDIT: I call addAccountExplicitly
in MyAuthenticatorActivity
after the app gets an auth token back for the user.
Snippet from class MyAuthenticatorActivity extends AccountAuthenticatorActivity
:
if (getIntent().getBooleanExtra(KEY_IS_ADDING_NEW_ACCOUNT, false)) {
// Creating the account on the device and setting the auth token we recieved
accountManager.addAccountExplicitly(account, null, null);
}
addAccountExplicitly
in your activity? – FiskeAccountAuthenticatorActivity
. I've added that to an EDIT in the question. – LeptosomegetAuthToken
is that your login activity is launched, then your authenticator is being called -- Android has no other way to determine what activity to launch than what you return as an Intent. I notice you're adding the account with a null password. Perhaps your implementation short-circuits if it finds a null pass? Post the body ofgetAuthToken
from your authenticator. – FiskegetAuthToken()
is not even called when there is an existing auth token set. It bypasses the call and just returns the auth token, which is not what I expect it to do. How I interpreted the documentation, it should still call my method. I then can decide if I want to return the existing token or get a new one (I check to see if the auth token is expired before returning it). – LeptosomegetAuthToken
method works great when it is actually called, no need to see the source there. – Leptosome