简体   繁体   English

如何检查Android设备上是否有Google身份验证器?

[英]how to check if Google authenticator is available on a Android device?

My Android app uses AccountManager API to access Google finance. 我的Android应用使用AccountManager API访问Google财经。 Is there any feature/attribute in AndroidManifest.xml or any other technique that I can use to make sure that the app will be only available to devices that have the Google authenticator (add-on) installed? AndroidManifest.xml中是否有任何功能/属性或我可用于确保该应用仅适用于安装了Google身份验证器(附加组件)的设备的任何其他技术?

  1. AccountManager is available since API Level 5, this means all devices with android 2.0 or higher will have it. AccountManager自API级别5开始可用,这意味着所有具有android 2.0或更高版本的设备都将拥有它。

  2. You can check for google account with getAccountsByType with com.google as the account type. 您可以使用com.google作为帐户类型,使用getAccountsByType检查Google帐户。

  3. Even if device has Android 2.0 or higher, there is no guarantee that user will setup a google account. 即使设备具有Android 2.0或更高版本,也无法保证用户将设置Google帐户。 They will not have access to market or other google apps (gmail, maps, etc..) but anything else will work. 他们将无法访问市场或其他谷歌应用程序(Gmail,地图等),但其他任何东西都可以使用。

Just do as google does: when user starts app, check if there is the right account and if not notify user and stop the app. 就像谷歌一样:当用户启动应用程序时,检查是否有正确的帐户,如果没有通知用户并停止应用程序。

It is not related only to google account authenticator, this behavior is general: 它与谷歌帐户身份验证器无关,此行为一般:

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

If you don't have authenticator installed on device, which supports both requested account type and token type, then you'll get AuthenticatorException. 如果您没有在设备上安装身份验证器,它支持所请求的帐户类型和令牌类型,那么您将获得AuthenticatorException。 Basically, any android device has google authenticator. 基本上,任何Android设备都有谷歌身份验证器。 If it is not rooted and related package deleted, of course :) 如果它没有root并删除相关包,当然:)

The problem with the solution of using getAccountsByType is that you cannot distinguish between the case of the authenticator not being installed or the presence of the authenticator but lack of accounts authenticated through it. 使用getAccountsByType的解决方案的问题在于,您无法区分未安装身份验证器的情况或身份验证器的存在,但缺少通过身份验证的帐户。 In the second case, you might want to prompt the user to add a new account. 在第二种情况下,您可能希望提示用户添加新帐户。

Attempting to add an account then checking for an exception is also less than ideal when the method AccountManager.getAuthenticatorTypes() exists. AccountManager.getAuthenticatorTypes()方法存在时,尝试添加帐户然后检查异常也不太理想。 Use it like so: 像这样使用它:

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.

My Java is a bit rusty (I'm a Xamarin developer) but this should give you an idea of how to check for the existence of an authenticator on the system without triggering the add account activity in case it does exist. 我的Java有点生疏(我是一个Xamarin开发人员)但是这应该让你知道如何检查系统上是否存在验证器,而不会触发添加帐户活动,以防它存在。

Sources: 资料来源:
getAuthenticatorTypes getAuthenticatorTypes
AuthenticatorDescription.type AuthenticatorDescription.type

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM