为什么 Accountmanager.get(context).getAccounts() 返回安全异常?

Why is Accountmanager.get(context).getAccounts() returning securityexception?

据我了解,Accountmanager.get(context).getAccounts 无法 return 安全异常,因为它不需要权限。来自文档:

Clients of this method that have not been granted the GET_ACCOUNTS permission, will only see those accounts managed by AbstractAccountAuthenticators whose signature matches the client.

https://developer.android.com/reference/android/accounts/AccountManager.html#getAccounts()

所以它看起来好像你还没有 GET_ACCOUNTS 那没关系,但是我的应用程序在调用该方法时崩溃,出现以下异常:

caller uid # lacks any of android.permission.GET_ACCOUNTS

为什么会这样?

Account[] accounts = AccountManager.get(parentActivity).getAccounts();

您的应用在您的清单中缺少匹配的权限。 添加

<uses-permission
     android:name="android.permission.GET_ACCOUNTS" />

给你的AndroidManifest.xml

编辑:对于 Android 6.0,在运行时请求它:http://developer.android.com/training/permissions/requesting.html

GET ACCOUNTS: Allows access to the list of accounts in the Accounts Service. Protection level: dangerous