-For account management, the IAccountManager interface describes a group of related behaviors.
-AccountManagerService is a system service for Android. It implements the behavior defined by the IAccountManager interface. The implementation of these actions depends on the Authenticator defined in the application.
-AccountManager is a component for application development. It provides a set of application interfaces corresponding to the IAccountManager protocol. This set of interfaces communicate with the System Service AccountManagerService through the Binder Mechanism, and collaborate to complete account-related operations. At the same time, AccountManager receives the callback provided by the application to return the corresponding result to the application after the account operation is completed, and triggers the application layer to process the result.
Take the addAccount () operation as an example. The procedure is as follows:
1. AccountManager initializes an anonymous AmsTask subclass instance. AmsTask is the internal class of AccountManager:
[Java] private abstract class AmsTask extends FutureTask <Bundle> implements AccountManagerFuture <Bundle> {
Final IAccountManagerResponse mResponse;
Final Handler mHandler;
Final AccountManagerCallback <Bundle> mCallback;
Final Activity mActivity;
Public AmsTask (Activity activity, Handler handler, AccountManagerCallback <Bundle> callback ){
Super (new Callable <Bundle> (){
Public Bundle call () throws Exception {
Throw new IllegalStateException ("this shoshould never be called ");
}
});
MHandler = handler;
MCallback = callback;
MActivity = activity;
MResponse = new Response ();
}
...
Private abstract class AmsTask extends FutureTask <Bundle> implements AccountManagerFuture <Bundle> {
Final IAccountManagerResponse mResponse;
Final Handler mHandler;
Final AccountManagerCallback <Bundle> mCallback;
Final Activity mActivity;
Public AmsTask (Activity activity, Handler handler, AccountManagerCallback <Bundle> callback ){
Super (new Callable <Bundle> (){
Public Bundle call () throws Exception {
Throw new IllegalStateException ("this shoshould never be called ");
}
});
MHandler = handler;
MCallback = callback;
MActivity = activity;
MResponse = new Response ();
}
...
It is a subclass of FutureTask that executes asynchronous tasks and returns results.
The anonymous subclass in addAccount () implements the AmsTask. doWork () method:
[Java] public AccountManagerFuture <Bundle> addAccount (final String accountType,
Final String authTokenType, final String [] requiredFeatures,
Final Bundle addAccountOptions,
Final Activity activity, AccountManagerCallback <Bundle> callback, Handler handler ){
...
Return new AmsTask (activity, handler, callback ){
Public void doWork () throws RemoteException {
MService. addAcount (mResponse, accountType, authTokenType,
RequiredFeatures, activity! = Null, optionsIn );
}
}. Start ();
}
Public AccountManagerFuture <Bundle> addAccount (final String accountType,
Final String authTokenType, final String [] requiredFeatures,
Final Bundle addAccountOptions,
Final Activity activity, AccountManagerCallback <Bundle> callback, Handler handler ){
...
Return new AmsTask (activity, handler, callback ){
Public void doWork () throws RemoteException {
MService. addAcount (mResponse, accountType, authTokenType,
RequiredFeatures, activity! = Null, optionsIn );
}
}. Start ();
}
In the implementation of the doWork () method, call the proxy object (mService) of the AccountManagerService held by AccountManager to initiate IPC to the AccountManagerService.
2. AccountManger calls the start () method of the AmsTask anonymous subclass to start the task.
3. The start () method calls the doWork () method of this class. Here, the addAccount () Operation of AccountManagerService is executed.
4. According to the implementation mechanism of FutureTask, The done () method of this class is called at the end of the task execution period. The AmsTask class overwrites this method:
[Java] protected void done (){
If (mCallback! = Null ){
PostToHandler (mHandler, mCallback, this );
}
}
Protected void done (){
If (mCallback! = Null ){
PostToHandler (mHandler, mCallback, this );
}
}
The implementation here calls the AccountManager. postHandler () method. You can guess by name. Here, the run () method in the mCallback callback object is sent to the handler of the main thread for calling:
[Java] private void postToHandler (Handler handler, final AccountManagerCallback <Bundle> callback,
Final AccountManagerFuture <Bundle> future ){
Handler = null? MMainHandler: handler;
Handler. post (new Runnable (){
Public void run (){
Callback. run (future );
}
});
}
Private void postToHandler (Handler handler, final AccountManagerCallback <Bundle> callback,
Final AccountManagerFuture <Bundle> future ){
Handler = null? MMainHandler: handler;
Handler. post (new Runnable (){
Public void run (){
Callback. run (future );
}
});
}
In this call, the sources of the three parameters are:
-Handler: mHandler, which is the main thread of the current application.
-Callback: Provided by the application that calls AccountManager.
-Future: this is the current AmsTask instance. It implements the AccountManagerCallback interface and contains the returned results of the Cross-Inbound account addition operation. It is a Bundler object:
** Contains an Intent instance: indicates that the Account must start the activity specified by the account to interact with the user. The user will provide authentication information, such as the user name and password.
** Or include the name and type of the created account
The application will take the next step based on the actual content encapsulated in the Bundle.