在Android Oreo中,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;
}
提前致谢。
答案 0 :(得分:0)
根据Android的更新,从奥利奥开始,我们无法使用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
来获取Google帐户列表。
快乐编码!