From dce40f9925ad27f3e11b837ab00d437d5c334ceb Mon Sep 17 00:00:00 2001 From: Omooo <869759698@qq.com> Date: Fri, 29 May 2020 07:57:11 +0800 Subject: [PATCH] =?UTF-8?q?Update=20Service=20=E7=BB=84=E4=BB=B6=E7=9A=84?= =?UTF-8?q?=E5=90=AF=E5=8A=A8=E8=BF=87=E7=A8=8B.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Service 组件的启动过程.md | 46 ++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/blogs/Android/Framework/源代码情景分析/四大组件的启动过程/Service 组件的启动过程.md b/blogs/Android/Framework/源代码情景分析/四大组件的启动过程/Service 组件的启动过程.md index 7c40a15..3cfdf37 100644 --- a/blogs/Android/Framework/源代码情景分析/四大组件的启动过程/Service 组件的启动过程.md +++ b/blogs/Android/Framework/源代码情景分析/四大组件的启动过程/Service 组件的启动过程.md @@ -411,4 +411,48 @@ private final void handleBindService(BindServiceData data) { } ``` -BindServiceData 对象 data 的成员变量 token 指向了一个 Binder 代理对象,它引用了 AMS 中的一个 ServiceRecord 对象,而这个 ServiceRecord 对象是用来描述应用程序的 Service 组件的。 \ No newline at end of file +BindServiceData 对象 data 的成员变量 token 指向了一个 Binder 代理对象,它引用了 AMS 中的一个 ServiceRecord 对象,而这个 ServiceRecord 对象是用来描述应用程序的 Service 组件的。 + +首先通过 Service 的 onBind 函数获取一个 Binder 本地对象,然后通过 publishService 将它传给 AMS。 + +```java +// ActivityManagerNative +public void publishService(IBinder token, Intent intent, IBinder service) { + Parcel data = Parcel.obtain(); + data.writeInterfaceToken(IActivityManager.descriptor); + data.writeStrongBinder(token); + mRemote.transact(PUBLISH_SERVICE_TRANSACTION, data, reply, 0); +} +``` + +在 AMS 中处理该请求: + +```java +// AMS +public void publishService(IBinder token, Intent intent, IBinder service) { + ServiceRecord r = (ServiceRecord)token; + Intent.FilterComparison filter = new Intent.FilterComparison(intent); + IntentBindRecord b = r.bindings.get(filter); + IntentBindRecord b = r.bindings.get(filter); + b.binder = service; + b.requested = true; + b.received = true; + Iterator> it = r.connections.values().iterator(); + while (it.hasNext()) { + ArrayList clist = it.next(); + for (int i=0; i