Android 中 Service AIDL使用

本文详细介绍如何利用Android接口定义语言(AIDL)实现跨应用间的Service绑定及数据交互过程,包括创建.AIDL文件、实现Service的onBind方法、客户端与服务端的连接与数据传递等关键步骤。

 

 
 
 1. 创建两个项目创建两个.aidl文件
2.在传递值的类里面创建Service并且返回接口:
服务返回值onBind
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
return new IMyAidlInterface.Stub() {
@Override
public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {

}
};
}
 
 
 
3.启动项目一service1将.aidl文件配置到项目中;
 
 
4.启动项目二service2:启动服务bindService
 
intentt = new Intent();
intentt.setComponent(new ComponentName("com.example.com.myapplication", "com.example.com.myapplication.MyService"));
启动服务
bindService(intentt, this, Context.BIND_AUTO_CREATE);
销毁服务
unbindService(this);
5.返回一个iMyAidlInterface接口
/**
*
* 连接
* @param name
* @param service
*/
@Override
public void onServiceConnected(ComponentName name, IBinder service) {

iMyAidlInterface=IMyAidlInterface.Stub.asInterface(service);


}
6.传递数据
if(iMyAidlInterface!=null) {
try {
iMyAidlInterface.setDate(et.getText().toString());
} catch (RemoteException e) {
e.printStackTrace();
}
}
7.接受数据并且应用
return new IMyAidlInterface.Stub() {
    @Override
public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {

}

@Override
public void setDate(String data) throws RemoteException {

MyService.this.data=data;
}
};
 
  
 
 
 
 
 
 
一,简单的启动
 
1.创建一个项目,创建一个类继承Service
重写方法
 
 <service android:name="com.example.android_aidl.MyServer"
            android:process=":remote"
           android:exported="true"
            ></service>
 
2.创建一个项目,启动第一个项目的服务Service
 intentt = new Intent();
 intentt.setComponent(new ComponentName("com.example.android_aidl", "com.example.android_aidl.MyServer"));
参数为:启动项目的包名,启动项目包名的服务Service
startService(intentt);
stopService(intentt);
 
 
 
二,跨应用绑定Service:AIDL:android接口定义语言
 
创建.AIDL文件
 
服务返回值onBind
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
return new IMyAidlInterface.Stub() {
@Override
public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {

}
};
}
启动服务
bindService(intentt, this, Context.BIND_AUTO_CREATE);
销毁服务
unbindService(this);
/**
* 连接成功
* @param name
* @param service
*/
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// 打印输出
Log.e("App2 BInd service", "StartService");

}

/**
* 连接失败
* @param name
* @param
*/
@Override
public void onServiceDisconnected(ComponentName name) {

}
 
二,通过:AIDL:传值:
 
创建两个项目中包名,类名一样的.aidl文件用来交互并且内容也一样
 
在p2里面向p里面传值:
 
按钮监听
if(iMyAidlInterface!=null) {
try {
iMyAidlInterface.setDate(et.getText().toString());
} catch (RemoteException e) {
e.printStackTrace();
}
}
返回时接受的接口
@Override
public void onServiceConnected(ComponentName name, IBinder service) {

iMyAidlInterface=IMyAidlInterface.Stub.asInterface(service);


}
在p里面的Service中实现接口并且传值
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
return new IMyAidlInterface.Stub() {
@Override
public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {

}

@Override
public void setDate(String data) throws RemoteException {

MyService.this.data=data;
}
};
}
在Myservic中使用:
 
@Override
public void onCreate() {
super.onCreate();

new Thread(new Runnable() {
@Override
public void run() {

run=true;
while (run){

try {
Thread.sleep(1000);
Log.e("service", data);
} catch (InterruptedException e) {
e.printStackTrace();
}


}

}
}).start();



Log.e("service", "startServier");
}
 
 

转载于:https://www.cnblogs.com/huihuizhang/p/5222431.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值