首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android AccountManager和登录活动

Android AccountManager和登录活动
EN

Stack Overflow用户
提问于 2014-01-11 20:19:33
回答 1查看 1.7K关注 0票数 2

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

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

我有一个BaseActivity,它在onCreate方法上执行,但是在启动LoginActivity时,旧的活动仍然在堆栈上,因此,通过按回按钮,用户可以返回到以前的活动,这是我不想要的行为,我在BaseActibity.onCreate上的代码如下

代码语言:javascript
复制
    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()

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-08 10:10:45

在您的AccountAuthenticatorActivity上,您可以重写back按钮行为:

代码语言:javascript
复制
@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();
}

现在你可以对这种取消做出反应了。在您的代码中:

代码语言:javascript
复制
            @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);
                }
            }
票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21067386

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档