Android学习笔记----跨进程调用Service(AIDL)

来源:互联网 发布:iphone6s网络支持 编辑:程序博客网 时间:2024/05/16 01:51
/*********************************************************************************************************************/
跨进程的Service调用(AIDL)

/*********************************************************************************************************************/

AIDL Service (android interface definition language) service

作用跨进程通信,(ContentProvider的作用是跨进程数据共享)

参考java中的RMI(remote method invocation)远程方法调用

与RMI的区别

 Android并不是直接返回Service对象给客户端,而是将Service的代理对象通过onbind()方法返回给客户端。因此,Android AIDL远程接口的实现类就是那个IBinder实现类。

与绑定本地Service的区别

绑定本地Service的时候,本地Service的onBind()方法会直接把IBinder对象本身传递给ServiceConnection的onServiceConnected方法的第二个参数。
绑定远程Service的时候,远程Service的onBind()方法只是将Binder对象的代理传给客户端的ServiceConnection的onServiceConnected方法的第二个参数。

客户端获取了远程Service的IBinder对象的代理之后,就可以通过IBinder对象,去回调远程Service的方法跟属性了。


语言:AIDL语言,即android interface definition language

AIDL语言语法很简单,语法跟Java接口很相似,但存在如下差异:

AIDL定义接口的源代码,必须以.aidl结尾
AIDL接口中用到的数据类型,除了基本类型,String,List,Map,CharSequence之外,其他类型全部都需要导包,即使他们在同一个包中也需要导入包。


跨进程调用的Service的步骤 

创建AIDL文件

例,我们在应用中定义如下AIDL接口代码

AidlService\src\org\crazyit\service\ICat.aidl

package org.crazyit.service;

interface ICat
{
String getColor();
double getWeight();
}

注:将AIDL文件放在service包下即可。

定义好AIDL接口之后,ADT工具会自动在service目录下生成一个ICat.java接口,在该接口里面包含一个Stub内部类,该内部类实现了IBinder,ICat两个接口,这个Stub类将会作为远程Service的回调类,-----他实现了IBinder接口,因此他可以作为Service的onBind()方法的返回值。

将接口暴露给客户端

上一步定义好一个AIDL接口之后,接下来就可以定义一个Service实现类了,该Servive的onBind()方法所返回的IBinder对象应该是ADT生成的ICat.Stub的子类实例。至于其他部分,则与开发本地Service完全一样。

/** * */        package org.crazyit.service;        import java.util.Timer;        import java.util.TimerTask;        import org.crazyit.service.ICat.Stub;        import android.app.Service;        import android.content.Intent;        import android.os.IBinder;        import android.os.RemoteException;/** * Description: * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>* <br/>Copyright (C), 2001-2014, Yeeku.H.Lee * <br/>This program is protected by copyright laws. * <br/>Program Name: * <br/>Date: * @author  Yeeku.H.Lee kongyeeku@163.com * @version  1.0 */public class AidlService extends Service{        private CatBinder catBinder;        Timer timer = new Timer();        String[] colors = new String[]{            "红色",            "黄色",            "黑色"        };        double[] weights = new double[]{             2.3,             3.1,             1.58        };        private String color;        private double weight;        // 继承Stub,也就是实现额ICat接口,并实现了IBinder接口        public class CatBinder extends Stub        {            @Override            public String getColor() throws RemoteException            {                return color;            }            @Override            public double getWeight() throws RemoteException            {                return weight;            }        }        @Override        public void onCreate()        {            super.onCreate();            catBinder = new CatBinder();            timer.schedule(new TimerTask()            {                @Override                public void run()                {                    // 随机地改变Service组件内color、weight属性的值。                    int rand = (int)(Math.random() * 3);                    color = colors[rand];                    weight = weights[rand];                    System.out.println("--------" + rand);                 }                } , 0 , 800);        }        @Override        public IBinder onBind(Intent arg0)        {            /* 返回catBinder对象           * 在绑定本地Service的情况下,该catBinder对象会直接           * 传给客户端的ServiceConnection对象           * 的onServiceConnected方法的第二个参数;           * 在绑定远程Service的情况下,只将catBinder对象的代理           * 传给客户端的ServiceConnection对象           * 的onServiceConnected方法的第二个参数;           */            return catBinder; //①        }        @Override        public void onDestroy()        {            timer.cancel();        }}

通过以上介绍可以看出,开发AIDL远程Service其实也很简单,只是需要比开发本地Service多定义一个AIDL接口而已。

该Service类开发完成以后,需要在AndroidManifest.xml文件中,进行相关配置

<service android:name= ".AidlService">
    <intent-filter>
            <action android:name = "org.crazyit.aidl.action.AIDL_SERVICE"/>
    </intent-filter>
</service>

客户端访问AIDLService

AIDL定义了两个进程之间的通信接口,因此不仅服务器端需要AIDL接口,客户端同样需要前面定义的AIDL接口,因此开发客户端的第一步就是将Service端的AIDL接口文件复制到客户端应用中,复制到客户端后,ADT工具会自动为AIDL接口生成相应的实现。

客户端绑定远程Service的步骤:

创建ServiceConnection对象
以ServiceConnection对象作为参数,调用Context的bindService()方法绑定远程Service即可。

与绑定本地Service不同的是,绑定远程Service的ServiceConnection并不能直接获取Service的onBind()方法返回的对象,他只能获取到onBind()方法返回的对象的代理,因此在ServiceConnection的onServiceConnected方法中需要通过如下代码进行处理:

catService = ICat.Stub.asInterface(service);

        package org.crazyit.client;        import org.crazyit.service.ICat;        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.EditText;/** * Description: <br/>* site: <a href="http://www.crazyit.org">crazyit.org</a> <br/>* Copyright (C), 2001-2012, Yeeku.H.Lee <br/>* This program is protected by copyright laws. <br/>* Program Name: <br/>* Date: * * @author Yeeku.H.Lee kongyeeku@163.com * @version 1.0 */public class AidlClient extends Activity{    private ICat catService;    private Button get;    EditText color, weight;    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)        {            catService = null;        }    };        @Override        public void onCreate(Bundle savedInstanceState)        {            super.onCreate(savedInstanceState);            setContentView(R.layout.main);            get = (Button) findViewById(R.id.get);            color = (EditText) findViewById(R.id.color);            weight = (EditText) findViewById(R.id.weight);            // 创建所需绑定的Service的Intent            Intent intent = new Intent();            intent.setAction("org.crazyit.aidl.action.AIDL_SERVICE");            // 绑定远程Service            bindService(intent, conn, Service.BIND_AUTO_CREATE);            get.setOnClickListener(new OnClickListener()            {                @Override                public void onClick(View arg0)                {                        try                        {                            // 获取、并显示远程Service的状态                            color.setText(catService.getColor());                            weight.setText(catService.getWeight() + "");                        }                        catch (RemoteException e)                        {                             e.printStackTrace();                        }                 }           });        }        @Override        public void onDestroy()        {            super.onDestroy();            // 解除绑定            this.unbindService(conn);        }}
传递复杂数据的AIDL Service

本实例也是一个调用AIDL Service的例子,与前面实例不同的是,该实例所传输的数据类型,是自定义的数据类型。

本实例用到了两个自定义类型:Person与Pet,其中Person对象作为调用远程Service的参数,而Pet作为返回值,就像RMI要求远程调用的参数跟返回值都必须实现Serializable接口,Android要求远程Service的参数跟返回值都必须实现Parcelable接口。

实现Parcelable接口不仅要求实现该接口里定义的方法,而且要求在实现类中定义一个名为CREATOR,类型为Parcelable.Creator的静态Field。除此之外,还要求使用AIDL代码来定义这些自定义类型。

注意:实现Parcelable接口相当于Android提供的一种自定义序列化机制。Java序列化机制要求序列化类必须实现Serializable接口,而Android序列化机制,则要求序列化类必须实现Parcelable接口。

要定义Person类,先要用AIDL来定义Person类

........\src\org\crazyit\service\Person.aidl

parcelable Person; 

使用AIDL定义自定义类只需要一行代码即可,如上。

接下来定义一个实现Parcelable接口的Person类

........\src\org\crazyit\service\Person.java

/** * */package org.crazyit.service;import android.os.Parcel;import android.os.Parcelable;/** * Description: * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>* <br/>Copyright (C), 2001-2014, Yeeku.H.Lee * <br/>This program is protected by copyright laws. * <br/>Program Name: * <br/>Date: * @author  Yeeku.H.Lee kongyeeku@163.com * @version  1.0 */public class Person implements Parcelable{    private Integer id;    private String name;    private String pass;    public Person()    {    }    public Person(Integer id, String name, String pass)    {        super();        this.id = id;        this.name = name;        this.pass = pass;    }    public Integer getId()    {        return id;    }    public void setId(Integer id)    {        this.id = id;    }    public String getName()    {        return name;    }    public void setName(String name)    {        this.name = name;    }    public String getPass()    {        return pass;    }    public void setPass(String pass)    {        this.pass = pass;    }    @Override    public int hashCode()    {        final int prime = 31;        int result = 1;        result = prime * result + ((name == null) ? 0 : name.hashCode());        result = prime * result + ((pass == null) ? 0 : pass.hashCode());        return result;    }    @Override    public boolean equals(Object obj)    {        if (this == obj)            return true;        if (obj == null)            return false;        if (getClass() != obj.getClass())            return false;        Person other = (Person) obj;        if (name == null)        {            if (other.name != null)                return false;            }        else if (!name.equals(other.name))            return false;        if (pass == null)        {            if (other.pass != null)                return false;         }         else if (!pass.equals(other.pass))              return false;        return true;     }    @Override    public int describeContents()    {        return 0;    }    @Override    public void writeToParcel(Parcel dest, int flags)    {        //把该对象所包含的数据写到Parcel        dest.writeInt(id);        dest.writeString(name);        dest.writeString(pass);    }    // 添加一个静态成员,名为CREATOR,该对象实现了Parcelable.Creator接口    public static final Parcelable.Creator<Person> CREATOR = new Parcelable.Creator<Person>()    {        @Override        public Person createFromParcel(Parcel source)        {            // 从Parcel中读取数据,返回Person对象            return new Person(source.readInt(), source.readString(), source.readString());        }        @Override        public Person[] newArray(int size)        {             return new Person[size];        }    };}
实现Parcelable接口主要是实现writeToParcel(Parcel dest,int flags)方法,该方法负责把Person对象的数据写入到Parcel中。于此同时,该方法必须定义一个类型为
Parcelable.Creator<Person>,名为CREATOR的静态变量,该静态常量的值负责从Parcel数据包中恢复Person对象,因此该对象定义的createFromPerson()方法用于恢复Person对象。

实际上让Person实现Parcelable接口也是一种序列化机制,只是Android没有直接使用Java提供的序列化机制,而是选择使用这种轻量化的序列化机制。

定义Pet类的方法跟定义Person类一样,此处不再给出代码。

接下来使用AIDL来定义通信接口

........\src\org\crazyit\service\IPet.aidl

package org.crazyit.service;
import org.crazyit.service.Pet;
import org.crazyit.service.Person;

interface IPet{
    //定义一个Person对象,作为传入参数
   List<Pet> getPets(in Person owner);
}

在AIDL接口中定义方法时,需要指定参数的传递模式,对于Java语言来讲,一般都是采用传入参数的方式,因此上面指定为in模式。

ADT工具会自动生成相应的java文件,这不需要开发者关心

接下来需要定义一个Service,让Service的onBind()方法返回IPet实现类的实例。该Service类的代码如下。

    /**    *    */    package org.crazyit.service;    import java.util.ArrayList;    import java.util.HashMap;    import java.util.List;    import java.util.Map;    import org.crazyit.service.IPet.Stub;    import android.app.Service;    import android.content.Intent;    import android.os.IBinder;    import android.os.RemoteException;    /**     * Description:     * <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>     * <br/>Copyright (C), 2001-2014, Yeeku.H.Lee     * <br/>This program is protected by copyright laws.     * <br/>Program Name:     * <br/>Date:     * @author  Yeeku.H.Lee kongyeeku@163.com     * @version  1.0     */    public class ComplexService extends Service    {        private PetBinder petBinder;        private static Map<Person , List<Pet>> pets = new HashMap<Person , List<Pet>>();        static        {            // 初始化pets Map集合            ArrayList<Pet> list1 = new ArrayList<Pet>();            list1.add(new Pet("旺财" , 4.3));            list1.add(new Pet("来福" , 5.1));            pets.put(new Person(1, "sun" , "sun") , list1);            ArrayList<Pet> list2 = new ArrayList<Pet>();            list2.add(new Pet("kitty" , 2.3));            list2.add(new Pet("garfield" , 3.1));            pets.put(new Person(2, "bai" , "bai") , list2);        }        // 继承Stub,也就是实现额IPet接口,并实现了IBinder接口        public class PetBinder extends Stub        {            @Override            public List<Pet> getPets(Person owner) throws RemoteException            {                // 返回Service内部的数据                return pets.get(owner);            }        }        @Override        public void onCreate()        {             super.onCreate();             petBinder = new PetBinder();        }        @Override        public IBinder onBind(Intent arg0)        {        /* 返回catBinder对象       * 在绑定本地Service的情况下,该catBinder对象会直接       * 传给客户端的ServiceConnection对象       * 的onServiceConnected方法的第二个参数;       * 在绑定远程Service的情况下,只将catBinder对象的代理       * 传给客户端的ServiceConnection对象       * 的onServiceConnected方法的第二个参数;       */        return petBinder; //①        }        @Override        public void onDestroy()        {        }    }

在AndroidManifest.xml中配置Service,跟之前一样。

定义客户端,将IPet.aidl文件复制过去,同时还要将Person.aidl,Person.java,Pet.aidl,Pet.java文件复制过去。
客户端依然按之前方式绑定远程Service,并在ServiceConnection实现类的onServiceConnected()方法中获得远程Service的onBind()方法返回的代理对象即可。

package org.crazyit.client;import java.util.List;import org.crazyit.service.IPet;import org.crazyit.service.Person;import org.crazyit.service.Pet;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.ArrayAdapter;import android.widget.Button;import android.widget.EditText;import android.widget.ListView;/** * Description: <br/>* site: <a href="http://www.crazyit.org">crazyit.org</a> <br/>* Copyright (C), 2001-2012, Yeeku.H.Lee <br/>* This program is protected by copyright laws. <br/>* Program Name: <br/>* Date:  * @author Yeeku.H.Lee kongyeeku@163.com * @version 1.0 */public class ComplexClient extends Activity{    private IPet petService;    private Button get;    EditText personView;    ListView showView;    private ServiceConnection conn = new ServiceConnection()    {        @Override        public void onServiceConnected(ComponentName name, IBinder service)        {            // 获取远程Service的onBind方法返回的对象的代理            petService = IPet.Stub.asInterface(service);        }        @Override        public void onServiceDisconnected(ComponentName name)        {            petService = null;        }    };    @Override    public void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        personView = (EditText) findViewById(R.id.person);        showView = (ListView) findViewById(R.id.show);        get = (Button) findViewById(R.id.get);        // 创建所需绑定的Service的Intent        Intent intent = new Intent();        intent.setAction("org.crazyit.aidl.action.COMPLEX_SERVICE");        // 绑定远程Service        bindService(intent, conn, Service.BIND_AUTO_CREATE);        get.setOnClickListener(new OnClickListener()        {            @Override            public void onClick(View arg0)            {                try                 {                    String personName = personView.getText().toString();                    // 调用远程Service的方法                    List<Pet> pets = petService.getPets(new Person(1,personName, personName)); //①                    // 将程序返回的List包装成ArrayAdapter                    ArrayAdapter<Pet> adapter = new ArrayAdapter<Pet>(ComplexClient.this,android.R.layout.simple_list_item_1, pets);                    showView.setAdapter(adapter);                }                catch (RemoteException e)                {                    e.printStackTrace();                }            }        });    }    @Override    public void onDestroy()    {        super.onDestroy();        // 解除绑定        this.unbindService(conn);    }}
除了由用户自行开发启动的服务之外,安卓还提供了大量的系统服务,开发者只要在程序中调用Context的如下方法即可获得这些系统服务,
getSystemService(String ServiceName),根据服务名称来获得服务

完整代码链接 代码下载

0 0