我的安卓应用程序使用AccountManager API访问谷歌金融。在AndroidManifest.xml或其他技术中有没有什么特性/属性可以用来确保应用程序只对安装了谷歌验证器(附加组件)的设备可用?
发布于 2011-05-08 02:26:14
com.google的getAccountsByType的谷歌账户。只要像谷歌那样做:当用户启动应用程序时,检查是否有正确的帐户,如果没有,通知用户并停止应用程序。
发布于 2015-06-18 17:59:09
它不仅仅与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);如果你没有在设备上安装验证器,它同时支持请求的帐户类型和令牌类型,那么你会得到AuthenticatorException。基本上,任何android设备都有谷歌验证器。当然,如果它不是根目录,相关的包也被删除了:)
发布于 2017-08-13 16:58:45
使用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.我的Java有点生疏(我是Xamarin开发人员),但这应该会让您了解如何检查系统上是否存在验证器,而不会触发add account活动,以防它确实存在。
资料来源:
https://stackoverflow.com/questions/5923083
复制相似问题