- AccountManager是一个面向应用程序开发的组件。它提供一组对应于IAccountManager协议的应用程序接口。这组接口通过Binder机制与系统服务AccountManagerService进行通信,协作完成帐户相关的操作。同时,AccountManager接收应用程序提供的回调,以此在帐号操作完成之后向应用程序返回对应的结果,同时触发应用程序层对这个结果的处理。
以addAccount()操作为例,步骤如下:
1. AccountManager初始化一个匿名的AmsTask子类实例。AmsTask是AccountManager的内部类:
private abstract class AmsTask extends FutureTask implements AccountManagerFuture {
final IAccountManagerResponse mResponse;
final Handler mHandler;
final AccountManagerCallback mCallback;
final Activity mActivity;
public AmsTask(Activity activity, Handler handler, AccountManagerCallback callback) {
super(new Callable() {
public Bundle call() throws Exception {
throw new IllegalStateException("this should never be called");
}
});
mHandler = handler;
mCallback = callback;
mActivity = activity;
mResponse = new Response();
}
...
public AccountManagerFuture addAccount(final String accountType,
final String authTokenType, final String[] requiredFeatures,
final Bundle addAccountOptions,
final Activity activity, AccountManagerCallback callback, Handler handler) {
...
return new AmsTask(activity, handler, callback) {
public void doWork() throws RemoteException {
mService.addAcount(mResponse, accountType, authTokenType,
requiredFeatures, activity != null, optionsIn);
}
}.start();
}
protected void done() {
if (mCallback != null) {
postToHandler(mHandler, mCallback, this);
}
}
private void postToHandler(Handler handler, final AccountManagerCallback callback,
final AccountManagerFuture future) {
handler = handler == null ? mMainHandler : handler;
handler.post(new Runnable() {
public void run() {
callback.run(future);
}
});
}