androidoauth-2.0android-authenticator

Custom AccountAuthenticator: auth token getting


When I try to get the auth token from AccountManager I need to call

AccountManagerFuture<Bundle> getAuthToken(Account account, String authTokenType, Bundle options, Activity activity, AccountManagerCallback<Bundle> callback, Handler handler);

What's difference between AccountManagerFuture<Bundle> and AccountManagerCallback<Bundle>? I read docs but it doesn't clearly.

P.S. Maybe, it's noobie question but I really cannot understand it from Google docs.


Solution

  • To my understanding, AccountManagerCallback is an optional callback to inform you when the result of that call is ready.

    Since AccountManagerFuture<Bundle> doesn't have any mechanism to notify you when the result is ready, you would have to make a call to getResult() or getResult(long, TimeUnit) to get the result. Since these calls are blocking you would need to spawn a new thread first. The documentation explicitly states that you must not call them from the main thread. Of course it should be safe to use them on the main thread once the isDone() method returns true.

    The callback just receives an AccountManagerFuture<Bundle> that also contains the result (which might actually be the same object as above) but is already done loading. It is a convenient method to get notified when the result is ready and you can safely call AccountManagerFuture.getResult() without having to wait for the result and without having to spawn a thread yourself.