在我们平时的工作中,进程间通信是很常见的。假如你做过音频类应用,在控制台会发现,除了应用默认的进程外,还有一个类似“xxx.player”的进程,专门用来处理播放相关内容的进程;同样的,推送可能也需要增加一个单独的推送进程去完成对应的工作。有时候,我们也需要自己去实现这些功能。
官方提供了非常便捷的实现方法,定义AIDL接口,我们只需要在该aidl文件内部定义一些抽象方法,通过构建项目就会自动生成对应的java文件:
(1)对AIDL的操作进行简单介绍(也是我们平时使用的方式):
1.创建aidl文件MakeProject(C/S两端一样)
interface IMathAidl {
double add(double a,double b);//加法
double sub(double a,double b);//减法
void play(String path);//路径信息
}
2.服务端创建Service,不要忘记注册
public class MyService extends Service {
private IMathAidl.Stub mStub = new IMathAidl.Stub() {
@Override
public double add(double a, double b) throws RemoteException {
return a + b;
}
@Override
public double sub(double a, double b) throws RemoteException {
return a - b;
}
@Override
public void play(String path) throws RemoteException {
Message message = Message.obtain();
message.obj = path;
mHandler.sendMessage(message);
}
};
@Override
public IBinder onBind(Intent intent) {
return mStub;
}
}
<service android:name=".MyService">
<intent-filter>
<action android:name="com.sinitek.aidl.service"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</service>
3.Client端进行关联调用Server端
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent("com.sinitek.aidl.service");
intent.setPackage("com.sinitek.transactionserver");//minSdkVersion 21
bindService(intent, mConn, Context.BIND_AUTO_CREATE);
}
private ServiceConnection mConn = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
IMathAidl mathAidl = IMathAidl.Stub.asInterface(service);
try {
double result = mathAidl.add(1, 1);
Toast.makeText(MainActivity.this, "计算结果为:" + result, Toast.LENGTH_SHORT).show();
mathAidl.play("请观看服务端播放:'西京一村夫'SINITEK冲刺之路.mp4");
} catch (RemoteException e) {
e.printStackTrace();
}
}
};
}
涉及的相关代码放在:https://github.com/BuilderPattern/AidlTransactionServer.git
https://github.com/BuilderPattern/AidlTransactionClient.git
(2)如果你是一个比较有求知欲的键盘侠,你通过一顿操作在IMathAidl.java中就会发现,该AIDL会生成java类,实际的交互都这里完成。问题来了,是不是可以不用定义aidl接口,直接采用IMathAidl.java类中的方式去实现呢?答案是:肯定可以!因为,本质上就是aidl通过构建生成对应的IMathAidl.java文件来实现具体操作,如下:
1.Server端创建一个Service并注册:
public class NoAidlService extends Service {
public IBinder onBind(Intent t) {
return mBinder;
}
private NormalBinder mBinder = new NormalBinder();
private class NormalBinder extends Binder {
@Override
protected boolean onTransact(int code, Parcel data, Parcel reply, int flags) throws RemoteException {//0加,1乘
switch (code) {
case 0: {
data.enforceInterface("NoAidlService");//检测标识
int _arg0 = data.readInt();
int _arg1 = data.readInt();
int _result = _arg0 + _arg1;
reply.writeNoException();
reply.writeInt(_result);
return true;
}
case 1: {
data.enforceInterface("NoAidlService");
int _arg0 = data.readInt();
int _arg1 = data.readInt();
int _result = _arg0 * _arg1;
reply.writeNoException();
reply.writeInt(_result);
return true;
}
}
return super.onTransact(code, data, reply, flags);
}
}
}
<service android:name=".NoAidlService">
<intent-filter>
<action android:name="com.sinitek.noaidl.myservice" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</service>
2.Client端连接Server端,发送收据/等待回复:
public class MainActivity extends AppCompatActivity {
IBinder mBinder;
ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mBinder = service;
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
TextView mTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = findViewById(R.id.activity_main_operate_tv);
Intent intent = new Intent("com.sinitek.noaidl.myservice");
intent.setPackage("com.sinitek.transactionservernoaidl");
bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);
initEvent();
}
private void initEvent() {
mTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mRcvSnd();
}
});
}
public void mRcvSnd() {
if (mBinder == null) {
return;
}
Parcel _data = Parcel.obtain();
Parcel _reply = Parcel.obtain();
int _code = (int) (Math.random() * 6) % 2;
int _result;
try {
_data.writeInterfaceToken("NoAidlService");//客户端标识
_data.writeInt(6);
_data.writeInt(6);
mBinder.transact(_code, _data, _reply, 0);
_reply.readException();
_result = _reply.readInt();
Toast.makeText(MainActivity.this, "收到回复:" + _result, Toast.LENGTH_SHORT).show();
} catch (RemoteException e) {
e.printStackTrace();
} finally {
_reply.recycle();
_data.recycle();
}
}
}
有关该交互过程比较容易理解,依次:创建Server》Client连接Service》Client向Service发送数据》Service收到Client数据进行一系列分析操作,做出回应》Client拿到Service的响应结果,过程中也会涉及到校验和数据回收等,如下图所示:
涉及到的代码放在:https://github.com/BuilderPattern/NoAidlTransactionServer.git
https://github.com/BuilderPattern/NoAidlTransactionClient.git
以上内容就是有关AIDL的操作实现,及内部工作原理。使用过程需要注意:aidl要求文件所在包名(不是应用包名,具体去看demo)一致。如果想学习进一步深入学习Binder通信原理,请查看:https://mp.weixin.qq.com/s/bjaC0tSB0R87kFZSe_G8sA