AccountManager return empty accounts

半腔热情 提交于 2019-12-12 11:21:04

问题


I have an app which is implementing my own Authenticator as described in this article. And it works fine as intended. Now I need to get google account from AccountManager and I am doing it this way:

 Account[] accounts = AccountManager.get(this).getAccountsByType("com.google");

The problem is that it return an empty array. But when I am using this snippet in my test app without all Authenticator stuff it works like a charm. I can't see where is an actual difference between these cases. Has anybody faced with this issue?


回答1:


My guess is that you lack the GET_ACCOUNTS permission. You can get your own app's accounts without it but access to other accounts requires permission.

It's a bit more complex than simply requesting GET_ACCOUNTS in the manifest these days because of runtime permissions. Now you have to request them dynamically, and the message shown to users for GET_ACCOUNTS is pretty cryptic (it asks for access to contacts, since it's part of the contacts permission group).

You may want to look into using AccountPicker (from Google Play Services), which requires no permissions. It presents a dialog to the user with the requested accounts (and gives an option to add a new account, too).



来源:https://stackoverflow.com/questions/34157116/accountmanager-return-empty-accounts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!