使用AccountManager对应用程序中的用户进行身份验证
我知道我可以调用getAuthTokenByFeatures,如果没有为我的特定accountType设置帐户,它会启动我的LoginActivity,这正是我想要的,
我有一个BaseActivity,它在onCreate方法上执行,但是在启动LoginActivity时,旧的活动仍然在堆栈上,因此,通过按回按钮,用户可以返回到以前的活动,这是我不想要的行为,我在BaseActibity.onCreate上的代码如下
AccountManager manager = AccountManager.get(getBaseContext());
manager.getAuthTokenByFeatures(
AccountGeneral.ACCOUNT_TYPE,
AccountGeneral.AUTHTOKEN_TYPE_FULL_ACCESS,
null,
this,
null,
null,
new AccountManagerCallback<Bundle>() {
@Override
public void run(AccountManagerFuture<Bundle> future) {
Bundle bnd = null;
try {
bnd = future.getResult();
final String authtoken = bnd.getString(AccountManager.KEY_AUTHTOKEN);
LOGV(TAG, "GetTokenForAccount Bundle is " + bnd);
} catch (Exception e) {
LOGE(TAG, "exception while getAuthTokenByFeatures", e);
}
}
}
, null);问题是:我如何才能阻止这种背靠背行为?如果是我编程地调用LoginActivity,我只需在BaseActivity上调用finish()
发布于 2014-05-08 10:10:45
在您的AccountAuthenticatorActivity上,您可以重写back按钮行为:
@Override
public void onBackPressed() {
Intent result = new Intent();
Bundle b = new Bundle();
result.putExtras(b);
setAccountAuthenticatorResult(null); // null means the user cancelled the authorization processs
setResult(RESULT_OK, result);
finish();
}现在你可以对这种取消做出反应了。在您的代码中:
@Override
public void run(AccountManagerFuture<Bundle> future) {
Bundle bnd = null;
try {
if (future.isCancelled()) {
// Do whatever you want. I understand that you want to close this activity,
// so supposing that mActivity is your activity:
mActivity.finish();
return;
}
bnd = future.getResult();
final String authtoken = bnd.getString(AccountManager.KEY_AUTHTOKEN);
LOGV(TAG, "GetTokenForAccount Bundle is " + bnd);
} catch (Exception e) {
LOGE(TAG, "exception while getAuthTokenByFeatures", e);
}
}https://stackoverflow.com/questions/21067386
复制相似问题