Binder Client
有了Binder Driver、ServiceManager以及Android系统专门面对应用开发提供的Binder封装,才能使应用程序之间顺利进行无缝通信。从四大组建中可以看出:
Activity:通过startActivity可以启动目标进程
Service:任何应用程序都可以通过startService和bindService来启动特定服务
Broadcast:任何应用程序都可以通过sendBroadcast来发送一个广播,且无论广播处理是否在同一进程中
Intent
bindService启动Service的步骤:
应用程序填写Intent,调用bindService发送请求
收到请求的bindService(此时还在应用程序的运行空间)将与ActivityManagerService(AMS)取得联系。为了获得AMS的Binder句柄,事先调用ServiceManager.getService,这里就涉及到进程间通信了。在得到AMS的句柄后,程序才能真正向它发送请求。
AMS基于特定的最有匹配策略,从其内部存储的系统所有服务组件中找到与Intent最匹配的一个,然后向它发送Service绑定请求。如果目标进程还不存在的话,AMS还要负责把它启动。
“被绑定”的服务进程需响应绑定,执行具体操作,并在成功后通知AMS。再由AMS回调发起请求的应用程序(回调接口是ServiceConnection)。
Context的相关继承关系:
Activity继承关系的“根”是Context,bindService即包含在Context中,Context只是提供了抽象的接口:
/*\Android4.4\frameworks\base\core\java\android\content\Context.java*/
public abstract boolean bindService(Intent service, ServiceConnection conn, int flags); |
具体实现在ContextWrapper中:
/*\Android4.4\frameworks\base\core\java\android\content\Context.java*/
@Override public boolean bindService(Intent service, ServiceConnection conn, int flags) { return mBase.bindService(service, conn, flags); } |
mBase也是一个Context对象,用ContexImpl实现(这里暂时留下疑问),bindService的实现如下:
/*\Android4.4\frameworks\base\core\java\android\app\ContextImpl.java*/
@Override public boolean bindService(Intent service, ServiceConnection conn, int flags) { warnIfCallingFromSystemProcess(); return bindServiceCommon(service, conn, flags, Process.myUserHandle()); }
/** @hide */ @Override public boolean bindServiceAsUser(Intent service, ServiceConnection conn, int flags, UserHandle user) { return bindServiceCommon(service, conn, flags, user); } private boolean bindServiceCommon(Intent service, ServiceConnection conn, int flags, UserHandle user) { … int res = ActivityManagerNative.getDefault().bindService( mMainThread.getApplicationThread(), getActivityToken(), service, service.resolveTypeIfNeeded(getContentResolver()), sd, flags, user.getIdentifier()); … }
|
和ServiceManager一样,AMS也同样提供了ActivityManagerNative和ActivityManagerProxy:
/*\Android4.4\frameworks\base\core\java\android\app\ActivityManagerNative.java*/
static public IActivityManager getDefault() { return gDefault.get(); } private static final Singleton<IActivityManager> gDefault = new Singleton<IActivityManager>() {à单实例,对象只被创建一次 protected IActivityManager create() { IBinder b = ServiceManager.getService("activity");à通过ServiceManager取得AMS的IBinder对象 if (false) { Log.v("ActivityManager", "default service binder = " + b); } IActivityManager am = asInterface(b);à创建一个可用的ActivityManagerProxy if (false) { Log.v("ActivityManager", "default service = " + am); } return am; } }; |
ActivityManagerNative的作用之一,就是帮助调用者快速取得一个ActivityManagerProxy。
在ActivityManagerProxy中有bindService的实现:
/*\Android4.4\frameworks\base\core\java\android\app\ActivityManagerNative.java*/
public ActivityManagerProxy(IBinder remote)à构造函数中remote是上面通过ServiceManager.getService("activity");获取的 { mRemote = remote; } public int bindService(IApplicationThread caller, IBinder token, Intent service, String resolvedType, IServiceConnection connection, int flags, int userId) throws RemoteException { Parcel data = Parcel.obtain(); Parcel reply = Parcel.obtain(); data.writeInterfaceToken(IActivityManager.descriptor); data.writeStrongBinder(caller != null ? caller.asBinder() : null); data.writeStrongBinder(token); service.writeToParcel(data, 0); data.writeString(resolvedType); data.writeStrongBinder(connection.asBinder()); data.writeInt(flags); data.writeInt(userId); mRemote.transact(BIND_SERVICE_TRANSACTION, data, reply, 0); reply.readException(); int res = reply.readInt(); data.recycle(); reply.recycle(); return res;
|
ActivityManagerNative的另一个作用是为ActivityManagerService的实现提供便利。可见在ActivityManagerNative中有如下代码:
/*\Android4.4\frameworks\base\core\java\android\app\ActivityManagerNative.java*/
@Override public boolean onTransact(int code, Parcel data, Parcel reply, int flags) throws RemoteException { switch (code) { case START_ACTIVITY_TRANSACTION: { data.enforceInterface(IActivityManager.descriptor); IBinder b = data.readStrongBinder(); IApplicationThread app = ApplicationThreadNative.asInterface(b); String callingPackage = data.readString(); Intent intent = Intent.CREATOR.createFromParcel(data); String resolvedType = data.readString(); IBinder resultTo = data.readStrongBinder(); String resultWho = data.readString(); int requestCode = data.readInt(); int startFlags = data.readInt(); String profileFile = data.readString(); ParcelFileDescriptor profileFd = data.readInt() != 0 ? data.readFileDescriptor() : null; Bundle options = data.readInt() != 0 ? Bundle.CREATOR.createFromParcel(data) : null; int result = startActivity(app, callingPackage, intent, resolvedType, resultTo, resultWho, requestCode, startFlags, profileFile, profileFd, options); reply.writeNoException(); reply.writeInt(result); return true; } … } |
ActivityManagerService继承自ActivityManagerNative,所以ActivityManagerNative既是面向调用者,也是面向服务本身的。
ActivityManagerService的内容异常繁杂,内容还有很多,暂时先学习到这里,与ServiceManager发生了联系。