1,简单了解
AIDL是进程间通信的接口描述语言,它的使用场景用官网的话来说:“ 只有当你允许来自不同的客户端访问你的服务并且需要处理多线程问题时你才必须使用AIDL”,所以AIDL可以用来处理多线程、多客户端并发访问的。
需要注意一点就是在创建完AIDL文件之后需要build->Make Project一下,要不会找不到IMyAidlInterface接口
onServiceConnected()方法里面和绑定服务获取Service有些不同,它是直接调用IMyAidlInterface.Stub.asInterface(service)获取接口,然后调用我们写入接口中的方法。
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();
}
}
}