Android笔记三十.Service入门(四).跨进程调用Service(AIDL Service)

来源:互联网 发布:神创天下女神升阶数据 编辑:程序博客网 时间:2024/06/05 08:31
跨进程调用Service(AIDL Service)
  转载请表明出处:http://blog.csdn.net/u012637501(嵌入式_小J的天空)
一、AIDL Service

1.什么是AIDL Service?
    AIDL,即Android Interface Definition Language.是Android用于定义远程接口,AIDL接口定义语言的语法比较简单,这种接口定义语言并不是真正的编程语言,它只是定义两个进程之间的通信接口。AIDL的语法与Java接口很相似,但存在如下几点差异:
(1)AIDL定义接口的源代码必须以.aidl结尾;
(2)AIDL接口中用到数据类型,除了基本类型、String、List、Map、CharSequence之外,其他类型全部都需要导包,即使他们在同一个包中也需要导包。
(3)定义好AIDL接口之后(如Song.aidl),ADT工具会自动在gen目录下生成相应的包,并生成一个Song.java接口,在该接口里包含一个Stub内部类该内部类实现了IBinder、Song两个接口,这个Stub类将会作为远程Service的回调类。它内部能实现一些功能。  
(4)开发客户端的第一步就是Service端的AIDL接口文件复制到客户端应用中,复制到客户端后ADT工具会为AIDL接口生成相应的Java接口类。
    在Android系统中,由于各个应用程序都是运行在自己的进程中(启动一个应用即启动一个进程),进程之间一般无法直接进行数据交换。所以,为了实现进程间的通信(interprocess communiocation,简称IPC),Android提供了AIDL Service。
2.远程Service
    与本地Service相对应的是远程Service,即一个应用程序(进程)可以访问另一个应用程序的Service组件前者我们称之为客户端、后者为服务端,而客户端访问服务端就是通过AIDL接口实现彼此间通信的。通过之前几篇文章我们知道,客户端访问Service时,Android并不是直接返回Service对象给客户端,而是将Service的代理对象(IBinder对象)通过Service的onBind()方法返回给客户端,因此,Android的AIDL远程接口的实现类就是那个IBinder实现类。当客户端获取了远程Service的IBinder对象的代理之后,客户端进程就可通过该IBinder对象去回调远程Service的属性或方法,从而实现进程间的通信。
3.Service返回的IBinder对象
    当访问者(客户端)调用bindService()方法来绑定与启动指定的Service,对于绑定本地Service还是绑定远程Service,Service返回的IBinder对象是有所区别的。
(1)绑定本地Service:本地Service的onBind()方法会直接把IBinder对象本身传给客户端的ServiceConnection对象的onServiceConnected方法的第二个参数;
(2)绑定远程Service:远程Service的onBind()方法只是将IBinder对象的代理传给客户端的ServiceConnection对象的onServiceConnected方法的第二个参数。
4.AIDL接口工作原理
    当在工程中实现了一个.aidl的AIDL接口源文件(如/src/com/android_aidlservice/ICat.aidl),我们可以通过两种方式生成需要的ICat.java接口文件:一是使用Android SDK安装目录下的platform-tools子录下的aidl.exe工具;二是ADT工具自动为该AIDL接口生成实现(路径:gen/com.example.android_aidlservice/ICat.java)。ICat.java接口包含一个Stub内部类,该内部类实现了IBinder、ICat两个接口,这个Stub类将会作为远程Service的回调类---它实现了IBinder接口,因此可作为Service的onBind()方法的返回值
    
二、进程间通信开发思路
1.服务端-远程Service开发
(1)定义一个AIDL接口,文件名为xxx.aidl,保存到源码所在的路径,/src/com/android_aidlservice/ICat.aidl;
package com.example.android_aidlservice;
interface ICat
{
     String getColor();
     double getWeight();
}
其中,com.example.android_aidlservice为其所在的包名。
注意:保存后,ADT工具会自动在gen/com.example.service目录下生成一个ICat.java接口
(2)定义一个Service实现类,并实现一个内q部类继承于Stub即实现了ICat接口,也就实现了IBinder接口。该Service的onBind()方法所返回的IBinder对象为ICat.Stub的子类的实例。
private CatBinder catBinder;
public class CatBinder extends Stub
{
    ......
}
@Override
public IBinder onBind(Intent arg0)
{
    return catBinder;
}
(3)在AndroidMainfest.xml工程文件中配置该Service
    <application .... > 
       <!--定义一个Service组件--> 
       <service android:name=".AidlService">
            <intent-filter>
                <action android:name="com.example.sercie.AIDL_SERVICE"></action>
            </intent-filter>
        </service>
  </application>
2.客户端开发思路
(1)定义一个AIDL接口,文件名为xxx.aidl,保存到源码所在的路径,如/src/com/android_aidlservice/ICat.aidl;
package com.example.android_aidlservice;
interface ICat
{
     String getColor();
     double getWeight();
}
其中,com.example.android_aidlservice为其所在的包名,该.aidl文件为从Service段的AIDL接口文件复制到客户端应用中。
(2)创建ServiceConnection对象,并在ServiceConnection对象的onServiceConnected方法中添加如下代码:
    catService = ICat.Stub.asInterface(service);
其中,catService为Service的onBind()方法返回IBinder对象的代理。
(3)以ServiceConnection对象作为参数,调用Context的bindService()方法绑定并启动远程Service即可。
(4)将返回的IBinder对象的代理类转换成IBinder对象,从而调用Service中的相应方法。
三、源码实现
1.服务端
(1)定义一个Service实现类-/src/com/example/android_service/AidlService.java
<span style="font-family:Times New Roman;font-size:18px;">package com.example.aidlservice;import java.util.Timer;import java.util.TimerTask;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.os.RemoteException;import com.example.aidlservice.ICat.Stub;public class AidlService extends Service{   private Timer timer=new Timer();;  //2.定义两个数组与两个私有变量  private String color;  private double weight;  String[] colors = new String[]{"黑色","黄色","红色"};  double[] weigths=new double[]{2.3, 3.1,1.58};  //1.声明一个IBinder对象、Timer对象  private CatBinder binder= new CatBinder();  //3.继承stub,也就实现了ICat接口,并实现了IBinder接口  public class CatBinder extends Stub  {   @Override   public String getColor() throws RemoteException {    return color;   }   @Override   public double getWeight() throws RemoteException {    return weight;   }   }  //4.当Service创建时调用该方法  @Override  public void onCreate() {   System.out.println("调用Service的onCreate方法");   super.onCreate();   //b.该service完成的任务:随机地改变Service组件内color、weight属性的值   timer.schedule(new TimerTask(){     @Override     public void run()     {       int rand=(int)(Math.random()*3);       color=colors[rand];       weight = weigths[rand];       System.out.println("-----------"+rand);     }  }, 0, 800);  }  /*5.访问者使用bindService()方法启动Service时,该方法用于返回catBinder对象   * (1)在绑定本地Service的情况下,该catBinder对象会直接传给客户端的ServiceConnection对象;   * (2)在绑定远程Service的情况下,只将catBinder对象的代理传递给客户端的ServiceConnection对象的   * onServiceConnection方法的第二个参数*/  @Override  public IBinder onBind(Intent intent) {   System.out.println("返回Service的IBinder对象");   return binder;  }  //6.回调该方法关闭Service  @Override  public void onDestroy() {   timer.cancel();  }}</span>
(2)在AndroidManifest文件中配置该Service
<span style="font-family:Times New Roman;font-size:18px;"><?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.aidlservice"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="14" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name=".ServiceTest"            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服务 -->       <service android:name=".AidlService">            <intent-filter>                    <action android:name="com.jiangdongguo.service"/>            </intent-filter>        </service>    </application></manifest></span>
2.客户端
(1)定义一个Activity实现类-/src/com/example/android_service/AidlClient.java
<span style="font-family:Times New Roman;font-size:18px;">package com.example.aidlclient;import android.app.Activity;import android.app.Service;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.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;import com.example.aidlservice.ICat;public class AidlClient extends Activity {  private ICat catService;//声明一个ICat对象  private Button get;  private TextView color,weight;   //1.创建一个ServiceConnection对象  private ServiceConnection conn=new ServiceConnection(){   @Override   public void onServiceConnected(ComponentName name, IBinder service) {    //获取远程Service的onBind方法的对象的代理    catService = ICat.Stub.asInterface(service);   }   @Override   public void onServiceDisconnected(ComponentName name) {   }  };   //2.绑定远程Service并获取其内容  @Override  protected void onCreate(Bundle savedInstanceState) {   super.onCreate(savedInstanceState);   //a.获取Activity主界面组件   setContentView(R.layout.main);   get=(Button)findViewById(R.id.get);   color=( TextView)findViewById(R.id.color);   weight=(TextView)findViewById(R.id.weight);    //b.创建所需绑定的Service的Intent并设置其Action属性(即指定启动哪个远程Service)   Intent intent=new Intent("com.jiangdongguo.service");   //c.绑定并启动远程Service   bindService(intent,conn,Service.BIND_AUTO_CREATE);   //d.通过IBinder对象的代理获取远程Service数据   get.setOnClickListener(new OnClickListener(){     @Override     public void onClick(View v)     {          try          {           color.setText("颜色:"+catService.getColor());           weight.setText("比重:"+catService.getWeight());          }          catch (RemoteException e) {           e.printStackTrace();          }          }    });  }  //3.退出Activity时调用该方法,解除远程Service的绑定  @Override  protected void onDestroy() {   super.onDestroy();   this.unbindService(conn);  }}</span>

效果演示:运行客户端程序,单击程序界面中"获取远程Service的数据"按钮,就会实现客户端读取远程Service功能。


0 0
原创粉丝点击