首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何检查Google authenticator在Android设备上是否可用?

如何检查Google authenticator在Android设备上是否可用?
EN

Stack Overflow用户
提问于 2011-05-08 02:17:11
回答 3查看 925关注 0票数 3

我的安卓应用程序使用AccountManager API访问谷歌金融。在AndroidManifest.xml或其他技术中有没有什么特性/属性可以用来确保应用程序只对安装了谷歌验证器(附加组件)的设备可用?

EN

回答 3

Stack Overflow用户

发布于 2011-05-08 02:26:14

  1. AccountManager是从API级别5开始提供的,这意味着所有安卓2.0或更高版本的设备都会有它。
  2. 你可以检查账户类型为com.googlegetAccountsByType的谷歌账户。
  3. 即使设备安装了安卓2.0或更高版本,也不能保证用户会设置谷歌账户。他们将无法访问市场或其他谷歌应用程序(gmail、地图等)。但其他任何东西都可以。

只要像谷歌那样做:当用户启动应用程序时,检查是否有正确的帐户,如果没有,通知用户并停止应用程序。

票数 3
EN

Stack Overflow用户

发布于 2015-06-18 17:59:09

它不仅仅与google帐户验证器相关,此行为是通用的:

代码语言:javascript
复制
AccountManager.get(context).addAccount(
                    <google account type>,
                    <needed token type>,
                    null,
                    <options or null if not needed>,
                    activityToStartAccountAddActivity,
                    new AccountManagerCallback<Bundle>() {
                        @Override
                        public void run(AccountManagerFuture<Bundle> future {
                            try {
                                future.getResult();
                            } catch (OperationCanceledException e) {
                                throw new RuntimeException(e);
                            } catch (IOException e) {
                                throw new RuntimeException(e);
                            } catch (AuthenticatorException e) {
                                throw new RuntimeException(e); // you'll go here with "bind failure" if google account authenticator is not installed
                            }
                        }
                    },
                    null);

如果你没有在设备上安装验证器,它同时支持请求的帐户类型和令牌类型,那么你会得到AuthenticatorException。基本上,任何android设备都有谷歌验证器。当然,如果它不是根目录,相关的包也被删除了:)

票数 0
EN

Stack Overflow用户

发布于 2017-08-13 16:58:45

使用getAccountsByType的解决方案的问题在于,您无法区分身份验证器是否未安装,或者身份验证器是否存在但缺少通过它进行身份验证的帐户。在第二种情况下,您可能希望提示用户添加新帐户。

AccountManager.getAuthenticatorTypes()方法存在时,尝试添加帐户然后检查异常也不太理想。像这样使用它:

代码语言:javascript
复制
String type = "com.example"; // Account type of target authenticator
AccountManager am = AccountManager.get(this);
AuthenticatorDescription[] authenticators = am.getAuthenticatorTypes();
for (int i = 0; i < authenticators.length(); ++i) {
    if (authenticators[i].type.equals(type)) {
        return true; // Authenticator for accounts of type "com.example" exists.
    }
return false; // no authenticator was found.

我的Java有点生疏(我是Xamarin开发人员),但这应该会让您了解如何检查系统上是否存在验证器,而不会触发add account活动,以防它确实存在。

资料来源:

getAuthenticatorTypes

AuthenticatorDescription.type

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5923083

复制
相关文章

相似问题

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