diff --git a/blogs/Android/Framework/源代码情景分析/四大组件的启动过程/Service 组件的启动过程.md b/blogs/Android/Framework/源代码情景分析/四大组件的启动过程/Service 组件的启动过程.md index 3cfdf37..ff8826d 100644 --- a/blogs/Android/Framework/源代码情景分析/四大组件的启动过程/Service 组件的启动过程.md +++ b/blogs/Android/Framework/源代码情景分析/四大组件的启动过程/Service 组件的启动过程.md @@ -454,5 +454,54 @@ ConnectionRecord 类的成员变量 conn 是一个类型为 IServiceConnection ```java // LoadedApk +static final class ServiceDispatcher { + private static class InnerConnection extends IServiceConnection.Stub { + final WeakReference mDispatcher; + + public void connected(ComponentName name, IBinder service) { + LoadedApk.ServiceDispatcher sd = mDispatcher.get(); + sd.connected(name, service); + } + } +} +``` + +有调到了 ServiceDispatcher 的 connected 方法: + +```java +// LoadedApk +static final class ServiceDispatcher { + private final Handler mActivityThread; + + public void connected(ComponentName name, IBinder service) { + mActivityThread.post(new RunConnection(name, service, 0)); + } + + static final class RunConnection implements Runnable { + RunConnection(ComponentName name, IBinder service, int command) { + mName = name; + mService = service; + mCommand = command; + } + + public void run() { + doConnected(mName, mService); + } + + final ComponentName mName; + final IBinder mService; + final int mCommand; + } +} +``` + +调用 ServiceDispatcher 类的成员函数 doConnected 将 RunConnection 类内部成员函数 mService 所描述的一个 Binder 本地对象传递给 Activity 组件。 + +```java +// ServiceDispatcher +public void doConnected(ComponentName name, IBinder service) { + mConnection.onServiceConnected(name, service); +} ``` +至此,Activity 组件绑定 Service 组件的过程就分析完成了,Activity 组件获得了 Service 组件的访问接口之后,就可以调用相关服务了。 \ No newline at end of file