Understanding Intent in Android Authentication

I have a few doubts about the Android architecture when working with authentication.

Suppose I call AccountManager.getAuthTokenfor a specific account that I must authenticate. Let's say that authentication failed due to a bad password. The contract AbstractAccountAuthenticatorrequires the authenticator to return Bundleusing Activitythat handles the username / password input through KEY_INTENT.

My question is: who should show the user interface? Does Android automatically detect that the KEY_INTENTuser interface is present and launches, or does my code have startActivitythe intention embodied in the answer AccountManager? The same applies to AccountManager.addAccountwhich links the result through the Future interface.

Where can I find some guides on these topics?

thank

+5
source share
1 answer

The system does not automatically show activity when present KEY_INTENT. It is for you to start this activity.

Here is a sample code:

private AccountManagerCallback<Bundle> mAccountManagerCallback = new AccountManagerCallback<Bundle>() {

    public void run(AccountManagerFuture<Bundle> future) {

        Bundle bundle;
        try {

            bundle = future.getResult();
            //if an intent was sent, start the required activity
            if (bundle.containsKey(AccountManager.KEY_INTENT)) {
                Intent intent = bundle.getParcelable(AccountManager.KEY_INTENT);

                //clear the new task flag just in case, since a result is expected
                int flags = intent.getFlags();
                flags &= ~Intent.FLAG_ACTIVITY_NEW_TASK;
                intent.setFlags(flags);

                startActivityForResult(intent, REQUEST_CODE_AUTH);

        } else {
            //otherwise, just get the credentials
            if (bundle.containsKey(AccountManager.KEY_AUTHTOKEN)) {
                    String authToken    = bundle.getString(AccountManager.KEY_AUTHTOKEN);
                    String userMail     = bundle.getString(AccountManager.KEY_ACCOUNT_NAME);
                    //use the credentials
            }
        }
      }
      catch(...) {
        ...
        //handle errors, maybe retry your getAuthToken() call
      }
    }
}

I hope this is what you were looking for, but if I did not understand your question correctly, please clarify.

Hooray!

0
source

All Articles