您的位置:首页 > 移动开发 > Android开发

android绑定远程服务以及android接口定义语言(aidl)

2015-12-10 14:40 603 查看
在Andorid平台中,各个组件运行在自己的进程中,他们之间是不能相互访问的,但是在程序之间是不可避免的要传递一些对象,在进程之间相互通信。为了实现进程之间的相互通信,Andorid采用了一种轻量级的实现方式RPC(Remote
Procedure Call 远程进程调用)来完成进程之间的通信,并且Android通过接口定义语言(Andorid
Interface Definition Language ,AIDL)来生成两个进程之间相互访问的代码,例如,你在Activity里的代码需要访问Service中的一个方法,那么就可以通过这种方式来实现了。

   AIDL是Android的一种接口描述语言; 编译器可以通过aidl文件生成一段代码,通过预先定义的接口达到两个进程内部通信进程的目的. 如果需要在一个Activity中, 访问另一个Service中的某个对象, 需要先将对象转化成 AIDL可识别的参数(可能是多个参数), 然后使用AIDL来传递这些参数, 在消息的接收端, 使用这些参数组装成自己需要的对象。

   AIDL RPC机制是通过接口来实现的,类似Windows中的COM或者Corba,但他是轻量级的,客户端和被调用实现之间是通过代理模式实现的,代理类和被代理类实现同一个接口Ibinder接口。

下面是实现Activity访问Service例子的步骤:

远程服务段端:

com.example.remote.Myservice

package com.example.remote;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.RemoteException;
import android.widget.Toast;

public class Myservice extends Service {

@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
System.out.println("服务绑定");
return new MiddlePerson();
}

@Override
public void onCreate() {
// TODO Auto-generated method stub
System.out.println("服务创建");
super.onCreate();
}

@Override
public void onDestroy() {
// TODO Auto-generated method stub
System.out.println("服务结束");
super.onDestroy();
}
public void methodInservice(String arr){

System.out.println("我是远程服务方法,我被调用了");
}
private class MiddlePerson extends IMiddlePerson.Stub{

@Override
public void call(String string) throws RemoteException {
// TODO Auto-generated method stub
methodInservice(string);
}

}

}
编写aidl接口

  AIDL使用简单的语法来声明接口,描述其方法以及方法的参数和返回值。这些参数和返回值可以是任何类型,甚至是其他AIDL生成的接口。重要的是必须导入导入除了内建类型(例如:int,boolean等)外的任何其他类型,哪怕是这些类型是在与接口相同的包中。具体的要求如下:
JAVA基本数据类型不需要导入
String,List,Map和CharSequence不需要导入
package com.example.remote;

interface IMiddlePerson {
void call(String string);
}


其次在清单文件中声明服务:

 
<service  android:name="com.example.remote.Myservice">
<intent-filter ><action android:name="com.example.remote.Myservice"/></intent-filter>
</service>

二:编写调用远程服务的程序:

1:将被调用服务的aidl文件copy过来,注意,报名必须一致





调用端布局文件

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="bind"
android:text="绑定远程" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="mystop"
android:text="解除绑定" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="call"
android:text="调用方法" />
</LinearLayout>


调用实现:

package com.example.bindremote;

import com.example.remote.IMiddlePerson;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

public class MainActivity extends Activity {
private IMiddlePerson im;
private Myconn conn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

public void mystop(View vew){
unbindService(conn);
}
public void bind(View vew){
Intent service=new Intent();
service.setAction("com.example.remote.Myservice");
conn=new  Myconn();
bindService(service, conn, BIND_AUTO_CREATE);

}
public void call(View vew){
try {
im.call("leigewudi");
System.out.println(im);
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public class Myconn implements ServiceConnection {

@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
im=IMiddlePerson.Stub.asInterface(service);

System.out.println("服务被调用了"+im);
}

@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
unbindService(conn);
System.out.println("服务结束");
}

}
}


实验结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: