本文共 9385 字,大约阅读时间需要 31 分钟。
前言:Binder的中文意思是“别针”/“回形针”,顾名思义,Binder的作用在于将不同的进程之间“别”在一起,完成IPC(Inter-process communication进程间通信)过程,那么这个Binder究竟是什么东西呢?原理又是什么呢?aidl又是必要的吗?本地调用和远程调用又有什么区别呢?接下来让我们一起来揭开其神秘面纱,在此谢过《Android内核剖析》一书,其中多处引自此书。aidl见下一篇博文。
public class MyServer extends Binder { /** * Binder父类的一个方法,用于区分客户端的请求,决定调用哪一个服务端的方法 */ @Override protected boolean onTransact(int code, Parcel data, Parcel reply, int flags) throws RemoteException { switch (code) { case 1000: service1(); break; case 1001: data.enforceInterface("MyServer"); String arg = data.readString(); service2(arg); break; default: break; } return super.onTransact(code, data, reply, flags); } public void service1() { System.out.println("----service1---"); } public void service2(String arg) { System.out.println("----service2---the arg is :" + arg); }}注意这里我们用了一个全 局MyServer对象。用于验证客户端获取到的Binder对象和服务端Binder对象是否是同一个。
public class ServerService extends Service { public static MyServer myServer = new MyServer(); @Override public void onCreate() { Log.i("DEBUG", "---onCreate()---"); super.onCreate(); } @Override public IBinder onBind(Intent intent) { Log.i("DEBUG", "---onBind()---"); return myServer; } @Override public boolean onUnbind(Intent intent) { Log.i("DEBUG", "---onUnbind()---"); return super.onUnbind(intent); }}客户端:
public class ClientActivity extends Activity { private Button btn_connectService; private Button btn_disconnectService; private Button btn_getService1; private Button btn_getService2; // 服务端Service的action,根据自己而变 private Intent intent = new Intent("com.wwt.server.ServerService"); private IBinder mIBinder; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 绑定服务端 btn_connectService = (Button) findViewById(R.id.btn_connectService); btn_connectService.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { bindService(intent, connection, Context.BIND_AUTO_CREATE); } }); // 取消绑定服务 btn_disconnectService = (Button) findViewById(R.id.btn_disconnectService); btn_disconnectService.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { unbindService(connection); } }); // 获取服务端服务1 btn_getService1 = (Button) findViewById(R.id.btn_getService1); btn_getService1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { int code = 1000; Parcel data = Parcel.obtain(); Parcel reply = Parcel.obtain(); // 用于对访问的服务端Binder进行验证 data.writeInterfaceToken("MyServer"); try { mIBinder.transact(code, data, reply, 0); } catch (RemoteException e) { e.printStackTrace(); } finally { data.recycle(); reply.recycle(); } } }); // 获取服务端服务2 btn_getService2 = (Button) findViewById(R.id.btn_getService2); btn_getService2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { int code = 1001; Parcel data = Parcel.obtain(); Parcel reply = Parcel.obtain(); // 用于对访问的服务端Binder进行验证 data.writeInterfaceToken("MyServer"); data.writeString("hello"); try { mIBinder.transact(code, data, reply, 0); } catch (RemoteException e) { e.printStackTrace(); } finally { data.recycle(); reply.recycle(); } } }); } /** * 将会获得从服务端传来的Binder对象 */ private ServiceConnection connection = new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName name) { } @Override public void onServiceConnected(ComponentName name, IBinder service) { mIBinder = service; if (mIBinder == ServerService.myServer) { System.out.println("客户端获取到了服务端的Binder对象引用"); } } };}运行结果:
public class MyServer extends Binder { @Override protected boolean onTransact(int code, Parcel data, Parcel reply, int flags) throws RemoteException { switch (code) { case 1000: service1(); break; case 1001: data.enforceInterface("MyServer");//用于验证客户端确实想访问这个服务端 String arg = data.readString(); service2(arg); break; default: break; } return super.onTransact(code, data, reply, flags); } public void service1() { System.out.println("----service1---"); } public void service2(String arg) { System.out.println("----service2---the arg is :" + arg); } }
public class ServerService extends Service { @Override public void onCreate() { Log.i("DEBUG", "---onCreate()---"); super.onCreate(); } @Override public IBinder onBind(Intent intent) { Log.i("DEBUG", "---onBind()---"); return new MyServer();//当客户端请求的时候,将服务端Binder返回。 } @Override public boolean onUnbind(Intent intent) { Log.i("DEBUG", "---onUnbind()---"); return super.onUnbind(intent); }}位于另一应用中的客户端:
public class ClientActivity extends Activity { private Button btn_connectService; private Button btn_disconnectService; private Button btn_getService1; private Button btn_getService2; //服务端Service的action,根据自己而变 private Intent intent = new Intent("com.wwt.server.ServerService"); private IBinder mIBinder; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 绑定服务端 btn_connectService = (Button) findViewById(R.id.btn_connectService); btn_connectService.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { bindService(intent, connection, Context.BIND_AUTO_CREATE); } }); // 取消绑定服务 btn_disconnectService = (Button) findViewById(R.id.btn_disconnectService); btn_disconnectService.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { unbindService(connection); } }); // 获取服务端服务1 btn_getService1 = (Button) findViewById(R.id.btn_getService1); btn_getService1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { int code = 1000; Parcel data = Parcel.obtain(); Parcel reply = Parcel.obtain(); // 用于对访问的服务端Binder进行验证 data.writeInterfaceToken("MyServer"); try { mIBinder.transact(code, data, reply, 0); } catch (RemoteException e) { e.printStackTrace(); } finally { data.recycle(); reply.recycle(); } } }); // 获取服务端服务2 btn_getService2 = (Button) findViewById(R.id.btn_getService2); btn_getService2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { int code = 1001; Parcel data = Parcel.obtain(); Parcel reply = Parcel.obtain(); // 用于对访问的服务端Binder进行验证 data.writeInterfaceToken("MyServer"); data.writeString("hello"); try { mIBinder.transact(code, data, reply, 0); } catch (RemoteException e) { e.printStackTrace(); } finally { data.recycle(); reply.recycle(); } } }); } /** * 将会获得从服务端传来的Binder对象(Binder驱动中的) */ private ServiceConnection connection = new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName name) { } @Override public void onServiceConnected(ComponentName name, IBinder service) { mIBinder = service; } };}运行结果: