AIDL是跨app之间进行通信的语言
----------------------------------------------app1------------------------------------------------
定义AIDL
IMyAidlInterface.aidl 提供接口函数
IMyAidlInterface2.aidl 提供接口函数
MainActivity 启动service
MyService 实现接口
AndroidManifest.xml 定义服务设置services中action
----两套接口----
IMyAidlInterface.aidl
package com.example.service;
interface IMyAidlInterface {
String pay1();
int pay2();
int pay3(int a,int b);
}
IMyAidlInterface2.aidl
package com.example.service;
interface IMyAidlInterface {
String pay1();
int pay2();
int pay3(int a,int b);
}
MyService
//绑定后自动返回接口
public IBinder onBind(Intent intent) {
if(intent.getAction().equals("service_1")){
return subx;
}else if(intent.getAction().equals("service_2")){
return subx2;
}
return null;
}
//实现AIDL中的两套接口
public final Subx subx=new Subx();
public class Subx extends IMyAidlInterface.Stub{
public String pay1(){
String str="我是服务";
return str;
}
public int pay2(){
return 123456;
}
public int pay3(int a,int b){
return a-b;
}
}
public final Subx2 subx2=new Subx2();
public class Subx2 extends IMyAidlInterface2.Stub{
public String fun1(){
String str="我是服务2";
return str;
}
}
AndroidManifest.xml
设置service中action
<service
android:name=".MyService"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="service_1"/>
<action android:name="service_2"/>
</intent-filter>>
</service>
MainActivity
Intent intent=new Intent(MainActivity.this,MyService.class);
startService(intent);
-----------------------------------------------------------------app2---------------------------------------------
将app1中生成好的AIDl文件拷贝到自己的文件夹中,记得拷贝的路径要相同,拷贝过来后在编译一遍
MainActivity
//定义两套接口
IMyAidlInterface iMyAidlInterface=null;
IMyAidlInterface2 iMyAidlInterface2=null;
//设置连接1
ServiceConnection conn=new ServiceConnection() {
//当绑定成服务功后会自动调用此函数
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
iMyAidlInterface=IMyAidlInterface.Stub.asInterface(iBinder);
System.out.println("---- 链接成功1 ----");
}
public void onServiceDisconnected(ComponentName componentName) {
}
};
//设置连接2
ServiceConnection conn2=new ServiceConnection() {
//当绑定服务成功后会自动调用此函数
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
iMyAidlInterface2=IMyAidlInterface2.Stub.asInterface(iBinder);
System.out.println("---- 链接成功2 ----");
}
public void onServiceDisconnected(ComponentName componentName) {
}
};
--------------------------------------------------------------------------------------------------
//绑定service_1 intent设定被绑定者的包名和action
Intent intent=new Intent();
intent.setAction("service_1");
intent.setPackage("com.example.service");
bindService(intent,conn, Context.BIND_AUTO_CREATE);
//绑定service_2 intent设定被绑定者的包名和action
Intent intent=new Intent();
intent.setPackage("com.example.service");
intent.setAction("service_2");
bindService(intent,conn2,Context.BIND_AUTO_CREATE);
------------------------------------------------------------------------------------------------------
//获取接口1
try {
String str = iMyAidlInterface.pay1();
System.out.println("收到: "+str);
}catch (RemoteException e){
e.printStackTrace();
}
try {
int a=iMyAidlInterface.pay2();
System.out.println("收到: "+a);
}catch (RemoteException e1){
e1.printStackTrace();
}
try{
int c=iMyAidlInterface.pay3(12,2);
System.out.println("收到: "+c);
}catch (RemoteException e2){
e2.printStackTrace();
}
}
//获取接口2
try{
String str=iMyAidlInterface2.fun1();
System.out.println("接受到服务2消息:"+str);
}catch (RemoteException e){
e.printStackTrace();
}