Android studio 上使用aidl总结

来源:互联网 发布:淘宝详情图怎么上传 编辑:程序博客网 时间:2024/06/05 09:27

AIDL

  Android Interface definition language 接口定义语言。不同的进程是不能通信的,通过aidl的方式来实现内存的共享,实现进程间的通信。

  只有当你允许来自不同的客户端访问你的服务并且需要处理多线程问题时你才必须使用AIDL,其他情况下你都可以选择其他方法,如使用Messager,也能跨进程通讯。可见AIDL是处理多线程、多客户端并发访问的。而Messager是单线程处理

Stub

  stub是为了方便client,service交互而生成出来的代码。要使用AIDLService需要以aidl文件的方式提供服务接口,AIDL工具将生成一个相应的java接口,并且在生成的服务接口中包含一个功能调用的stub服务桩类。Service的实现类需要去继承这个stub服务桩类。ServiceonBind方法会返回实现类的对象,之后你就可以使用它了。

  交互过程client<-->proxy<-->stub<-->service。stub和proxy是为了方便client/service交互而生成出来的代码,这样client/service的代码就会比较干净,不会嵌入很多很难懂的与业务无关的代码

数据

AIDL只支持有限的数据类型:

1、java的简单类型(int double boolean),不需要导入

2、String和charsequence,不需要导入。

3、List和map,元素类型支持的是aidl支持的数据类型,不需要导入。

4、实现parcelable的类,需要导入。


内容

Android studio两个moduleeclipse中的两个project 

Module1

首先定义AIDL接口:

package com.xhyy.lxr.testaidlapp;import com.xhyy.lxr.entity.Dog;// Declare any non-default types here with import statementsinterface MyAidl {    String getName();    Dog getDog();}

Android studio aidl生成:

在要生成aidl的包中 点击右键 生成AIDL File并命名,studio自动生成aidl文件夹和一致的包。声明两个方法 返回String和实体类。



package com.xhyy.lxr.entity;// Declare any non-default types here with import statementsparcelable Dog;

Dog实体类类实现Parcelable接口  在Dog类的包点击右键,生成DogAIDL。肯定会重名,先随意写,再次rename即可。

结构如下



写完aidl之后,make project,这和普通的类不同,不会立即就能索引到。(还可以在gradle操作,待研究)


核心:MyService:

public class MyService extends Service {    @Nullable    @Override    public IBinder onBind(Intent intent) {        return binder;    }    MyAidl.Stub binder= new MyAidl.Stub() {        @Override        public String getName() throws RemoteException {            return "我的刀快,是因为我直接。";        }        @Override        public Dog getDog() throws RemoteException {            return new Dog("洪七");        }    };}


Manifest中注册,需要的是隐式intent绑定,故声明action

<service android:name="com.xhyy.lxr.service.MyService">    <intent-filter>        <action android:name="lxr.service.MyService"></action>    </intent-filter></service>

 



Module2

生成aidl。需要module1aidl结构完全一致,先在java中建包(即使包是空的),再根据包生成aidl。内容和module1完全一致。

Aidl的内容复制粘贴就可以了。

两个module传递了dog实体类,因此module中也需要dog类来对接受到的数据进行规范。



核心操作:

public void doClick(View view){    Intent intent=new Intent("lxr.service.MyService");    Intent exIntent=getExplicitIntent(MainActivity.this,intent);    bindService(exIntent,serviceConnection,BIND_AUTO_CREATE);}


ServiceConnection serviceConnection=new ServiceConnection() {    @Override    public void onServiceConnected(ComponentName name, IBinder service) {        myAidl=MyAidl.Stub.asInterface(service);        try {            String str=myAidl.getName();            Dog dog=myAidl.getDog();            mainTv.setText(dog.toString()+":"+str);        } catch (RemoteException e) {            e.printStackTrace();        }    }    @Override    public void onServiceDisconnected(ComponentName name) {    }};

关键代码:
  myAidl=MyAidl.Stub.asInterface(service);


得到module2 MyAidl

绑定的时候如果用的是intent Android 5.0以上就会崩溃。

解决方法,把intent变成显式的。(除此之外,还可以加上包名)

http://blog.csdn.net/vrix/article/details/45289207

注意action一定要谨慎,防止多写一个空格,连看都看不出来。


异常

包名不一致

java.lang.SecurityException: Binder invocation to an incorrect interface

Action加了空格    

java.lang.IllegalStateException: Could not execute method of the activity空指针

5.0以上 隐式intent

Caused by: java.lang.IllegalArgumentException: Service Intent must be explicit: Intent

魅族的开发者模式打开

*#*#6961#*#*



完毕。



1 0
原创粉丝点击