I am trying to get the Authentication Token from an account in Android before I make my request to the server. I Am trying to control the flow with a CountdownLatch so that it waits until:
- a) A timeout (10s)
b) We get the token
private CountDownLatch tokenLatch = new CountDownLatch(1); final long tokenTimeoutSeconds = 10; AccountManager manager = AccountManager.get(mContext); Account userAccount = getCurrentAccount(); // Get the auth token if (userAccount != null) { AccountManagerFuture<Bundle> future = manager.getAuthToken(userAccount, AccountUtility.AUTHTOKEN_TYPE_FULL_ACCESS, true, new AccountManagerCallback<Bundle>() { @Override public void run(AccountManagerFuture<Bundle> future) { try { Bundle bundle = future.getResult(); currentAuthToken = bundle.get(AccountManager.KEY_AUTHTOKEN).toString(); tokenLatch.countDown(); } catch (Exception e) { Log.e(LOG_TAG, "Problem getting auth token!", e); } } }, null); try { tokenLatch.await(tokenTimeoutSeconds, TimeUnit.SECONDS); } catch (InterruptedException e) { Log.e(LOG_TAG, "Interupted while getting auth token!", e); }
Context is passed:
mContext = ... getApplicationContext();
Right now it exits before either of those two cases. It does, however, always reach the AccountManagerCallback after all other processes are finished. Strange. I am most definitely doing something wrong. Thanks for the helperooni!