基于Andoird 4.2.2的Account Manager源代码分析学习:AccountManager的简要工作流程
- 对于帐号管理,由接口IAccountManager描述其相关的一组行为
- AccountManagerService是Android的系统服务。它实现了接口IAccountManager定义的这一组行为。这些行为的实现依赖应用程序中定义的Authenticator。
- AccountManager是一个面向应用程序开发的组件。它提供一组对应于IAccountManager协议的应用程序接口。这组接口通过Binder机制与系统服务AccountManagerService进行通信,协作完成帐户相关的操作。同时,AccountManager接收应用程序提供的回调,以此在帐号操作完成之后向应用程序返回对应的结果,同时触发应用程序层对这个结果的处理。
以addAccount()操作为例,步骤如下:
1. AccountManager初始化一个匿名的AmsTask子类实例。AmsTask是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 should 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 should never be called");
}
});
mHandler = handler;
mCallback = callback;
mActivity = activity;
mResponse = new Response();
}
...
它是一个FutureTask子类,执行异步的任务,并返回结果。
addAccount()中的匿名子类实现了AmsTask.doWork()方法:
[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();
}
在doWork()方法的实现中,调用AccountManager持有的AccountManagerService的代理对象(mService)向AccountManagerService发起IPC。
2. AccountManger调用AmsTask匿名子类的start()方法启动任务。
3. start()方易做图调用本类的doWork()方法,在这里就是执行AccountManagerService的addAccount()操作。
4. 根据FutureTask的实现机制,在任务执行的结束时期,会调用本类的done()方法。AmsTask类覆盖了这个方法:
[java] protected void done() {
if (mCallback != null) {
postToHandler(mHandler, mCallback, this);
}
}
protected void done() {
if (mCallback != null) {
postToHandler(mHandler, mCallback, this);
}
}
这里的实现调用了AccountManager.postHandler()方法。看名字就可以猜到,这里将mCallback回调对象里面的run()方法传送给主线程的handler进行调用:
[java] private void postToHandler(Handler handler, final AccountManagerCallback<Bundle> callback,
final AccountManagerFuture<Bundle> future) {
handler = handler == null ? mMainHandler : handler;
补充:移动开发 , Android ,