Skip to content
Search

Android.accounts.AccountManager

lastmod 2015-10-17created 2015-10-171 edits

Service process

Applications that connect to a server usually go through the following steps.

  1. Get an instance of AccountManager using get(Context).
  2. List the available accounts using getAccountByType(String) or getAccountByTypeAndFeatures(String, String[], AccountManagerCallback, Handler). Applications are usually interested in accounts of one particular type (the type identifies the Authenticator). Account Features are used to identify particular account subtypes and Capabilities. Both Account Type and Feature are strings specific to the Authenticator, and must be known by the application in coordination with its Prefered Authenticators.
  3. Select one or more of the available accounts, if possible by asking the user for their preference. If no suitable accounts are available, addAccount(String, String, String[], Bundle, Activity.AccountManagerCallback, Handler) is called to prompt the user to create an account of the appropriate type.
  4. If the application uses a previously remembered account selection, it must make sure the account is still in the list of accounts returned by getAccountByType(String). Requesting an Auth token for an account no longer on the device results in an undefined failure.
  5. Request an Auth token for the selected account(s) using the getAuthToken(Account, String, Bundle, Activity, AccountManagerCallback, Handler) method or one of its related Helpers.
  6. Make the request using the Auth token. The form of the Auth token, the format of the request, and the protocol used are all specific to the service the developer is accessing. The application may use whatever network and protocol libraries are useful.
  7. If the request fails with an Authentication error, it means the cached Auth token is stale or no longer honored by the server. The application must call invalidateAuthToken(String, String) to remove the Token from the cache; otherwise requests will keep failing. After invalidating the Auth token, immediately go back to the "Request an Auth token" step above. If it fails a second time, it is treated as a genuine Authentication failure, and the user is notified or other appropriate actions are taken.

Favorite site