Android笔记 使用AIDL和远程服务实现进程通信

来源:互联网 发布:软件测试怎么学 编辑:程序博客网 时间:2024/05/19 19:30

简介

AIDL(Android Interface Definition Language:接口定义语言)

在Android中, 每个应用程序都有自己的进程,当需要在不同的进程之间传递对象时,该如何实现呢? 显然, Java中是不支持跨进程内存共享的。因此要传递对象, 需要把对象解析成操作系统能够理解的数据格式, 以达到跨界对象访问的目的。在JavaEE中,采用RMI通过序列化传递对象。在Android中, 则采用AIDL(Android Interface Definition Language:接口定义语言)方式实现。


AIDL是一种接口定义语言,用于约束两个进程间的通讯规则,供编译器生成代码,实现Android设备上的两个进程间通信(IPC)。AIDL的IPC机制和EJB所采用的CORBA很类似,进程之间的通信信息,首先会被转换成AIDL协议消息,然后发送给对方,对方收到AIDL协议消息后再转换成相应的对象。由于进程之间的通信信息需要双向转换,所以android采用代理类在背后实现了信息的双向转换,代理类由android编译器生成,对开发人员来说是透明的。




假设A应用需要与B应用进行通信,调用B应用中的download(String path)方法,B应用以Service方式向A应用提供服务。需要下面四个步骤: 


1> 在B应用中创建*.aidl文件,aidl文件的定义和接口的定义很相类,如:在com.gem.aidl包下创建IDownloadService.aidl文件,内容如下:
package com.gem.aidl;
interface IDownloadService {
void download(String path);
}
当完成aidl文件创建后,eclipse会自动在项目的gen目录中同步生成IDownloadService.java接口文件。接口文件中生成一个Stub的抽象类,里面包括aidl定义的方法,还包括一些其它辅助方法。值得关注的是asInterface(IBinder iBinder),它返回接口类型的实例,对于远程服务调用,远程服务返回给客户端的对象为代理对象,客户端在onServiceConnected(ComponentName name, IBinder service)方法引用该对象时不能直接强转成接口类型的实例,而应该使用asInterface(IBinder iBinder)进行类型转换。


编写Aidl文件时,需要注意下面几点: 
  1.接口名和aidl文件名相同。
  2.接口和方法前不用加访问权限修饰符public,private,protected等,也不能用final,static。
  3.Aidl默认支持的类型包话java基本类型(int、long、boolean等)和(String、List、Map、CharSequence),使用这些类型时不需要import声明。对于List和Map中的元素类型必须是Aidl支持的类型。如果使用自定义类型作为参数或返回值,自定义类型必须实现Parcelable接口。
  4.自定义类型和AIDL生成的其它接口类型在aidl描述文件中,应该显式import,即便在该类和定义的包在同一个包中。
  5.在aidl文件中所有非Java基本类型参数必须加上in、out、inout标记,以指明参数是输入参数、输出参数还是输入输出参数。
  6.Java原始类型默认的标记为in,不能为其它标记。


2> 在B应用中实现aidl文件生成的接口(本例是IDownloadService),但并非直接实现接口,而是通过继承接口的Stub来实现(Stub抽象类内部实现了aidl接口),并且实现接口方法的代码。内容如下:
public class ServiceBinder extends IDownloadService.Stub {
@Override
public void download(String path) throws RemoteException {
Log.i("DownloadService", path);
}
}


3> 在B应用中创建一个Service(服务),在服务的onBind(Intent intent)方法中返回实现了aidl接口的对象(本例是ServiceBinder)。内容如下:
public class DownloadService extends Service {
private ServiceBinder serviceBinder = new ServiceBinder();
@Override
public IBinder onBind(Intent intent) {
return serviceBinder;
}
public class ServiceBinder extends IDownloadService.Stub {
@Override
public void download(String path) throws RemoteException {
Log.i("DownloadService", path);
}
}
}
其他应用可以通过隐式意图访问服务,意图的动作可以自定义,AndroidManifest.xml配置代码如下:
<service android:name=".DownloadService" >
<intent-filter>
<action android:name="com.gem.process.aidl.DownloadService" />
</intent-filter>
</service>


4> 把B应用中aidl文件所在package连同aidl文件一起拷贝到客户端A应用,eclipse会自动在A应用的gen目录中为aidl文件同步生成IDownloadService.java接口文件,接下来就可以在A应用中实现与B应用通信,代码如下:
public class ClientActivity extends Activity {
private IDownloadService downloadService;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.bindService(new Intent("com.gem.process.aidl.DownloadService"), this.serviceConnection, BIND_AUTO_CREATE);//绑定到服务
}


@Override
protected void onDestroy() {
super.onDestroy();
this.unbindService(serviceConnection);//解除服务
}

private ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
downloadService = IDownloadService.Stub.asInterface(service);
try {
downloadService.download("http://www.gem.com");
} catch (RemoteException e) {
Log.e("ClientActivity", e.toString());
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
downloadService = null;
}
};
}

下面是具体实例

A.先编写远程服务端ServiceRemote工程

1.编写Iservice.aidl文件(本质是接口 新建一个Interface 继承 写出方法 然后在本地目录将Isevice.java改成Iservice.aidl 会发现gen目录下多了一些东西)

package com.example.serviceremote;interface IService {//AIDL Android Interface Definition Language 接口里的所有属性方法修饰符只能是默认的String getData();}


2.编写MyServie 继承 Service

package com.example.serviceremote;import com.example.serviceremote.IService.Stub;import android.app.Service;import android.content.Intent;import android.os.Binder;import android.os.IBinder;import android.os.RemoteException;public class MyServie extends Service {String data = "远程访问";@Overridepublic IBinder onBind(Intent intent) {// TODO Auto-generated method stubreturn new MyBinder();}//自定义IBinder子类,通过该类访问属性class MyBinder extends Stub{@Overridepublic String getData() throws RemoteException {// TODO Auto-generated method stubreturn data;}}}


3配置清单文件  主要是两点 1配置Service(四大组件均需要在清单文件配置application节点下配置否则出错)  2配置action属性(隐式意图用到)

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.serviceremote"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="18" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name="com.example.serviceremote.MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <service android:name=".MyServie">            <intent-filter>                <action android:name="com.myservice"></action>            </intent-filter>        </service>    </application></manifest>


这样远程服务端就完成了 下面编写客户端

B.ServiceClient工程编写

1.布局文件   三个按钮对应三个点击事件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >    <Button        android:id="@+id/button1"        android:onClick="bind"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_alignParentTop="true"        android:text="bind" />    <Button        android:id="@+id/button2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:onClick="unbind"        android:layout_alignLeft="@+id/button1"        android:layout_below="@+id/button1"        android:layout_marginTop="14dp"        android:text="unbind" />    <Button        android:id="@+id/button3"        android:layout_width="wrap_content"        android:onClick="get"        android:layout_height="wrap_content"        android:layout_alignRight="@+id/button2"        android:layout_below="@+id/button2"        android:text="get" /></RelativeLayout>

2复制远程服务端的包名及包下的IService.aidl文件 我的包名为com.example.serviceremote


3编写访问端的代码

package com.example.serviceclient;import com.example.serviceremote.IService;import android.os.Bundle;import android.os.IBinder;import android.os.RemoteException;import android.app.Activity;import android.content.ComponentName;import android.content.Intent;import android.content.ServiceConnection;import android.view.View;import android.widget.Toast;public class MainActivity extends Activity {Intent intent;IService iService;MyConnection myConnection = new MyConnection();boolean flag = false;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);intent =new Intent("com.myservice");}public void bind(View view) {bindService(intent, myConnection, BIND_AUTO_CREATE);}public void unbind(View view) {if (flag) {unbindService(myConnection);flag=false;}}public void get(View view) {try {String text = iService.getData();Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show();} catch (RemoteException e) {// TODO Auto-generated catch blocke.printStackTrace();}}@Overrideprotected void onDestroy() {// TODO Auto-generated method stubif (flag) {unbindService(myConnection);flag=false;}super.onDestroy();}class MyConnection implements ServiceConnection{@Overridepublic void onServiceConnected(ComponentName name, IBinder service) {// TODO Auto-generated method stubiService = IService.Stub.asInterface(service);flag = true;}@Overridepublic void onServiceDisconnected(ComponentName name) {// TODO Auto-generated method stub}}}

客户端编写完毕 下面说下运行顺序

先运行远程的服务 在运行client端



0 0