但是:
客户端使用服务
进程之间无法进行直接通信,所以通过Binder驱动
客户端和服务端不需要了解binder协议,所以使用代理和存根
客户端不想要知道正在使用IPC,也不关心binder和代理,所以,需要管理对象进行抽象
但是客户端怎样获取它想要通信的服务的handle,只需要问问sevicemanager(Context Manager),服务是否已经注册
最后,我们看下总体的架构
使用aidl实现跨进程的加减法
新建android工程,创建包com.realize.calc.aidl,新建文件ICalcAIDL.aidl,内容如下
package com.realize.calc.aidl; interface ICalcAIDL { int add(int x , int y); int min(int x , int y ); }
package com.realize.lizijun.binder_server; import com.realize.calc.aidl.ICalcAIDL; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.os.RemoteException; import android.util.Log; public class CalcService extends Service { private static final String TAG = "server"; public void onCreate() { Log.e(TAG, "onCreate"); } public IBinder onBind(Intent t) { Log.e(TAG, "onBind"); return mBinder; } public void onDestroy() { Log.e(TAG, "onDestroy"); super.onDestroy(); } public boolean onUnbind(Intent intent) { Log.e(TAG, "onUnbind"); return super.onUnbind(intent); } public void onRebind(Intent intent) { Log.e(TAG, "onRebind"); super.onRebind(intent); } private final ICalcAIDL.Stub mBinder = new ICalcAIDL.Stub() { @Override public int add(int x, int y) throws RemoteException { return x + y; } @Override public int min(int x, int y) throws RemoteException { return x - y; } }; }
<service android:name="com.realize.lizijun.binder_server.CalcService" > <intent-filter> <action android:name="com.realize.calc.aidl" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </service>
新建android工程,创建包com.realize.calc.aidl,新建文件ICalcAIDL.aidl(与服务端是一样的),内容如下
package com.realize.calc.aidl; interface ICalcAIDL { int add(int x , int y); int min(int x , int y ); }
package com.realize.lizijun.binder_client; import android.app.Activity; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.util.Log; import android.view.View; import android.widget.Toast; import com.realize.calc.aidl.ICalcAIDL; public class MainActivity extends Activity { private static final String TAG = "client"; private ICalcAIDL mCalcAidl; private ServiceConnection mServiceConn = new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName name) { Log.e(TAG, "onServiceDisconnected"); mCalcAidl = null; } @Override public void onServiceConnected(ComponentName name, IBinder service) { Log.e(TAG, "onServiceConnected"); mCalcAidl = ICalcAIDL.Stub.asInterface(service); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } /** * 点击BindService按钮时调用 * @param view */ public void bindService(View view) { Intent intent = new Intent(); intent.setAction("com.realize.calc.aidl"); bindService(intent, mServiceConn, Context.BIND_AUTO_CREATE); } /** * 点击unBindService按钮时调用 * @param view */ public void unbindService(View view) { unbindService(mServiceConn); } /** * 点击12+12按钮时调用 * @param view */ public void addInvoked(View view) throws Exception { if (mCalcAidl != null) { int addRes = mCalcAidl.add(12, 12); Toast.makeText(this, addRes + "", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "服务器被异常杀死,请重新绑定服务端", Toast.LENGTH_SHORT) .show(); } } /** * 点击50-12按钮时调用 * @param view */ public void minInvoked(View view) throws Exception { if (mCalcAidl != null) { int addRes = mCalcAidl.min(50, 12); Toast.makeText(this, addRes + "", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "服务器未绑定或被异常杀死,请重新绑定服务端", Toast.LENGTH_SHORT) .show(); } } }
上面创建ICalcAIDL.aidl之后,在gen目录下回生成文件ICalcAIDL.java, 该文件实现了客户端和服务端的代理(proxy)和存根(stub)
服务端代码中调用ICalcAIDL.Stub
private final ICalcAIDL.Stub mBinder = new ICalcAIDL.Stub() { @Override public int add(int x, int y) throws RemoteException { return x + y; } @Override public int min(int x, int y) throws RemoteException { return x - y; } };
public static abstract class Stub extends android.os.Binder implements com.realize.calc.aidl.ICalcAIDL
@Override public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException { switch (code) { case INTERFACE_TRANSACTION: { reply.writeString(DESCRIPTOR); return true; } case TRANSACTION_add: { data.enforceInterface(DESCRIPTOR); int _arg0; _arg0 = data.readInt(); int _arg1; _arg1 = data.readInt(); int _result = this.add(_arg0, _arg1); reply.writeNoException(); reply.writeInt(_result); return true; } case TRANSACTION_min: { data.enforceInterface(DESCRIPTOR); int _arg0; _arg0 = data.readInt(); int _arg1; _arg1 = data.readInt(); int _result = this.min(_arg0, _arg1); reply.writeNoException(); reply.writeInt(_result); return true; } } return super.onTransact(code, data, reply, flags); } 服务端会根据客户端发送来的消息执行onTransact方法,该方法有四个参数 code 是一个整形的唯一标识,用于区分执行哪个方法 data 客户端传递过来的参数 reply 服务器返回的值 flags 标明是否有返回值,0为有(双向),1为没有(单向)
客户端代码中,调用了ICalcAIDL.Stub.asInterface
private ServiceConnection mServiceConn = new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName name) { Log.e(TAG, "onServiceDisconnected"); mCalcAidl = null; } @Override public void onServiceConnected(ComponentName name, IBinder service) { Log.e(TAG, "onServiceConnected"); mCalcAidl = ICalcAIDL.Stub.asInterface(service); } };
public static com.realize.calc.aidl.ICalcAIDL asInterface(android.os.IBinder obj) { if ((obj==null)) { return null; } android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR); if (((iin!=null)&&(iin instanceof com.realize.calc.aidl.ICalcAIDL))) { return ((com.realize.calc.aidl.ICalcAIDL)iin); } return new com.realize.calc.aidl.ICalcAIDL.Stub.Proxy(obj); }
@Override public int add(int x, int y) throws android.os.RemoteException { android.os.Parcel _data = android.os.Parcel.obtain(); android.os.Parcel _reply = android.os.Parcel.obtain(); int _result; try { _data.writeInterfaceToken(DESCRIPTOR); _data.writeInt(x); _data.writeInt(y); mRemote.transact(Stub.TRANSACTION_add, _data, _reply, 0); _reply.readException(); _result = _reply.readInt(); } finally { _reply.recycle(); _data.recycle(); } return _result; } 声明了两个android.os.Parcel对象,_data用于传递数据,_replay用于接收返回的数据 最后调用transact方法,与服务端进行通信
怎样不通过AIDL文件,实现跨进程通信呢,从上面的分析中,我们可以知道,不通过AIDL文件,实现跨进程通信,那么实际上,就是要实现自动生成的AIDL文件中的接口功能, 下面我们实现跨进程的乘除调用
创建工程,实现CalcService.java,代码如下
package com.realize.lizijun.noaidl_binder_server; import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.IBinder; import android.os.Parcel; import android.os.RemoteException; import android.util.Log; public class CalcService extends Service { private static final String DESCRIPTOR = "CalcService"; private static final String TAG = "server"; public void onCreate() { Log.e(TAG, "onCreate"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.e(TAG, "onStartCommand"); return super.onStartCommand(intent, flags, startId); } public IBinder onBind(Intent t) { Log.e(TAG, "onBind"); return mBinder; } public void onDestroy() { Log.e(TAG, "onDestroy"); super.onDestroy(); } public boolean onUnbind(Intent intent) { Log.e(TAG, "onUnbind"); return super.onUnbind(intent); } public void onRebind(Intent intent) { Log.e(TAG, "onRebind"); super.onRebind(intent); } private MyBinder mBinder = new MyBinder(); private class MyBinder extends Binder { @Override protected boolean onTransact(int code, Parcel data, Parcel reply, int flags) throws RemoteException { switch (code) { case 0x110: { Log.e(TAG, "0x110"); data.enforceInterface(DESCRIPTOR); int _arg0; _arg0 = data.readInt(); int _arg1; _arg1 = data.readInt(); int _result = _arg0 * _arg1; reply.writeNoException(); reply.writeInt(_result); return true; } case 0x111: { Log.e(TAG, "0x111"); data.enforceInterface(DESCRIPTOR); int _arg0; _arg0 = data.readInt(); int _arg1; _arg1 = data.readInt(); int _result = _arg0 / _arg1; reply.writeNoException(); reply.writeInt(_result); return true; } } return super.onTransact(code, data, reply, flags); } }; } 自定义了一个Binder子类,然后复写了其onTransact方法
<service android:name="com.realize.lizijun.noaidl_binder_server.CalcService" > <intent-filter> <action android:name="com.realize.noaidl.calc" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </service>
创建工程,其主activity内容如下所示:
package com.realize.lizijun.noaidl_binder_client; import android.app.Activity; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.Bundle; import android.os.IBinder; import android.os.RemoteException; import android.util.Log; import android.view.View; import android.widget.Toast; public class MainActivity extends Activity { private static final String TAG = "client"; private IBinder mPlusBinder = null; private ServiceConnection mServiceConnPlus = new ServiceConnection() { // 只有当servie异常退出时,系统才会调用onServiceDisconnected() @Override public void onServiceDisconnected(ComponentName name) { Log.e(TAG, "mServiceConnPlus onServiceDisconnected"); mPlusBinder = null; } @Override public void onServiceConnected(ComponentName name, IBinder service) { Log.e(TAG, " mServiceConnPlus onServiceConnected"); mPlusBinder = service; } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void bindService(View view) { Intent intentPlus = new Intent(); intentPlus.setAction("com.realize.noaidl.calc"); boolean result = bindService(intentPlus, mServiceConnPlus, Context.BIND_AUTO_CREATE); Log.e(TAG, result + ""); } public void unbindService(View view) { if (mServiceConnPlus != null) { Log.e(TAG, "unbindService"); unbindService(mServiceConnPlus); //mServiceConnPlus = null; } } public void mulInvoked(View view) { if (mPlusBinder == null) { Toast.makeText(this, "未连接服务端或服务端被异常杀死", Toast.LENGTH_SHORT).show(); } else { android.os.Parcel _data = android.os.Parcel.obtain(); android.os.Parcel _reply = android.os.Parcel.obtain(); int _result = 0; try { _data.writeInterfaceToken("CalcService"); _data.writeInt(50); _data.writeInt(12); mPlusBinder.transact(0x110, _data, _reply, 0); _reply.readException(); _result = _reply.readInt(); Toast.makeText(this, _result + "", Toast.LENGTH_SHORT).show(); } catch (RemoteException e) { e.printStackTrace(); } finally { _reply.recycle(); _data.recycle(); } } } public void divInvoked(View view) { if (mPlusBinder == null) { Toast.makeText(this, "未连接服务端或服务端被异常杀死", Toast.LENGTH_SHORT).show(); } else { android.os.Parcel _data = android.os.Parcel.obtain(); android.os.Parcel _reply = android.os.Parcel.obtain(); int _result = 0; try { _data.writeInterfaceToken("CalcService"); _data.writeInt(36); _data.writeInt(12); mPlusBinder.transact(0x111, _data, _reply, 0); _reply.readException(); _result = _reply.readInt(); Toast.makeText(this, _result + "", Toast.LENGTH_SHORT).show(); } catch (RemoteException e) { e.printStackTrace(); } finally { _reply.recycle(); _data.recycle(); } } } }
aidl示例代码