我正在编写一个针对 Lollipop 及更高版本的应用程序。这是我的第一个 Android 应用。
我正在尝试获取与该设备关联的所有帐户的列表。这是我的代码:
public void getAllContactsAccounts(Context context){
AccountManager accountManager = (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);
Account[] accounts = accountManager.getAccounts();
System.out.println("PrintingAccounts: " + accounts.length);
for(Account a : accounts){
System.err.println("AccountName: " + a.name);
}
}
accountManager.getAccounts()
中的数组最终长度为 0,并且没有任何反应。
我不知道如何解决这个问题。我已经添加了必要的权限(加上一些其他权限),没有发生安全异常,应用程序运行正常,但它似乎无法检索帐户。
关于我在这里可以做什么有什么建议吗?
最佳答案
从 Android 8.0 开始,GET_ACCOUNTS 不再足以访问设备上的帐户。
基于documentation :
In Android 8.0 (API level 26), apps can no longer get access to user accounts unless the authenticator owns the accounts or the user grants that access. The GET_ACCOUNTS permission is no longer sufficient. To be granted access to an account, apps should either use AccountManager.newChooseAccountIntent() or an authenticator-specific method. After getting access to accounts, an app can call AccountManager.getAccounts() to access them.
你可以查看这个link AccountManager.newChooseAccountIntent()
关于Android:AccountManager.getAccounts() 返回一个空数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35050548/