返回
Binder: addService初探之创建过程剖析
Android
2024-02-11 15:54:45
在Binder: ServiceManager的获取文章中,我们分析了ProcessState与IPCThreadState的创建过程。最后在defaultServiceManager中,返回的是持有BpBinder的BpServiceManager对象。下面我们直接看BpServiceManager的构造过程:
```java
public BpServiceManager(BpBinder binder) {
this.mBinder = binder;
}
它的构造函数只是简单的将BpBinder类型的binder字段赋值给mBinder字段,因此我们有必要看下BpBinder的构造函数:
```java
public BpBinder(String descriptor) {
this(new BinderImpl(), descriptor);
}
它的构造函数调用了另一个构造函数:
```java
public BpBinder(BinderImpl impl, String descriptor) {
mImpl = impl;
mDescriptor = descriptor;
mBinder = null;
mServiceManager = null;
}
这个构造函数中,又调用了BinderImpl的构造函数:
```java
public BinderImpl() {
mWeakThreadState = null;
mObjects = new ArrayList<>();
mHasStrongRef = false;
}
至此,BpServiceManager、BpBinder和BinderImpl的构造过程都已分析完毕,整个addService的创建过程也随之分析完毕。
从整个addService的创建过程可以看出,Binder框架的设计非常巧妙,它将Binder对象、ServiceManager对象和ProcessState对象等复杂的概念抽象成了简单的类,并通过这些类之间的相互协作来实现了Binder框架的功能。
Binder框架是一个非常重要的Android系统组件,它为Android应用程序提供了跨进程通信的机制。通过Binder框架,应用程序可以与系统服务进行交互,也可以与其他应用程序进行通信。Binder框架的设计非常高效,它可以支持大量并发请求,并且可以提供很高的吞吐量。
Binder框架是一个非常复杂的系统组件,它的实现细节非常多。本文只是简单介绍了Binder框架中addService的创建过程,希望读者能够对Binder框架有一个初步的了解。