2

I'm making a simple REST client in Android: I created a service that is responsible for the REST calls, but it needs the username and password to operate. The username and password is stored in a regular account registry, so what I want to do is to retrieve the usernama and auth token in the service.

The problem is that getAuthToken wants to have the Activity as the parameter, but while I'm in the service I don't have any kind of Activity to pass. Is there a known way to deal with this issue? Or I'm doing is all wrong and I shouldn't even try to access this data from within a service?

Alex Bitek
  • 6,529
  • 5
  • 47
  • 77
Juriy
  • 5,009
  • 8
  • 37
  • 52

1 Answers1

2

Not necessarily: I've used http://developer.android.com/reference/android/accounts/AccountManager.html#getAuthToken%28android.accounts.Account,%20java.lang.String,%20boolean,%20android.accounts.AccountManagerCallback%3Candroid.os.Bundle%3E,%20android.os.Handler%29 before from within a Service.

EDIT: The world changed. It is now recommended to use this. You just have to have your app register an IntentReceiver for the LOGIN_ACCOUNTS_CHANGED_ACTION and then you will receive that intent whenever the user enters a password. You very explicitly don't wait for the result: you either get it immediately if they're already logged in or you get told you need to wait for the intent.

Femi
  • 64,273
  • 8
  • 118
  • 148
  • Hehehe. That's alright: I just remembered 'cause I used that a while ago. – Femi May 05 '11 at 15:12
  • @Femi Can you provide an example that uses this method, the documentation provided is narrow and have no idea when it comes to implementing this. I am also trying to get the auth token of a gmail account form inside a service, – diyoda_ Sep 12 '14 at 19:34
  • I know this is old, but, like Diode, I'm looking for an example. Specifically, I'm wondering how you managed to wait for the potentially returned authentication Intent to finish, once you started it within the Service. – user3570982 Oct 02 '14 at 19:30
  • As it so happens, you shouldn't use this anymore: I'll update the answer. – Femi Oct 03 '14 at 23:25
  • As an example, see here: http://stackoverflow.com/questions/13498584/trouble-capturing-activity-result-when-firing-intent-received-from-accountmanage. That shows the general flow. – Femi Oct 03 '14 at 23:29
  • Please if you can provide an example it would be great – f.trajkovski Jun 11 '18 at 16:43