AIDL ---(Android Interface Definition Language)Android接口定义语言

  简述:

1、什么时候使用?

通信方式 使用场景
AIDL IPC 、多个应用程序、多线程
Binder IPC 、多个应用程序
Messenger IPC
注:  IPC --- (Inter-Process communication)进程间通信/跨进程通信

    

2、如何实现 AIDL 通信?

   下面是项目目录结构图:客户端与服务端代码要保证 aidl 文件夹下内容的一致

    步骤①:创建一个AIDL文件. (在服务端)

 // IMyAidlInterface.aidl
package com.anglus.aidl; // Declare any non-default types here with import statements interface IMyAidlInterface {
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
// 计算两个数的和
int add(int num1, int num2);
}

    步骤②:实现自定义的AIDL 接口文件(在服务端)

  public class IRemoteService extends Service {

    @Override
public IBinder onBind(Intent intent) {
return mIBinder;
} private IBinder mIBinder = new IMyAidlInterface.Stub() {
@Override
public int add(int num1, int num2) throws RemoteException {
Log.i("IRemoteService", "收到远程的请求,传入的参数是:" + num1 + "和" + num2);
return num1 + num2;
}
};
}

      一定不要忘了在 AndroidManifest.xml 文件中注册服务:

  <service
android:name=".IRemoteService"
android:enabled="true"
android:exported="true"/>

    步骤③: 绑定服务,得到远程服务对象,即可在客户端进行使用。

   private void bindService() {
// 绑定到服务
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.anglus.aidl",
"com.anglus.aidl.IRemoteService"));
bindService(intent, conn, Context.BIND_AUTO_CREATE);
} private ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// 拿到远程服务
iMyAidlInterface = IMyAidlInterface.Stub.asInterface(service);
} @Override
public void onServiceDisconnected(ComponentName name) {
// 回收资源
iMyAidlInterface = null;
}
};

      注意:在 new ComponetName() 中,第一个参数是:服务端的 项目包名;第二个参数是:要绑定的服务 包名 + 类名。

    

Android AIDL 一探究竟的更多相关文章

  1. Android AIDL自动生成Java文件测试

    /******************************************************************************** * Android AIDL自动生成 ...

  2. Using self-defined Parcelable objects during an Android AIDL RPC / IPC call

    Using self-defined Parcelable objects during an Android AIDL RPC / IPC call In my previous post “Usi ...

  3. AIDL/IPC Android AIDL/IPC 进程通信机制——超具体解说及使用方法案例剖析(播放器)

    首先引申下AIDL.什么是AIDL呢?IPC? ------ Designing a Remote Interface Using AIDL 通常情况下,我们在同一进程内会使用Binder.Broad ...

  4. (转载)你真的理解Android AIDL中的in,out,inout么?

    前言 这其实是一个很小的知识点,大部分人在使用AIDL的过程中也基本没有因为这个出现过错误,正因为它小,所以在大部分的网上关于AIDL的文章中,它都被忽视了——或者并没有,但所占篇幅甚小,且基本上都是 ...

  5. Android AIDL使用详解_Android IPC 机制详解

    一.概述 AIDL 意思即 Android Interface Definition Language,翻译过来就是Android接口定义语言,是用于定义服务器和客户端通信接口的一种描述语言,可以拿来 ...

  6. android aidl

    参考: http://blog.csdn.net/u014614038/article/details/48399935 本文提供了一个关于AIDL使用的简单易懂的例子,分为客户端和服务端两部分,分别 ...

  7. android aidl 进程间通信需要注意的地方(android.os.TransactionTooLargeException)

    转自:http://blog.sina.com.cn/s/blog_4e1e357d0102wau9.html 1.bus工程实现通过service实现aidl实体类 2.actor工程通过发起bin ...

  8. Android AIDL 进行进程间通讯(IPC)

    编写AIDL文件时,需要注意: 1.接口名和aidl文件名相同. 2.接口和方法前不用加访问权限修饰符 (public.private.protected等,也不能用final.static). 3. ...

  9. [Android Pro] Android Support 包里究竟有什么

    reference to : http://www.2cto.com/kf/201411/350928.html 随着 Android 5.0 Lollipop 的发布,Android 又为我们提供了 ...

随机推荐

  1. [Unity菜鸟] 笔记2 —— 问题篇

    记录在学习<Unity 3.x 游戏开发 经典教材>时遇到的各种问题与笔记 1. 初始不能降低Terrain的高度,需要到Terrain设置的第二个按钮中将Height从0调高 (注意:最 ...

  2. Android:控件布局(单帧布局)FrameLayout

    FrameLayout:所有控件位于左上角,并且直接覆盖前面的子元素. 在最上方显示的层加上: android:clickable="true" 可以避免点击上层触发底层. 实例: ...

  3. C++中的指针与const

    刚开始接触C++时,指针和const之间的关系有点混乱,现在总结如下: 一.指向const变量的指针 #include<iostream.h> void main() { const in ...

  4. 220. Contains Duplicate III

    题目: Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  5. CodePage------Encoding 类支持的编码以及与这些编码关联的代码页(CodePage)

    Encoding 类 .NET Framework 4  表示字符编码. 继承层次结构 System.Object  System.Text.Encoding    System.Text.ASCII ...

  6. 十个免费的Web压力测试工具

    两天,jnj在本站发布了<如何在低速率网络中测试 Web 应用>,那是测试网络不好的情况.而下面是十个免费的可以用来进行Web的负载/压力测试的工具,这样,你就可以知道你的服务器以及你的W ...

  7. 读取Excel任务列表并显示在Outlook日历上

    前几天,公司发了一个任务安排,时间不固定,但要求准时到,为了给自己加一个提醒,也为了回顾一下以前的技术,特做了一个Demo. 读取Excel就不多说了,代码很简单,但支持老版本Excel和的版本Exc ...

  8. NOI2002银河英雄传说

    原先就看过这道题,觉得很复杂. 不知道为什么今天一看觉得好水啊…… 难道这就是并查集的启发式合并? 数组d[i]表示i到其父节点的距离,即中间隔了多少船舰. 数组sum[i]记录以i为根的集合总共有多 ...

  9. Java中的字符串驻留(String Interning)

    1. 首先String不属于8种基本数据类型,String是一个对象. 因为对象的默认值是null,所以String的默认值也是null:但它又是一种特殊的对象,有其它对象没有的一些特性. 2. ne ...

  10. 思考之spring的ioc

    控制反转 英语:Inversion of control,缩写为IoC 我想很多同学都会思考过这样的一个问题,控制反转,什么地方反转了,是不是翻译的不对? 这里插一句 当年马云借着盖茨的嘴说:“互联网 ...