一、AIDL基本概念
AIDL定义
:个人理解就是Android开发中提供的一种快速实现binder的工具,而binder就是一种跨进程通信,也可以不用AIDL,自己实现binder来达到同样的效果AIDL支持的基本类型
: String,int,long,boolean,float,double,ArrayList,HashMap,Parcelable
二、使用
1、先定义一个IPersion.aidl接口(IPerson.aidl)
interface IPerson {
void setName(String s);
String getName();
}
2、编译服务端AIDLService,实例化binder对象并返回给客户端(AIDLService.java)
package com.returntolife.jjcode.mydemolist.demo.function.aidl;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.Nullable;
import com.returntolife.jjcode.mydemolist.IPerson;
import com.tools.jj.tools.utils.LogUtil;
/*
* Create by JiaJun He on 2019/5/31$
* Email:1021661582@qq.com
* des:
* version:1.0.0
*/
public class AIDLService extends Service {
private static final String TAG = "AIDLService";
private String name;
private Binder binder = new IPerson.Stub() {
@Override
public void setName(String s) throws RemoteException {
name = s;
}
@Override
public String getName() throws RemoteException {
return name;
}
};
@Override
public void onCreate() {
super.onCreate();
LogUtil.d("onCreate");
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
LogUtil.d("onBind");
return binder;
}
@Override
public boolean onUnbind(Intent intent) {
LogUtil.d("onUnbind");
return super.onUnbind(intent);
}
@Override
public void onDestroy() {
super.onDestroy();
LogUtil.d("onDestroy");
}
}
3、客户端通过binderService获取binder后,就可以提供调用相应的方法
public class AIDLClientAcitvity extends Activity {
@BindView(R.id.btn_bindservice)
Button btnBindservice;
@BindView(R.id.btn_setname)
Button btnSetname;
@BindView(R.id.btn_getName)
Button btnGetName;
@BindView(R.id.et_name)
EditText etName;
private IPerson iPerson;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_aidl_client);
ButterKnife.bind(this);
}
@OnClick({R.id.btn_bindservice, R.id.btn_setname, R.id.btn_getName})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.btn_bindservice:
bindServiceByAidl();
break;
case R.id.btn_setname:
setName();
break;
case R.id.btn_getName:
getName();
break;
}
}
private void bindServiceByAidl() {
Intent intent = new Intent(this, AIDLService.class);
bindService(intent, new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
LogUtil.d("onServiceConnected");
iPerson = IPerson.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
LogUtil.d("onServiceDisconnected");
}
}, BIND_AUTO_CREATE);
}
private void setName() {
if (iPerson != null) {
try {
iPerson.setName(etName.getText().toString().trim());
} catch (RemoteException e) {
e.printStackTrace();
Toast.makeText(this, "setName error="+e, Toast.LENGTH_SHORT).show();
}
}
}
private void getName(){
if(iPerson!=null){
try {
Toast.makeText(this, iPerson.getName(), Toast.LENGTH_SHORT).show();iPerson.getName();
} catch (RemoteException e) {
e.printStackTrace();
Toast.makeText(this, "getName error="+e, Toast.LENGTH_SHORT).show();
}
}
}
}