通讯节点: /dev/binder
何时打开:一般在ProcessState::self()
何时用:透过BBinder, BpBinder
管理者BpServiceManager , BnServiceManager
它们会通过IPCThreadState::transact()等进行操作, handle在这里指定。结构binder_transaction_data 用来读写数据。 remote()->transact()就是BpBinder->transact(), 再到IPCThreadState::transact()
BpBinder不在继承关系里,只是打包数据。
Binder 通讯Thread何时起来:
某个service死掉,要如何知道:
ServiceManager的handle 是0
int main(int argc, char** argv)
{
sp<ProcessState> proc(ProcessState::self());
sp<IServiceManager> sm = defaultServiceManager();
MediaPlayerService::instantiate();
ProcessState::self()->startThreadPool();
IPCThreadState::self()->joinThreadPool();
}
C++层自己写个process 来启动特定的service.
在Java层,写好service后,可以通过在SystemServer中启动一个thread来启动service
Framework service需要aidl文件,而native service(如media.player)则不需要。
sMediaPlayerService = interface_cast<IMediaPlayerService>(binder);是对应IMPLEMENT_META_INTERFACE(MediaPlayerService, "android.media.IMediaPlayerService");里的asInterface(): 实现从IBinder 到BpMediaPlayerService(obj)的转换,一般的,如果obj为0 表示service manager本身的handle. 注意,该转换在local和remote的地方,是不同的。 如下示例:
void MediaPlayerService::instantiate() { defaultServiceManager()->addService( String16("media.player"), new MediaPlayerService()); }
//获取ServiceManager的代理对象 sp<IServiceManager> sm = defaultServiceManager(); //通过ServiceManager获取media Service binder binder = sm->getService(String16("media.player")); //将binder封装 构造media Service代理对象 BpMediaPlayerService sMediaPlayerService = interface_cast<IMediaPlayerService>(binder);
参考: http://blog.csdn.net/luoshengyang/article/details/6621566
http://www.cnblogs.com/innost/archive/2011/01/09/1931456.html
http://www.cnblogs.com/bastard/archive/2012/11/13/2766611.html
http://blog.csdn.net/zbunix/article/details/8758631
来源:oschina
链接:https://my.oschina.net/u/996206/blog/162484