Android的认证:回调不叫 [英] Android authentication: callback isn't called
问题描述
我在Android 4.2使用验证样本作为一个例子实现一个身份验证系统。在我的应用我有登录选项MenuActivity。如果该选项被单击,并且用户未在已经登录,我的认证系统启动并AuthenticationActivity被显示。
I'm implementing an authentication system in Android 4.2 using the authentication sample as an example. In my application I have a MenuActivity with the Login option. If that option is clicked and the user isn't logged in already, my authentication system starts and the AuthenticationActivity is showed.
在登录结束我想,所以它知道。我创建了一个回调做这样用户已登录到执行中的一些MenuActivity code,但它永远不会被调用。登录工作正常,如果我关闭应用程序并重新启动它检测到作为登录的用户。
When the login ends I want to execute some code in the MenuActivity so it knows the user has logged in. I created a callback to do so, but it's never called. The login works fine, if I close the app and start it again it detects the user as logged in.
在我的菜单活动我有这样的:
In my Menu Activity I have this:
public void login() {
if(mAccount != null)
Toast.makeText(MenuActivity.this, getString(R.string.account_exists), Toast.LENGTH_LONG).show();
else{
mAccountManager.addAccount(ACCOUNT_TYPE, AUTHTOKEN_TYPE, null, null, this, completeCallbackLogin, null);
}
}
// Callback called when the login ends.
private AccountManagerCallback<Bundle> completeCallbackLogin = new AccountManagerCallback<Bundle>() {
public void run(AccountManagerFuture<Bundle> arg0) {
Log.d("MenuActivity", "CALLBACK");
// When the login ends we save the account in the global variables
refreshAccount();
Toast.makeText(MenuActivity.this, getString(R.string.login_ok), Toast.LENGTH_LONG).show();
}
};
在我AuthenticatorActivity(称为LoginActivity)我做到以下几点:
In my AuthenticatorActivity (called LoginActivity) I do the following:
Account account = new Account(mUsername, ACCOUNT_TYPE);
try{
mAccountManager.addAccountExplicitly(account, mPassword, newBundle());
}catch(Exception e){
e.printStackTrace();
return;
}
final Intent intent = new Intent();
intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, mUsername);
intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, ACCOUNT_TYPE);
intent.putExtra(AccountManager.KEY_PASSWORD, mPassword);
intent.putExtra(AccountManager.KEY_AUTHTOKEN, mAuthToken);
intent.putExtra(AccountManager.KEY_BOOLEAN_RESULT, true);
LoginActivity.this.setAccountAuthenticatorResult(intent.getExtras());
setResult(RESULT_OK, intent);
LoginActivity.this.finish();
就像我说的,我的回调不会被调用。这是为什么?
Like I said, my callback is never called. Why is that?
我只注意到,如果我再次运行应用程序(Eclipse的>运行)回调是正确的我的应用程序关闭,以重新安装之前调用。
I just noticed that if I run the application again (Eclipse->Run) the callback is called right before my application closes to be installed again.
[/编辑]
推荐答案
在我AbstractAccountAuthenticator我忘了添加在addAccount方法以下行:
In my AbstractAccountAuthenticator I forgot to add the following line in the addAccount method:
intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
这篇关于Android的认证:回调不叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!