AIDL:Android interface definition language 接口定义语言
不同APP进程之间数据通信的接口
目录
(一)AIDL组件的创建
- 创建AIDL组件
- 在生成的AIDL接口内部 申明 需要自定义实现的方法
(二)服务端
在Service的 Binder() 对象的实例中,返回AIDL的接口,并且实现自定义
(三)本地客户端
实现
ServiceConnection
接口,在其中拿到 AIDL 类IMyAidlInterface aidl= IMyAidlInterface.Stub.asInterface(iBinder) //传入一个iBinder对象,得到一个IMyAidlInterface返回对象
- 通过返回的IMyAidlInterface对象,拿到自定义的方法
(四)远程客户端
保持和本地客户端的AIDL完全一致(直接将本地客户端main文件夹下的aidl文件夹拷贝到远程客户端main文件夹下)Make project ,生成对应的 Binder 的 Java 文件
在远程客户端实现service绑定操作,实现
ServiceConnection
接口,在其中拿到 AIDL 类 (同本地客户端一样操作)IMyAidlInterface aidl= IMyAidlInterface.Stub.asInterface(iBinder) //传入一个iBinder对象,得到一个IMyAidlInterface返回对象
通过返回的IMyAidlInterface对象,拿到自定义的方法,拿到不同进程需要传递的数据
(一)AIDL组件的创建
- 创建AIDL组件
- 在生成的AIDL接口内部 申明 需要自定义实现的方法
IMyAidlInterface.aidl
// IMyAidlInterface.aidl
package com.moxuan.test;
// Declare any non-default types here with import statements
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);//系统默认生成的方法
void getProgess();//自定义方法
}
- Make project ,生成 Binder 的 Java 文件,可以在app→build→generated→source→aidl文件夹下找到对应的AIDL java文件
(二)服务端
- 在Service的 Binder() 对象的实例中,返回AIDL的接口,并且实现自定义
(三)本地客户端
- 在MainActivity中实现
ServiceConnection
接口,在其中拿到 AIDL 类ServiceConnection用于绑定客户端和服务 执行服务的绑定操作时
ServiceConnection()会被调用
执行服务的绑定操作,当客户端正常连接服务时,onServiceConnected()会被调用,进而AIDL接口会被调用
-
IMyAidlInterface aidl= IMyAidlInterface.Stub.asInterface(iBinder) //传入一个iBinder对象,得到一个IMyAidlInterface返回对象
public class MainActivity extends AppCompatActivity {
private TextView abd;
private TextView abd1;
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
private ServiceConnection serviceConnection= new ServiceConnection() {//用于绑定客户端和服务
@Override
public void onServiceConnected(ComponentName name, IBinder iBinder) {//当客户端正常连接服务时,执行服务的绑定操作会被调用
IMyAidlInterface aidl= IMyAidlInterface.Stub.asInterface(iBinder);
try {
aidl.getProgess();
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void onServiceDisconnected(ComponentName name) {//当客户端正常连接丢失时调用
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
abd = (TextView) findViewById(R.id.abd);
abd1 = (TextView) findViewById(R.id.abd1);
/*
服务启动
*/
Intent intent=new Intent(MainActivity.this,MyService.class);
startService(intent);
// bindService(intent,serviceConnection ,BIND_AUTO_CREATE);
//绑定
abd1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,MyService.class);
bindService(intent,serviceConnection ,BIND_AUTO_CREATE);
}
});
abd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//解绑
unbindService(serviceConnection);
// IBinder:用于远程操作的接口
// ServiceConnection;
}
});
}
}
- 通过返回的IMyAidlInterface对象,拿到自定义的方法
(四)远程客户端
- 保持和本地客户端的AIDL完全一致(直接将本地客户端main文件夹下的aidl文件夹拷贝到远程客户端main文件夹下)Make project ,生成对应的 Binder 的 Java 文件
- 在远程客户端实现service绑定操作,在activity中实现
ServiceConnection
接口,在其中拿到 AIDL 类 (同本地客户端一样操作)(参考三、本地客户端)
ServiceConnection用于绑定客户端和服务 执行服务的绑定操作时
ServiceConnection()会被调用
执行服务的绑定操作,当客户端正常连接服务时,onServiceConnected()会被调用,进而AIDL接口会被调用
-
IMyAidlInterface aidl= IMyAidlInterface.Stub.asInterface(iBinder) //传入一个iBinder对象,得到一个IMyAidlInterface返回对象
- 通过返回的IMyAidlInterface对象,拿到自定义的方法,拿到不同进程需要传递的数据