我通过扩展AbstractAccountAuthenticator贯彻addAccount()和getAuthToken().一些在它的方法是由AccountManager,但其他人都没有。
AccountManager#addAccount()
的AccountManager的AccountManager = AccountManager.get(活动);
accountManager.addAccount(MyAccountAuthenticator.ACCOUNT_TYPE,
MyAccountAuthenticator.AUTHTOKEN_TYPE_FULL_ACCESS,NULL,NULL,
活动中,回调,空);
当我称之为AccountManager#getAuthToken()在我的活动
。该的AccountManager不会调用 getAuthToken()
方法,我在 AccountAuthenticator
定义。它要求一些其他的默认方法只检查是否存在的的authToken
开始之前 AuthenticatorActivity
。
getAuthToken()
方法:AccountManager#getAuthToken()
的AccountManager的AccountManager = AccountManager.get(活动);
accountManager.getAuthToken(
mAccount,MyAccountAuthenticator.AUTHTOKEN_TYPE_FULL_ACCESS,空,
活动中,回调,处理程序);
我创造了我的服务,并定义 onBind()
。 addAccount()
不应该工作,否则。
公开的IBinder onBind(意向意图){
返回intent.getAction()。等于(ACTION_AUTHENTICATOR_INTENT)?新MyAccountAuthenticator(本).getIBinder():空;
}
编辑:我称之为 addAccountExplicitly
在 MyAuthenticatorActivity
应用程序后,得到一个身份验证令牌回给用户。
从类摘录 MyAuthenticatorActivity延伸AccountAuthenticatorActivity
:
如果(getIntent()。getBooleanExtra(KEY_IS_ADDING_NEW_ACCOUNT,FALSE)){
//创建设备上的帐户,并设置我们:收到的身份验证令牌
accountManager.addAccountExplicitly(帐户,NULL,NULL);
}
解决方案
您的评论茅塞顿开极大 - 如果你设置了身份验证令牌的帐户,那么你的 getAuthToken
方法将不会被调用,直到令牌无效。你通常通过调用invalidateAuthToken当接收到401或403或你有你的Web服务。
从的Javadoc getAuthToken
方法:
如果pviously生成身份验证令牌一个$ P $是缓存此帐户和类型,然后返回。否则,如果一个保存的密码是可用的,它被发送到服务器,以生成一个新的身份验证令牌。否则,系统将提示用户输入密码。
由于您的令牌在缓存中,则直接返回,你的身份验证是没有咨询。
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.
AccountManager#addAccount()
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
.
getAuthToken()
method:AccountManager#getAuthToken()
AccountManager accountManager = AccountManager.get(activity);
accountManager.getAuthToken(
mAccount, MyAccountAuthenticator.AUTHTOKEN_TYPE_FULL_ACCESS, null,
activity, callback, handler);
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);
}
解决方案
Your comment cleared things up immensely -- if you set the auth token for the account, then your getAuthToken
method will not be called until the token is invalidated. You generally do this by calling invalidateAuthToken upon receiving a 401 or 403 or what have you from the web service.
From the Javadoc for the getAuthToken
methods:
If a previously generated auth token is cached for this account and type, then it is returned. Otherwise, if a saved password is available, it is sent to the server to generate a new auth token. Otherwise, the user is prompted to enter a password.
Since your token is in the cache, it is returned directly and your authenticator is not consulted.