Binder 是什么?
BInder 是IPC进程间通讯机制(IPC是Inter-Process Communication的缩写,是进程间通信或者跨进程通信的意思,既然说到进程,大家要区分一下进程和线程,进程一般指的是一个执行单元,它拥有独立的地址空间,也就是一个应用或者一个程序。线程是CPU调度的最小单元,是进程中的一个执行部分或者说是执行体,两者之间是包含与被包含的关系。因为进程间的资源不能共享的,所以每个系统都有自己的IPC机制,Android是基于Linux内核的移动操作系统,但它并没有继承Linux的IPC机制,而是有着自己的一套IPC机制。 )
什么时候需要用到进程间通信?
webView进程隔离,图片下载,推送等
为什么要多进程?
虚拟机给应用分配的内存是有限制的,保证主程序的稳定
进程间通信为什么要用到Binder机制?
内存划分为内核空间和用户空间,用户空间没办法直接访问,需要通过Binder进行通信


Binder就是Android中最具特色的IPC方式,AIDL其实就是通过Binder实现的,因为在我们定义好aidl文件后,studio就帮我们生成了相关的Binder类。事实上我们在使用AIDL时候继承的Stub类,就是studio帮我们生成的Binder类,所以我们可以通过查看studio生成的代码来了解Binder的工作原理。
AIDL 简单实现介绍
1.服务端穿件aidl文件 同时拷贝到客户端一份 (sycn project一下)

package com.xx.leo_service;
// Declare any non-default types here with import statements
import com.xx.leo_service.Person;
//创建服务端 aidl 文件
interface ILeoAidl {
//提供两个方法
void addPerson(in Person person);
List<Person> getPersonList();
}
// Person.aidl
package com.xx.leo_service;
// Declare any non-default types here with import statements
parcelable Person;
package com.xx.leo_service;
import android.os.Parcel;
import android.os.Parcelable;
public class Person implements Parcelable {
private String name;
private int grade;
public Person(String name, int grade) {
this.name = name;
this.grade = grade;
}
protected Person(Parcel in) {
this.name = in.readString();
this.grade = in.readInt();
}
public static final Creator<Person> CREATOR = new Creator<Person>() {
@Override
public Person createFromParcel(Parcel in) {
return new Person(in);
}
@Override
public Person[] newArray(int size) {
return new Person[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeInt(grade);
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", grade=" + grade +
'}';
}
}
2.创建 服务端 service
package com.xx.leo_service;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.annotation.Nullable;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
public class LeoAidlService extends Service {
private ArrayList<Person> persons;
@Nullable
@Override
public IBinder onBind(Intent intent) {//返归内部类的实例
persons = new ArrayList<>();
Log.e("LeoAidlService", "success onBind");
return iBinder;
}
//在service里面创建一个内部类,继承你刚才创建的ILeoAidl.aidl的名称里的Stub类,
// 并实现接口方法,在onBind返回内部类的实例
private IBinder iBinder = new ILeoAidl.Stub() {
@Override
public void addPerson(Person person) throws RemoteException {
persons.add(person);
}
@Override
public List<Person> getPersonList() throws RemoteException {
return persons;
}
};
@Override
public void onCreate() {
super.onCreate();
Log.e("LeoAidlService", "onCreate: success");
}
}
这边我们通过隐式意图来绑定service,在onServiceConnected方法中通过ILeoAidl.Stub.asInterface(service)获取ILeoAidl对象,然后在onClick中调用ILeoAidl.addPerson()和ILeoAidl.getPersonList()。
package com.xx.leo_client;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import com.xx.leo_service.ILeoAidl;
import com.xx.leo_service.Person;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private final static String TAG = "MainActivity";
private ILeoAidl iLeoAidl;
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
bindService();
}
private void initView() {
btn = (Button) findViewById(R.id.but_click);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
iLeoAidl.addPerson(new Person("leo", 3));
List<Person> persons = iLeoAidl.getPersonList();
Log.e(TAG, persons.toString());
} catch (RemoteException e) {
e.printStackTrace();
}
}
});
}
private void bindService() {
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.xx.leo_service", "com.xx.leo_service.LeoAidlService"));
bindService(intent, connection, Context.BIND_AUTO_CREATE);
}
private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.e(TAG, "onServiceConnected: success");
iLeoAidl = ILeoAidl.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.e(TAG, "onServiceDisconnected: success");
iLeoAidl = null;
}
};
@Override
protected void onDestroy() {
super.onDestroy();
unbindService(connection);
}
}
整个aidl的调用流程
