在安卓奥利奥中,AccountManager.getAccountsByType("com.google");
回报null
.
它在 Android 8 以下版本中运行良好。
下面是我的代码:
private static Account getAccount(AccountManager accManager) {
Account[] accounts = accManager.getAccountsByType("com.google");
Account account;
if (accounts.length > 0) {
account = accounts[0];
} else {
account = null;
}
return account;
}
提前致谢。
根据 Android 的更新,从 Oreo 开始我们无法使用AccountManager.getAccountsByType
获取用户设备上配置的 google 帐户列表,因为他们已经更新了 Google 登录功能。新功能将提示用户选择帐户,并且该帐户仅对我们的应用程序可见。
请参阅文档:https://developer.android.com/about/versions/oreo/android-8.0-changes#aaad
如果您仍想继续使用向用户显示所有帐户的旧方法,则需要通过执行以下步骤获得用户的额外同意。
您可以使用GoogleAuthUtil.requestGoogleAccountsAccess
获取 Google 帐户列表。
下面给出示例代码:
new Thread(() -> {
try {
GoogleAuthUtil.requestGoogleAccountsAccess(getApplicationContext());
} catch (Exception e) {
if (e instanceof UserRecoverableAuthException) {
startActivityForResult(((UserRecoverableAuthException) e).getIntent(),
REQ_CODE_PERMISSION_GET_GOOGLE_ACCOUNTS);
} else {
Log.e("SignIn", "Exception in getting google accounts" + e);
}
}
}).start();
这将创建一个活动,提示用户接受同意,允许 Google Play 服务访问设备上配置的 Google 帐户列表。
然后你可以覆盖onActivityResult()
继续您的活动。
然后你可以使用AccountManager.getAccountsByType
像以前一样获取谷歌帐户列表。
快乐编码!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)