4

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?

Geralt_Encore
  • 3,721
  • 2
  • 31
  • 46
  • I'm a little confused by the question. Are you saying that line of code works when you don't have your own Authenticator? Or are you saying that your own Authenticator works for getting your accounts, but you can never get Google accounts? – Dan Lew Dec 08 '15 at 13:44
  • http://stackoverflow.com/questions/2727029/how-can-i-get-the-google-username-on-android – IntelliJ Amiya Dec 08 '15 at 13:48
  • @DanielLew the second one. If I query all accounts I will get only the one from my app – Geralt_Encore Dec 08 '15 at 13:49

1 Answers1

10

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).

Dan Lew
  • 85,990
  • 32
  • 182
  • 176
  • 1
    You are right! The problem was in runtime permission. I was confused because in me test app permission was granted. As soon as it was revoked behaviour of both apps became the same. Many thanks! And btw thank you for your articles about Rx and styling =) – Geralt_Encore Dec 08 '15 at 14:34