進程隔離是爲保護操做系統中進程互不干擾而設計的一組不一樣硬件和軟件的技術。進程數據不共享,進程A的虛擬地址和進程B的虛擬地址不一樣,這樣就防止進程A將數據信息寫入進程B,保證了數據的安全性。 java
Linux 下的傳統 IPC 通訊原理: android
傳輸過程:(2)穩定性緩存
(3)安全性安全
Binder IPC正是基於內存映射(mmap)來實現的 bash
Binder通訊過程:(1)生成AIDL接口(new->AIDL->AIDL File)架構
interface MyWindowManager {
void sysout();
}
複製代碼
生成AIDL文件以後,比起之前多了一個叫作 aidl 的包,並且他的層級是和 java 包相同的。
(2)編譯MyWindowManager.aidl生成Java文件ide
public interface MyWindowManager extends android.os.IInterface {
/** Local-side IPC implementation stub class. */
//Stub 繼承 Binder, 說明它是一個 Binder 本地對象;實現 IInterface 接口,代表Server能夠提供的方法
public static abstract class Stub extends android.os.Binder
implements com.example.myview.binder.MyWindowManager {
private static final java.lang.String DESCRIPTOR = "com.example.myview.binder.MyWindowManager";
/** Construct the stub at attach it to the interface. */
public Stub() {
this.attachInterface(this, DESCRIPTOR);
}
public static com.example.myview.binder.MyWindowManager asInterface(android.os.IBinder obj) {
if ((obj == null)) {
return null;
}
android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
if (((iin != null) && (iin instanceof com.example.myview.binder.MyWindowManager))) {
return ((com.example.myview.binder.MyWindowManager) iin);
}
return new com.example.myview.binder.MyWindowManager.Stub.Proxy(obj);
}
@Override
public android.os.IBinder asBinder() {
return this;
}
@Override
public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags)
throws android.os.RemoteException {
......
return super.onTransact(code, data, reply, flags);
}
//Binder本地代理對象
private static class Proxy implements com.example.myview.binder.MyWindowManager {
private android.os.IBinder mRemote;
Proxy(android.os.IBinder remote) {
mRemote = remote;
}
@Override
public android.os.IBinder asBinder() {
return mRemote;
}
public java.lang.String getInterfaceDescriptor() {
return DESCRIPTOR;
}
@Override
public void sysout() throws android.os.RemoteException {
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
try {
_data.writeInterfaceToken(DESCRIPTOR);
mRemote.transact(Stub.TRANSACTION_sysout, _data, _reply, 0);
_reply.readException();
} finally {
_reply.recycle();
_data.recycle();
}
}
}
static final int TRANSACTION_basicTypes = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
static final int TRANSACTION_sysout = (android.os.IBinder.FIRST_CALL_TRANSACTION + 1);
}
public void sysout() throws android.os.RemoteException;
}
複製代碼
(3)Server端提供方法的具體實現工具
public class MyWindowManagerService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
return mWindowManager;
}
private final MyWindowManager.Stub mWindowManager = new MyWindowManager.Stub() {
@Override
public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble,
String aString) throws RemoteException {
}
@Override
public void sysout() throws RemoteException {
Log.e("hj", "sysout: " );
}
};
}
複製代碼
(4)其餘進程的Activity實現跟Service的通訊性能
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(this, MyWindowManagerService.class);
bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}
ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
MyWindowManager windowManager = MyWindowManager.Stub.asInterface(service);
try {
windowManager.sysout();
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
}
複製代碼
參考文章:
Android Binder設計與實現 - 設計篇
爲何 Android 要採用 Binder 做爲 IPC 機制?
寫給 Android 應用工程師的 Binder 原理剖析ui