为什么Accountmanager.get(context).getAccounts()返回securityexception?

时间:2015-11-24 14:55:16

标签: java android android-permissions android-6.0-marshmallow

我的理解是Accountmanager.get(context).getAccounts无法返回securityexception,因为它不需要权限。来自文档:

  

此方法的客户端尚未被授予GET_ACCOUNTS   权限,只会看到由管理的帐户   AbstractAccountAuthenticators,其签名与客户端匹配。

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

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

  

调用者uid#缺少任何android.permission.GET_ACCOUNTS

为什么会发生这种情况?

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

1 个答案:

答案 0 :(得分:4)

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

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

到您的AndroidManifest.xml

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

  

获取帐户:   允许访问“帐户服务”中的帐户列表。   保护等级:危险