AIDL

来源:互联网 发布:tekla软件免费下载 编辑:程序博客网 时间:2024/06/06 01:54

  android里每个应用程序有一个独立的虚拟机,每个程序无法和另一个程序直接通信,保证了进程之内数据的安全,保证一个程序挂掉不至于影响另一个程序。Android里跨进程间通信必须通过系统底层间接的进行通信。

  在android SDK中提供了4种用于跨进程通讯的方式。这4种方式正好对应于android系统中4种应用程序组件:Activity、Content Provider、Broadcast和Service。其中Activity可以跨进程调用其他应用程序的Activity;Content Provider可以跨进程访问其他应用程序中的数据(以Cursor对象形式返回),当然,也可以对其他应用程序的数据进行增、删、改操 作;Broadcast可以向android系统中所有应用程序发送广播,而需要跨进程通讯的应用程序可以监听这些广播;Service和Content Provider类似,也可以访问其他应用程序中的数据,但不同的是,Content Provider返回的是Cursor对象,而Service返回的是Java对象,这种可以跨进程通讯的服务叫AIDL服务。

   Android Interface Defined Language(安卓接口定义语言),是android提供的一种进程间通信的解决方案;AIDL用起来比较耗内存、资源,所以没有必要所有地方都用AIDL。

如果只想IPC(跨进程)多个应用程序(无多线程)去访问只需要用binder,如果只想IPC(跨进程)不跨应用程序访问只需要用Messenger;只有你允许客户端从不同的应用程序为了进程间的通信而去访问你的service,以及想在你的service处理多线程,才去使用AIDL。


 一. AIDL的基本使用:

1.创建.aidl文件:

    as可以自动生成,但需要手动进行编译;eclipse则需要自己创建file,然后把后缀改为.aidl;如果不用IDE编译,也可以用sdk\build-tools\版本号\aidl.exe 工具进行编译,但注意要先添加环境变量。

    编译成功后会在相应的目录下生成.java文件。

     以下为官方的示例:

// 包名package com.example.android;// Declare any non-default types here with import statements/** Example service interface */interface IRemoteService { //实际就是一个接口,注意接口名要和aidl文件名一致    /** Request the process ID of this service, to do evil things with it. */    int getPid();//业务方法    /** Demonstrates some basic types that you can use as parameters     * and return values in AIDL.     */    void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,            double aDouble, String aString);}
2.继承接口,并向客户端暴露这个接口

public class RemoteService extends Service {    @Override    public void onCreate() {        super.onCreate();    } 
 private final IRemoteService.Stub mBinder = new IRemoteService.Stub() {    public int getPid(){        return Process.myPid();    }    public void basicTypes(int anInt, long aLong, boolean aBoolean,        float aFloat, double aDouble, String aString) {        // Does nothing    }};
    @Override    public IBinder onBind(Intent intent) {        // 客户端进行绑定的时候,就获取到了该接口        return mBinder;    }    private final IRemoteService.Stub mBinder = new IRemoteService.Stub() {        public int getPid(){//实现业务方法的逻辑            return Process.myPid();        }        public void basicTypes(int anInt, long aLong, boolean aBoolean,            float aFloat, double aDouble, String aString) {            // Does nothing        }    };}
现在当客户端调用bindService时,客户端的onServiceConected就会接收到这个binder实例了。

继承接口的时候有以下注意事项:

  • Incoming calls are not guaranteed to be executed on the main thread, so you need to thinkabout multithreading from the start and properly build your service to be thread-safe.
  • By default, RPC calls are synchronous. If you know that the service takes more than a fewmilliseconds to complete a request, you should not call it from the activity's main thread, becauseit might hang the application (Android might display an "Application is Not Responding"dialog)—you should usually call them from a separate thread in the client.
  • No exceptions that you throw are sent back to the caller.

3.客户端调用


IRemoteService mIRemoteService;private ServiceConnection mConnection = new ServiceConnection() {    // Called when the connection with the service is established    public void onServiceConnected(ComponentName className, IBinder service) {        // Following the example above for an AIDL interface,        // this gets an instance of the IRemoteInterface, which we can use to call on the service        mIRemoteService = IRemoteService.Stub.asInterface(service);    }    // Called when the connection with the service disconnects unexpectedly    public void onServiceDisconnected(ComponentName className) {        Log.e(TAG, "Service has unexpectedly disconnected");        mIRemoteService = null;    }};

二.数据传递

    支持的类型:

  • String
  • CharSequence
  • List(里面的元素都必须是支持的类型)
  • Map(里面的元素都必须是支持的类型)
  • 除short外的基本类型                  
  • 使用List Map 需要加 in/out标识
   自定义类型传递,步骤如下:

  1. 继承 Parcelable 接口.
  2. Implement writeToParcel, which takes thecurrent state of the object and writes it to aParcel.
  3. Add a static field called CREATOR to your class which is an object implementingtheParcelable.Creator interface.
  4. Finally, create an .aidl file that declares your parcelable class (as shown for theRect.aidl file, below).
Person类

public class Person implements Parcelable{
  private String name;
  private int age;
    /**
 * @param source
 */
    public Person(Parcel source) {
        this.name = source.readString();
        this.age = source.readInt();
   }

    public Person(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        //塞进去 注意读写的顺序
        dest.writeString(name);
        dest.writeInt(age);
    }
    public static final Creator<Person>  CREATOR = new Creator<Person>() {

        @Override//取出来 注意读写的顺序
        public Person createFromParcel(Parcel source) {
            return new Person(source);
        }

        @Override
        public Person[] newArray(int size) {
            return new Person[size];
        }
    };
    public String toString() {
        return "Person{name:"+name+ "   age:"+age+ "}";
    };
}

IRemoteService.aidl文件

package com.pigpp.aidl;

import com.pigpp.aidl.Person;
 
      interface IRemoteService {
    List<Person> add(in Person person);
}

Person.aidl(必须有,不然在aidl文件里导入Person类时找不到)

package com.pigpp.aidl;

parcelable Person;

服务端的service类,供客户端绑定后调用服务端的业务方法:

public class IRemoteService extends Service {
    List<Person> persons;
    private IBinder mBinder = new PersonCURD.Stub() {

        @Override
        public List<Person> add(Person person) throws RemoteException {
            persons = new ArrayList<>();
            persons.add(person);
            return persons;
        }
    };

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

}

客户端MainActivity


  
    private Button btn;
    PersonCURD mPersonCURD;
    private ServiceConnection conn = new ServiceConnection() {
        
        @Override
        public void onServiceDisconnected(ComponentName name) {
            mPersonCURD = null ;            
        }
        
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mPersonCURD = PersonCURD.Stub.asInterface(service);
            
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn = (Button) findViewById(R.id.btn);
         Intent intent = new Intent("com.pigpp.aidl.IRemoteService");
         bindService(intent, conn , Context.BIND_AUTO_CREATE);
        btn.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                try {
                    List<Person> persons = mPersonCURD.add(new Person("张三",12));
                    Log.d("TAG", persons.toString());
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        });
    }




-------------------------------End-----------------------------------------------

参考文章:http://android.blog.51cto.com/268543/537684/

参考视频:http://www.imooc.com/learn/606

代码地址:http://download.csdn.net/detail/u010431640/9449801




      

 

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 红米手机黑屏打不开怎么办 手机萤石云视频下载打不开怎么办 oppo打开网页视频慢怎么办? 晒课显示待提交怎么办 华为手机无法访问移动网络怎么办 晒课上传课堂实录太大怎么办 手机酷狗音乐下载要钱怎么办 手机酷狗下载要钱怎么办 酷我音乐没有声音怎么办 手机酷我音乐没有声音怎么办 酷我音乐歌曲下载收费怎么办 网易云下载超过每日上限怎么办 全民k歌领不了花怎么办 安卓全民k歌延迟怎么办 全民k歌唱歌延迟怎么办 全民k歌耳机延迟怎么办 word文档打开是乱码怎么办 全民k歌不能录音怎么办 全民k歌登录不上怎么办 平果手机迅雷闪退怎么办 电脑打开央视影音死机怎么办 先锋影音二级网页打不开怎么办 手机qq音乐登录失效怎么办 酷狗账号忘记了怎么办 手机qq音乐听不了歌怎么办 第一试用网密码忘了怎么办 玩h1z1画面卡顿怎么办 uu跑腿抢不到单怎么办 比特币加密忘了怎么办 路虎发现cd卡死怎么办 苹果手机帐号被锁定怎么办 苹果手机帐号锁定了怎么办 微博帐号被锁定怎么办 微博显示帐号被锁定怎么办 uc屏蔽了一个网站怎么办 uu跑腿送货遇到不方便收货怎么办 雷神加速器忘记暂停怎么办 obs直播开摄像头吃鸡掉帧怎么办 陌陌收到的礼物怎么办 吃了油腻的东西恶心怎么办 主播工资不发怎么办