AIDL的使用步骤

aidl远程调用传递的参数和返回值支持Java的基本类型(int long booen char byte等)和String,List,Map等。当然也支持一个自定义对象的传递。

服务端

新建一个MyAidlDemoServer工程,然后在java目录下右键新建一个aidl File,然后在该目录下新建一个IMyAidlInterface.aidl文件,代码如下:

  

修改生成的.aidl文件中的内容

 interface IMyAidlInterface {

     int add(int arg1, int arg2);

 }  //aidl文件里面的代码不需要加任何修饰符

这里定义了一个IMyAidlInterface接口,里面定义的add方法用于求和计算。

然后Build当前工程(Build选项里的Make Project)。

会发现在app/build/generated/source/aidl/debug目录下会生成一个与IMyAidlInterface.aidl文件同样包名的一个文件,该文件下面自动生成IMyAidlInterface文件,该文件里面自动实现了一些方法用于远程调用。

编写远程服务

新建MyService类继承Service,并实现以下代码。

  

 public class MyService extends Service {
IMyAidlInterface.Stub mStub = new IMyAidlInterface.Stub() {
@Override
public int add(int arg1, int arg2) throws RemoteException {
return arg1 + arg2;
}
}; @Override
public IBinder onBind(Intent intent) {
return mStub;
}
}

服务里的代码重写了IMyAidlInterface.Stub类中的 add方法,然后通过重写onBind()方法将重写的IMyAidlInterface.Stub类返回出去。

然后在AndroidManifest.xml对Service进行配置。

  <service
android:process=":remote"
android:name=".MyService"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="co.example.leo.myService"/>
</intent-filter>
</service>

这里设置了android:process属性,并且设置为":remote"。

android:process=":remote",代表在应用程序里,当需要该service时,会自动创建新的进程。而如果是android:process="remote",没有“:”分号的,则创建全局进程,不同的应用程序共享该进程。

然后添加了一个意图过滤器。

客户端

新建MyAidlDemoCustomer工程,然后直接把服务端的aidl目录直接拷贝到客户端的main目录下。这么一来客户端的aidl就无需编写了,直接和服务端的一模一样。包括路径的包名等。 当然也可以在客户端这边重新写aidl文件。

编辑布局文件:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"> <TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp" />
</LinearLayout>

这里只用了一个TextView来显示最终的计算结果。

然后编辑客户端的调用代码:

 public class MainActivity extends AppCompatActivity {

         TextView tv;
IMyAidlInterface mStub; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); tv = (TextView)findViewById(R.id.tv); Intent intent = new Intent();
//由于是隐式启动Service 所以要添加对应的action,A和之前服务端的一样。
intent.setAction("co.example.leo.myService");
//android 5.0以后直设置action不能启动相应的服务,需要设置packageName或者Component。
intent.setPackage("co.example.leo.myaidldemoserver"); //packageName 需要和服务端的一致.
bindService(intent,serviceConnection,BIND_AUTO_CREATE);
} private ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
//调用asInterface()方法获得IMyAidlInterface实例
mStub = IMyAidlInterface.Stub.asInterface(service);
if (mStub == null) {
Log.e("MainActivity", "the mStub is null");
} else { //当mStub不为空就调用其add方法进行计算,并显示到TextView上面。
try {
int value = mStub.add(, );
tv.setText(value + "");
} catch (RemoteException e) {
e.printStackTrace();
}
}
} @Override
public void onServiceDisconnected(ComponentName name) { }
}; @Override
protected void onDestroy(){
//解绑服务
super.onDestroy();
unbindService(serviceConnection);
}
}
最后安装上客户端和服务端,打开客户端后会发现已经调用了服务端的方法并计算出了结果。

总结

这是一个在AS下最简单的一个AIDL编程: 
1.服务端创建一个aidl目录,然后在该目录下新建一个.aidl为后缀的接口类,该类定义远程调用的接口方法。 
2.build编译之后会在app/build/generated/source/aidl/debug目录下会生成aidl远程实现类,该类是AS自动生成的。 
3.在AndroidManifest.xml下配置Service的action和process属性。 
4.将服务端的aidl目录拷贝到客户端相应的目录下,然后编写客户端调用代码,AS下简单的aidl编程就ok了。

 

AndroidStudio实现AIDL的更多相关文章

  1. AndroidStudio 使用AIDL

    http://blog.csdn.net/ducklikejava/article/details/51559244 Android Studio中写的一个AIDL的小DEMO. 步骤很繁琐,本来不准 ...

  2. Android-Service基本用法、AIDL、Binder连接池详解

    本文介绍Service与Activity之间的通信,文章包含以下内容: 一.Service基本用法 二.通过AIDL实现Service与Activity跨进程通信 三.Binder连接池 四.使用Me ...

  3. Android IPC机制之AIDL

    什么是AIDL AIDL:Android Interface Definition Language,即Android接口定义语言. Android系统中的进程之间不能共享内存,因此,需要提供一些机制 ...

  4. [转]AndroidStudio导出jar包

    原文链接:http://blog.csdn.net/hjq842382134/article/details/38538097# 1. 不像在Eclipse,可以直接导出jar包.AndroidStu ...

  5. [Android Pro] AndroidStudio导出jar包

    reference :  http://blog.csdn.net/beijingshi1/article/details/38681281 不像在Eclipse,可以直接导出jar包.Android ...

  6. 一起简单写一下AIDL,入个门

    前话 最近接触了Android开发的一个新知识,AIDL(¬_¬因为到现在都没用过) 因此不断谷歌找资料找Demo,自己尝试写一下. 因为用AndroidStudio作为开发环境,期间遇到过许多问题, ...

  7. Android的IPC机制(一)——AIDL的使用

    综述 IPC(interprocess communication)是指进程间通信,也就是在两个进程间进行数据交互.不同的操作系统都有他们自己的一套IPC机制.例如在Linux操作系统中可以通过管道. ...

  8. AIDL进程间调用与Binder的简单介绍

    Binder是安卓中特有的一种进程间通信(IPC)方式,从Unix发展而来的手段,通信双方必须处理线程同步.内存管理等复杂问题,传统的Socket.匿名通道(Pipe).匿名管道(FIFO).信号量( ...

  9. AndroidStudio学习记录

    AndroidStudio学习记录 1. 插件的使用. plugins.jetbrains.com插件网站. 2. 目录介绍: 1.Studio中有Project和Module的概念,前面说到Stud ...

随机推荐

  1. Sex linkage

    I.8 Sex linkage 单倍体:性别决定基因(S\s)和与性别决定基因连锁的等位基因(A\a)存在于同一套遗传物质上,其配子结合和减数分裂图示如下: 如果性别是由染色体区域决定的,自然选择会避 ...

  2. 物联网应用层协议选择和分析--MQTT、CoAP 、HTTP、XMPP、SoAP

    MQTT协议 MQTT(Message Queuing Telemetry Transport,消息队列遥测传输)最早是IBM开发的一个即时通讯协议,MQTT协议是为大量计算能力有限且工作在低带宽.不 ...

  3. php面向对象理解(一)

    常用的继承过程,以及对public.private.protected修饰符的理解: /*****************************父类************************* ...

  4. The website is API(4)

    1.淘宝商品信息定向爬虫 目标:获取淘宝搜索页面信息,提取其中的商品名称和价格 理解:淘宝的搜索接口 翻页的处理 技术路线:requests+re https://s.taobao.com/searc ...

  5. Docker系列四: 使用UI管理docker容器

    一.什么是Portainer? Portainer是Docker的图形化管理工具,提供状态显示面板.应用模板快速部署.容器镜像网络数据卷的基本操作(包括上传下载镜像,创建容器等操作).事件日志显示.容 ...

  6. Linux_centos安装后无法进入图形界面

    问题 直接默认进入字符界面 root之后init 5也没用 解决方法 出现问题的原因在于安装时选择了最小安装,如图所示

  7. OLED带来全新视觉体验

    2013年1月,在国际消费电子展(CES)上世界首款曲面OLED电视--LG OLED电视亮相,LG Display将曲面与OLED完美结合的面板技术让显示设备的外观和品质都达到了一个全新的高度,惊艳 ...

  8. Invalid action class configuration that references an unknown class解决方案

    Sturts2整合后时出现诡异的异常: java.lang.RuntimeException: Invalid action class configuration that references a ...

  9. php单例模式的常见应用场景

    单例模式(Singleton)也叫单态模式,是设计模式中最为简单的一种模式,甚至有些模式大师都不称其为模式,称其为一种实现技巧,因为设计模式讲究对象之间的关系的抽象,而单例模式只有自己一个对象,也因此 ...

  10. 翻转链表和k个一组翻转以及两两反转

    一.输入一个链表,输出反转后的链表. 非递归实现: # -*- coding:utf-8 -*- # class ListNode: # def __init__(self, x): # self.v ...