Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android AccountManager API

Tags:

android

I'm struggling to understand the Android AccountManager API. As far as I got thinks working I can use the blockingGetAuthToken method and specify whether Android should provide a notification for user to allow or deny the request. Another possibility is to use getAuthToken and check if KEY_INTENT is returned. If that's the case I could start a new Activity where the user can confirm my request.

My problem is that I would like to call one of these two methods from within a Service. Is there any chance to get a callback once the user has made a decision?

Thanks for your help

like image 521
Steph Avatar asked Feb 19 '26 15:02

Steph


1 Answers

If you want a callback after the user has made a decision it's probably better to use the asynchronous version:

AccountManager mgr = AccountManager.get(getApplicationContext());
Account[] accounts = mgr.getAccountsByType("com.mydomain");
// assert that accounts is not empty

You'll want to use an AccountManagerFuture<Bundle> to hold results of the authentication token. This has to be async since the Android device may ask the user to login in the meantime:

private AccountManagerFuture<Bundle> myFuture = null;
private AccountManagerCallback<Bundle> myCallback = new AccountManagerCallback<Bundle>() {
    @Override public void run(final AccountManagerFuture<Bundle> arg0) {
        try {
           myFuture.getResult().get(AccountManager.KEY_AUTHTOKEN); // this is your auth token
       } catch (Exception e) {
           // handle error
       }
   }

}

Now you can ask for the auth token asynchronously:

myFuture = mgr.getAuthToken(accounts[0], AUTH_TOKEN_TYPE, true, myCallback, null);
like image 128
Abdullah Jibaly Avatar answered Feb 21 '26 10:02

Abdullah Jibaly



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!