Wednesday, 6 June 2012

Android - Account manager - Part II

Note: Android - Account manager - Part I

Update: Sample code uploaded to GitHub.

6. Account manager implementation
6.1 Introduction
The implementation of account manager is pretty unusual because the coder should create at least 3 files. This includes
  • The activity which utilize the account manager
  • The service which should extends the class AbstractAccountAuthenticator
  • The Authenticator (XML)
  • The preference file (XML) - Optional
  • The classes which interacts with the service - Optional
6.2 The main activity
The main activity is the one which get an instance of Account Manager and invokes its methods. Most of the methods are explained in the previous post.

6.2 The service
The service is one of the most important pieces to interact with the account manager. It is used when the user can creates account explicitly, without using any other app than Android it self. This is, when the user click on Settings -> Account and Sync -> Creates a new accounts, the system will show her the list of the account that she can creates explicitly. If an app can creates accounts by itself, it is expected to allow the user to creates account explicitly here.

Thus, when the user clicks on any element, the Account manager will bind a service, which should return an implementation of AbstractAccountAuthenticator. Furthermore, the system will invoke the method addAccount of such service. This method should launch an activity to allow the user to input the credentials (user name, password, etc) and finally, return the proper answer to the Account manager. Here is a very good example of its implementation:

This service should be declared in the Android Manifest file in a special way:

<service android:name="AccountAuthenticatorService"
    android:exported="true" android:process=":auth">
        <action android:name="android.accounts.AccountAuthenticator" />
    <meta-data android:name="android.accounts.AccountAuthenticator"
      android:resource="@xml/authenticator" />

Source: Did you win yet?

The code above associated the service with a specific xml file called authenticator. It could be stored in the res/xml folder.

6.3 Authenticator
The authenticator sets a collections of parameters used:
  • Account type: The type of account. This should be the same when the account is created explicitly. Other wise, a no-sense error will appear.
  • Icons: The icons shown in the account manager
  • Label: The label to show
  • AccountPreferences: The location of a XML file which indicates the preferences to show once the account has been created. For example, to access to the account settings
  • etc.
Here is an example of the xml file:

<account-authenticator xmlns:android=""

Source: Did you win yet?

6.4 Preference file
The preference file is a normal xml for preferences. 
Here is an example:

            android:title="General Settings" />

        android:title="Account Settings"
        android:summary="Sync frequency, notifications, etc.">
            android:targetClass="" />

6.5 Service helper classes
The service helper classes are used to help a specific method of the class. For example, the user might want to create a specific class to allow the user to create an account, when the method addAccount of the service is being called.

For this specific case, there is a specific class called AccountAuthenticatorActivity provided by Google that  make the authentication a bit easier. Here is the source code:

What does this class do is allow the user to set the Authentication result, putting the user name and the account type, and when finish, send it back to the Account manager. The use of this class is not compulsory. One can creates any activity as the AccountAuthenticatorActivity.

You might find an example of usage in the example of Google called SampleSynchAdapter.


  1. Both articles are very good because they summarize all the necessary concepts which other tutorials fail to do. Well done!
    What I am still missing though is how to embed the functionality inside any activity. What is the common flow? Alan D. Jackson's sample is inadequate

    1. Yes you are right. A good example of how to use it is missing. I have created the structure for the code 10 months ago but I am too busy to complete it. Sorry about that.

    2. Hi:

      I just upload the sample code to GitHub. I hope it is useful.

  2. Hello Jiahao,

    It was a nice article.
    Is it possible to add google account programmatically into my android phone? Or is it that only Google's authenticator can do this ?

    My intention is this: When Gmail is launched it should use the my newly added gmail account. (I want to add this account by program).


    1. Hi:
      I am not sure about it. Why don't you ask it on StackOverFlow?