1、AIDL (Android Interface Definition Language )

2、AIDL 适用于 进程间通信,并且与Service端多个线程并发的情况,如果只是单个线程 可以使用 Messenger ,如果不需要IPC 可以使用Binder

3、AIDL语法:基础数据类型都可以适用,List Map等有限适用。static field 不适用。

4、AIDL基本用法

第一步:实现.aidl文件

接口描述文件
、导入的包名
、如果有使用Object对象,需要该对象 implement Parcelable 接口,并且需要导入该接口包名+类名;
如果是primitive type 不需要这步。
、定义方法名称。
、所有的.aidl文件已经需要传递的对象接口需要在Service 与Client中各一份 package com.aidl;
import com.aidl.Data;
interface IaidlData
{
int getPid(); String getName(); com.aidl.Data getData();
}

2、在Service中实现.aidl 接口。实际实现的接口是在 gen中自动生成的 IaidlData.java的抽象内部类 Stub

、需要在配置文件Androidmanifest.xml文件中声明Service,并且添加intent-filter 节点 的action属性,
此属性一般可以使用 aidl的包名+文件名(因为该值在服务器与客户端一致)
、需要实现IaidlData.aidl文件中定义的接口。
aidl文件是一个接口描述文件,会在gen中自动生成一个同名的IaidlData.java接口文件,该接口文件包含一个抽象类stub,其继承了android.os.Binder、实现IaidlData接口 故,我们实际需要实现的是Stub抽象类。 public class AidlService extends Service
{
public void onCreate()
{
Log.d("aidl", "aidlService--------------onCreate");
} public IBinder onBind(Intent intent)
{
return mBinder;
} private final IaidlData.Stub mBinder = new IaidlData.Stub()
{
public int getPid()
{
return Process.myPid();
} public String getName() throws RemoteException
{
return "go or not go is a problem";
} public Data getData() throws RemoteException
{
Data data = new Data();
data.id = Process.myPid();
data.name = "go or not go is a problem";
return data;
}
};
}

3、绑定Service ,并且获取IaidlService 对象

、建立连接,使用Action属性定位需要的Service
actoin的属性的采用aidl文件的类名+包名(与服务一致),之前需要在服务中设置相同的action属性,否则找不到服务。 、获取服务返回的stub对象,mIaidlData = IaidlData.Stub.asInterface(service); package com.clent; import com.aidl.IaidlData; import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View; public class AidlClientActivity extends Activity
{
IaidlData mIaidlData; public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
} protected void onStart()
{
super.onStart();
Log.d("aidl" , "onstart ----------bindservice-----"+IaidlData.class.getName());
Intent intent = new Intent(IaidlData.class.getName());
bindService(intent, mSecondaryConnection, BIND_AUTO_CREATE);
} private ServiceConnection mSecondaryConnection = new ServiceConnection()
{
public void onServiceConnected(ComponentName className, IBinder service)
{
Log.d("aidl", "onServiceConnected----------------");
mIaidlData = IaidlData.Stub.asInterface(service);
} public void onServiceDisconnected(ComponentName className)
{
mIaidlData = null;
}
}; public void onClick(View view)
{
System.out.println( " onclick--------------- : ");
if(mIaidlData != null)
{
try
{
System.out.println( " name : "+mIaidlData.getName()); System.out.println( " id : "+mIaidlData.getPid()); System.out.println( " data : "+mIaidlData.getData().id +" "+mIaidlData.getData().name);
}
catch (RemoteException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} }
} protected void onDestroy()
{
super.onDestroy();
unbindService(mSecondaryConnection);
}
}

4、如果aidl文件中需要传递Object对象,需要添加对应的.aidl文件

、定义该对象Data,并实现Parcelable
、添加Data.aidl文件,并采用如下方式编写导入Data
、需要在引用到Data的.aidl文件中 import com.aidl.Data
、需要在服务端和 客户端都添加 Data.aidl与 Data.java文件 并且需要一致。 Data.aidl
aidl文件:
package com.aidl;
parcelable Data;

5、添加 对应的Object类,并且实现Parcelable接口

public class Data implements Parcelable
{
public int id;
public String name; public static final Parcelable.Creator<Data> CREATOR = new Parcelable.Creator<Data>()
{
public Data createFromParcel(Parcel in)
{
return new Data(in);
} public Data[] newArray(int size)
{
return new Data[size];
}
}; public Data()
{
} private Data(Parcel in)
{
readFromParcel(in);
} public void readFromParcel(Parcel in)
{
id = in.readInt();
name = in.readString();
} public int describeContents()
{
return ;
} public void writeToParcel(Parcel dest, int flags)
{
dest.writeInt(id);
dest.writeString(name);
}
}

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的用法

    一.什么是AIDL服务   一般创建的服务并不能被其他的应用程序访问.为了使其他的应用程序也可以访问本应用程序提供的服务,Android系统采用了远程过程调用(Remote Procedure Cal ...

  7. android aidl

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

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

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

  9. AIDL小结

    AIDL : Android Interface Define Language(接口定义语言) Service中跨进程间通信利器.... 一般都会有Client端和Server端(Server端提供 ...

随机推荐

  1. OpenStack_Swift源代码分析——ObjectReplicator源代码分析(1)

    1.ObjectorReplicator的启动 首先执行启动脚本 swift-init object-replicator start 此执行脚本的执行过程和ring执行脚本执行过程差点儿相同.找到s ...

  2. js--27门面模式

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...

  3. samba-设定文件共享

    家里有两台电脑,一台装的是window,一台装的是ubuntu.一直想要实现两台电脑资源共享,今天终于成功实现了. smaba 具体简介就不说了,反正就知道,它可以创建一个服务器,然后让其他的电脑共享 ...

  4. activity 接回返回值

    activity 接回返回值 今天做订单列表显示 点击某一项显示订单详细信息,在详细activity中用户可以选择取消订单(未支付的状态下)当用户取消订单后订单列表也要改变状态,原来最初做法是所加载绑 ...

  5. 7.Linux 输入子系统分析

    为什么要引入输入子系统? 在前面我们写了一些简单的字符设备的驱动程序,我们是怎么样打开一个设备并操作的呢? 一般都是在执行应用程序时,open一个特定的设备文件,如:/dev/buttons .... ...

  6. ubuntu14中 memcached安装与使用

    第一步,先安装lib-event 下载lib-event  的包http://libevent.org/ 下载完之后,解压安装 ./configure –prefix=/usr (或 ./config ...

  7. 基于Eclipse的Android JNI层測试应用开发过程记录

    前言 本文记录一个Java层与JNI层參数与数据交互的应用程序开发过程.为实现一个功能完整的带Java与JNI的应用程序打下基础. 本文如果读者已搭建好Android的Eclipse与NDK开发环境, ...

  8. Android判断App是否在前台运行

    版权声明:本文为博主原创文章,未经博主允许不得转载. //当前应用是否处于前台 private boolean isForeground(Context context) { if (context ...

  9. PatentTips - Method for guest operating system integrity validation

    BACKGROUND The embodiments relate to guest operating system integrity validation, and more particula ...

  10. UWP 新手教程1——UWP的前世今生

    文件夹 引言 设备族群 UI 和通用输入模式 通用控件和布局面板 工具 自适应扩展 通用输入处理 引言 在本篇文章中,可以掌握下面知识: 设备族群,怎样决定目标设备 新的UI控件和新面板帮助你适应不同 ...