如何检查Android设备上是否有Google authenticator?
我的Android应用程序使用AccountManager API访问Google finance。AndroidManifest.xml中是否有任何功能/属性或任何其他技术可用于确保该应用程序仅对安装了Google authenticator(附加组件)的设备可用如何检查Android设备上是否有Google authenticator?,android,Android,我的Android应用程序使用AccountManager API访问Google finance。AndroidManifest.xml中是否有任何功能/属性或任何其他技术可用于确保该应用程序仅对安装了Google authenticator(附加组件)的设备可用 AccountManager从API级别5开始提供,这意味着所有android 2.0或更高版本的设备都将拥有它 您可以使用getAccountsByType和com.google作为帐户类型来检查google帐户 即使该设备有安卓
getAccountsByType
和com.google
作为帐户类型来检查google帐户就像谷歌做的那样:当用户启动应用程序时,检查是否有正确的帐户,如果没有,通知用户并停止应用程序 它不仅与google帐户验证器有关,这种行为是普遍的:
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);
AccountManager.get(context.addAccount)(
,
,
无效的
,
activityToStartAccountAddActivity,
新建AccountManagerCallback(){
@凌驾
公共作废运行(AccountManagerFuture){
试一试{
future.getResult();
}捕捉(操作取消异常e){
抛出新的运行时异常(e);
}捕获(IOE异常){
抛出新的运行时异常(e);
}捕获(认证异常e){
抛出新的RuntimeException(e);//如果没有安装google帐户验证器,您将在此处显示“绑定失败”
}
}
},
无效);
如果您的设备上没有安装authenticator,该设备支持请求的帐户类型和令牌类型,那么您将获得AuthenticateTerException。基本上,任何android设备都有google authenticator。如果它没有根目录并且相关包被删除,当然:)使用
getAccountsByType
的解决方案的问题在于,您无法区分身份验证程序未安装的情况,或者存在身份验证程序但缺少通过身份验证程序验证的帐户的情况。在第二种情况下,您可能希望提示用户添加新帐户
当方法AccountManager.getAuthenticatorTypes()
存在时,尝试添加帐户然后检查异常也不太理想。像这样使用它:
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.
String type=“com.example”;//目标验证器的帐户类型
AccountManager am=AccountManager.get(这个);
AuthenticatorDescription[]authenticators=am.getAuthenticatorTypes();
对于(int i=0;i
我的Java有点生疏(我是Xamarin开发人员),但这应该让您了解如何检查系统上是否存在验证器,而不触发AddAccount活动(如果确实存在)
资料来源:假设该设备安装了Google API(安卓2.0之上),那么我们应该能够将用户重定向到设置以设置新帐户。我需要一种方法来确定是否安装了Google authenticator的附加组件,而不是确定他们是否安装了Google帐户。在安卓模拟器上,“添加Google帐户”只有当目标在平台2.2或更高版本上设置为Google API(级别8)时,选项才会显示在设置中!它不仅与Google帐户验证器相关,当没有为所需的令牌类型注册验证器时,我们会得到此行为,作为参数传递