在我們平時的作業中,行程間通信是很常見的,假如你做過音頻類應用,在控制臺會發現,除了應用默認的行程外,還有一個類似“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
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/68875.html
標籤:其他
