AIDL是進程間通信的接口描述語言,它的使用場景用官網的話來說:“只有當你允許來自不同的客戶端訪問你的服務並且需要處理多線程問題時你才必須使用AIDL”,所以AIDL可以用來處理多線程、多客戶端併發訪問的。
2,使用
服務端:
(1)在main目錄右鍵選擇創建aidl,如圖所示。
會生成aidl接口文件 IMyAidlInterface.aidl。
(2)添加要調用的方法 getData()
interface IMyAidlInterface {
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
double aDouble, String aString);
String getData(String str);
}
(3) 編寫MyService需要注意一點就是在創建完AIDL文件之後需要build->Make Project一下,要不會找不到IMyAidlInterface接口
public class MyService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
return iBinder;
}
private IBinder iBinder = new IMyAidlInterface.Stub(){
@Override
public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {
}
@Override
public String getData(String str) throws RemoteException {
return "獲取到的數據++"+str;
}
};
在getData()方法中就可以實現數據處理,返回想要調用的數據。
(4) 注意在配置文件中配置service並且使屬性exported爲true,這個作用是否可以外部調用服務
<service android:name=".service.MyService"
android:exported="true"></service>
客戶端:
之前都是服務端的編寫,可以另外創建個Moudle來編寫客戶端。
(1)將之前創建的aidl整個文件複製粘貼到客戶端的main包下,注意包名還是服務端的包名。
(2) 在客戶端MainActivity中調用服務:
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.laojiang.serviceaidl","com.laojiang.serviceaidl.service.MyService"));
bindService(intent,connection,BIND_AUTO_CREATE);
ComponentName() 參數第一個是服務端包名,第二個參數是Service類名,connection是ServiceConnection對象的引用 private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
iMyAidlInterface = IMyAidlInterface.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
iMyAidlInterface = null;
}
};
onServiceConnected()方法裏面和綁定服務獲取Service有些不同,它是直接調用IMyAidlInterface.Stub.asInterface(service)獲取接口,然後調用我們寫入接口中的方法。
運行一下:
可以看到跨進程通信已經完成。
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btStart;
private TextView tvShow;
private IMyAidlInterface iMyAidlInterface;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btStart = (Button) findViewById(R.id.bt_start);
tvShow = (TextView) findViewById(R.id.tv_show);
btStart.setOnClickListener(this);
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.laojiang.serviceaidl","com.laojiang.serviceaidl.service.MyService"));
bindService(intent,connection,BIND_AUTO_CREATE);
}
private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
iMyAidlInterface = IMyAidlInterface.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
iMyAidlInterface = null;
}
};
@Override
public void onClick(View v) {
try {
String data = iMyAidlInterface.getData("我是客戶端數據");
tvShow.setText(data);
} catch (RemoteException e) {
e.printStackTrace();
}
}
}