Android: calling AccountManager.getAuthToken from within the service
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?
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.
精彩评论