博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android studio 上使用aidl总结
阅读量:4030 次
发布时间:2019-05-24

本文共 4060 字,大约阅读时间需要 13 分钟。

AIDL

   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

 

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);}

public static Intent getExplicitIntent(Context context, Intent implicitIntent) {        // Retrieve all services that can match the given intent        PackageManager pm = context.getPackageManager();        List            resolveInfo = pm.queryIntentServices(implicitIntent, 0);        // Make sure only one match was found        if (resolveInfo == null || resolveInfo.size() != 1) {            return null;        }        // Get component info and create ComponentName        ResolveInfo serviceInfo = resolveInfo.get(0);        String packageName = serviceInfo.serviceInfo.packageName;        String className = serviceInfo.serviceInfo.name;        ComponentName component = new ComponentName(packageName, className);        // Create a new intent. Use the old one for extras and such reuse        Intent explicitIntent = new Intent(implicitIntent);        // Set the component to be explicit        explicitIntent.setComponent(component);        return explicitIntent;    }
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变成显式的。(除此之外,还可以加上包名)

注意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#*#*

完毕。
你可能感兴趣的文章
Oracle树查询及相关函数
查看>>
Struts2中配置Servlet详解
查看>>
weblogic10和hibernate3 冲突解决方案
查看>>
WebLogic部署SSH2项目的小结
查看>>
JS+Struts2多文件上传完整示例
查看>>
Hibernate中DetachedCriteria的使用
查看>>
ORM是什么?
查看>>
关于Hibernate的一些常规问题
查看>>
Oracle中视图的创建和处理方法
查看>>
在Hibernate应用中使用视图
查看>>
MyEclipse 8.6 特性,安装与优化
查看>>
Oracle 查询并删除重复记录的SQL语句
查看>>
OnGestureListener的一些体会
查看>>
完全精通java输入输出流
查看>>
Spring与weblogic jndi集成
查看>>
android签名
查看>>
Ruby环境搭建与hello world
查看>>
rails中的form表单总结
查看>>
dip,px,pt,sp 的区别
查看>>
关于做android+J2ee系统集成开发的一点心得
查看>>