1,创建aidl的文件
package com.sdingba.aidl
interface ISDingBaAidl{
//计算2个数的和
int add(int num1,int num2);
}
2,创建类继承service
IRemoteService extends Service{
@Override
public IBinder onBind(Intent intent) {
return new MiddlePerson();
}
private IBinder iBinder = new ISDingBaAidl.Stub{
@Override
public int add(int num1,int num2){
return num1+num2;
}
}
}
3,声明:
<service android:name="com.sdingba.aidl.IRemoteService">
创建客户端app
1,在客服端差创建文件夹,吧服务端的aidl文件拷贝到文件夹中。
2,远程绑定的代码:
Intent inten = new Intent();
//新版本必须 显示Intent显示绑定服务
intent.setComponent(new CompeoentName("com.sdingba.aidl","com.sdingba.aidl.IRemoteService"));
bindService(intent,conn,Context.BIND_AUTO_CREATE);
3,创建conn
private ServiceConnection conn = new ServiceConnection(){
@Override
public void onServiceConnected(){
//拿到了远程的服务
ISDingBaAidl issdingba = ISDingBaAidl.Stub.adInterface(service);
}
@Override
public void onServiceDisconnected(){
issdingba = null;//回收资源
}
}
3.调用函数
issdingba.add(num1,num2);
4,解除绑定
onDestroy(){
unbindService(conn);
}
88888888888888888888888888888888888888888888888888888888888888
×××××××××××××××××××××××××××××××××××××××××××××××××××××××××
服务app:
1:IMiddlePerson.aidl
package com.sdingba.remoteservice;
interface IMiddlePerson {
/**
* 调用服务里面的方法
*/
void callMethodInService();
}
2:RemoteService。java
public class RemoteService extends Service {
@Override
public void onCreate() {
System.out.println("远程服务被创建了。。。");
super.onCreate();
}
@Override
public void onDestroy() {
System.out.println("远程服务被销毁了。");
super.onDestroy();
}
@Override
public IBinder onBind(Intent intent) {
return new MiddlePerson();
}
private void methodInService(){
System.out.println("我是远程服务的方法,我被调用了。。。。");
}
//1.创建一个中间人 远程服务继承的是ipc的一个实现类
private class MiddlePerson extends IMiddlePerson.Stub{
@Override
public void callMethodInService() {
methodInService();
}
}
}
3,:XML文件;配置
<service android:name="com.sdingba.remoteservice.RemoteService">
<intent-filter >
<!-- 为了方便别的程序用,所以定义一个action来使得别的程序可以调用 -->
<action android:name="com.sdingba.remoteservice"/>
</intent-filter>
</service>
绑定服务app
**1,拷贝aidl文件到目录
2:MainActivity。java**
public class MainActivity extends Activity {
private MyConn conn;
private IMiddlePerson iMp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* 绑定远程服务
* @param view
*/
public void bind(View view){
Intent intent = new Intent();
intent.setAction("com.sdingba.remoteservice");
conn = new MyConn();
bindService(intent, conn, BIND_AUTO_CREATE);
}
private class MyConn implements ServiceConnection{
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
//得到的方式和刚才不同,这个为远程绑定、、得到服务
iMp = IMiddlePerson.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
}
public void call(View view){
try {
//调用到远程服务的方法。。在远程服务中写的方法
iMp.callMethodInService();
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
protected void onDestroy() {
unbindService(conn);
super.onDestroy();
}
}