gpt4 book ai didi

Android AccountManager 和登录 Activity

转载 作者:太空狗 更新时间:2023-10-29 15:53:34 25 4
gpt4 key购买 nike

我在我的应用程序中使用 AccountManager 对用户进行身份验证

我知道我可以调用 getAuthTokenByFeatures,如果没有为我的特定帐户类型设置帐户,它会启动我的 LoginActivity,这正是我想要的,

我有一个 BaseActivity,它在 onCreate 方法上执行此操作然而,在启动 LoginActivity 时,旧 Activity 仍在堆栈中,因此通过按下后退按钮,用户可以返回到上一个 Activity 是我不想要的行为,我在 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()

最佳答案

在您的AccountAuthenticatorActivity 上,您可以覆盖后退按钮行为:

@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();
}

现在您可以对此取消使用react。在您的代码中:

            @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);
}
}

关于Android AccountManager 和登录 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21067386/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com