Android:AccountManager.getAccounts() 返回一个空数组

标签 android accountmanager

我正在编写一个针对 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/

相关文章:

android - 如何在我的网络应用程序中使用来自 AccountManager 的 Google token ? (Rails with omniauth-google-oauth2)

android - 如何在android中获取帐户列表?

跨应用程序的 Android AccountManager : uninstalling the first app that registered the account causes the account to be deleted?

android - 硬件后退按钮第一次不起作用

android - 父中心不适用于自定义相对布局

android - 使用 Intent 将用户转发到带有预先确定的搜索关键字的 Twitter 应用程序

android - 如果帐户身份验证器中的身份验证 token 已过期,则使用刷新 token

android - 删除 RecyclerView FooterView

android - 以编程方式设置 SlidingUpPanelLayout 的面板高度

android - 我应该使用 Android AccountManager 做什么?