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

Android:AccountManager.getAccounts()返回一个空数组,android,accountmanager,Android,Accountmanager,我正在写一个针对棒棒糖及以上的应用程序。这是我的第一个Android应用程序 我正在尝试获取与设备关联的所有帐户的列表。这是我的密码: public void getAllContactsAccounts(上下文){ AccountManager=(AccountManager)context.getSystemService(context.ACCOUNT\u SERVICE); Account[]accounts=accountManager.getAccounts(); System.ou

我正在写一个针对棒棒糖及以上的应用程序。这是我的第一个Android应用程序

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

public void getAllContactsAccounts(上下文){
AccountManager=(AccountManager)context.getSystemService(context.ACCOUNT\u SERVICE);
Account[]accounts=accountManager.getAccounts();
System.out.println(“PrintingAccounts:+accounts.length”);
(账户a:账户){
System.err.println(“AccountName:+a.name”);
}
}
accountManager.getAccounts()
中的数组的长度最终为0,并且不会发生任何事情

我想不出怎么解决这个问题。我已经添加了必要的权限(加上一些其他权限),没有发生安全异常,应用程序运行正常,但似乎无法检索帐户


关于我可以在这里做什么有什么建议吗?

由于SdkTarget 23及以上版本(棉花糖6.0),您需要向用户请求通过运行时访问的权限

 int MY_PERMISSIONS_REQUEST_READ_CONTACTS = 0;
    // Here, thisActivity is the current activity
    if (ContextCompat.checkSelfPermission(this,
            android.Manifest.permission.GET_ACCOUNTS)
            != PackageManager.PERMISSION_GRANTED) {

        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                android.Manifest.permission.GET_ACCOUNTS)) {

            // Show an expanation to the user *asynchronously* -- don't block
            // this thread waiting for the user's response! After the user
            // sees the explanation, try again to request the permission.

        } else {

            // No explanation needed, we can request the permission.

            ActivityCompat.requestPermissions(this,
                    new String[]{android.Manifest.permission.GET_ACCOUNTS},
                    MY_PERMISSIONS_REQUEST_READ_CONTACTS);
    }
}
    String possibleEmail="";
    try{
        possibleEmail += "************* Get Registered Gmail Account *************\n\n";
        Account[] accounts = AccountManager.get(this).getAccountsByType("com.google");

        for (Account account : accounts) {

            possibleEmail += " --> "+account.name+" : "+account.type+" , \n";
            possibleEmail += " \n\n";

        }
    }


     Log.i("EXCEPTION", "mails: " + possibleEmail);
onCreate()中的代码段仅供参考


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

根据:

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


您可以检查一下AccountManager.newChooseAccountContent()的使用情况。

出于某种原因,我也必须请求“android.permission.READ\u CONTACTS”才能访问帐户列表。 似乎所需的权限是:

android.permission.READ_联系人


android.permission.GET_ACCOUNTS

我复制了你的代码并在我的棒棒糖设备上运行了它。下面列出了我在设备上的两个Google帐户。当您转到手机上的设置-->帐户时,是否看到其中列出的帐户?这可能是因为Android M中的“危险权限”新概念。有关更多信息,请查看此答案:Pascals注释正确,Android现在要求在您的活动中显式地请求权限。作为用户,这是有意义的,因为它提高了安全性。当用户授予权限时,帐户管理器阵列将不会为空。我也有同样的问题,我已经获得了权限,有解决方案吗?有解决方案的家伙即使在权限允许后也无法使其工作Android如何知道验证者是否拥有帐户?我用不同的应用程序ID和帐户类型制作了两个应用程序,但不知何故,其中一个能够检索到另一个的帐户。这里也是一样。我没有找到关于这一点的任何文件。甚至android.com上的帐户教程也没有提到这一点()。这是我不理解的问题的解决方法吗?