Android:AccountManager.getAccounts()返回一个空数组 [英] Android: AccountManager.getAccounts() returns an empty array

查看:108
本文介绍了Android:AccountManager.getAccounts()返回一个空数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个针对Lollipop及更高版本的应用程序.这是我的第一个Android应用.

I'm writing an app targeted at Lollipop and above. This is my first Android app.

我正在尝试获取与设备关联的所有帐户的列表.这是我的代码:

I'm trying to get a list of all the accounts that are associated with the device. Here is my code:

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,并且没有任何反应.

The array from accountManager.getAccounts() ends up having a length of 0, and nothing happens.

我不知道如何解决此问题.我已经添加了必要的权限(以及其他一些权限),没有发生安全异常,应用运行良好,但似乎无法检索到帐户.

I can't figure out how to fix this. I've added the necessary permissions (plus a few others), no security exception happens, the app runs fine, but it just can't seem to retrieve the accounts.

关于我在这里可以做什么的任何建议吗?

Any advice on what I can do here?

推荐答案

从Android 8.0开始,GET_ACCOUNTS不再足以访问设备上的帐户.

From Android 8.0, GET_ACCOUNTS is no longer sufficient to access the Accounts on device.

基于文档:

在Android 8.0(API级别26)中,除非身份验证者拥有帐户或用户授予访问权限,否则应用将无法再访问用户帐户.GET_ACCOUNTS权限不再足够.要被授予访问帐户的权限,应用程序应使用AccountManager.newChooseAccountIntent()或特定于身份验证器的方法.访问帐户后,应用程序可以调用AccountManager.getAccounts()来访问它们.

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.

您可以点击链接以使用 AccountManager.newChooseAccountIntent()

这篇关于Android:AccountManager.getAccounts()返回一个空数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆