- 浏览: 229346 次
- 性别:
- 来自: 广州
最新评论
-
Janne:
你好 有源代码?可以发到我的邮箱里学学吗?2731049993 ...
achartengine画出动态折线图的效果 -
anbo724:
我的邮箱 anbo724@gmail.com谢谢@
achartengine画出动态折线图的效果 -
anbo724:
你好 请问有源码没《?谢谢
achartengine画出动态折线图的效果 -
weiday123:
额,觉得这个会不会占堆内存?
AdapterView、Adapter优化 -
wen742538485:
为什么没有呢?权限没加还是发创建了给你删了再想创建?是不允许重 ...
Android中为你的应用程序添加桌面快捷方式
http://blog.csdn.net/stonecao/article/details/6425019
1.什么是aidl:aidl是 Android Interface definition language的缩写,一看就明白,它是一种android内部进程通信接口的描述语言,通过它我们可以定义进程间的通信接口
icp:interprocess communication :内部进程通信
2.既然aidl可以定义并实现进程通信,那么我们怎么使用它呢?文档/android-sdk/docs/guide/developing/tools/aidl.html中对步骤作了详细描述:
--1.Create your .aidl file - This file defines an interface (YourInterface.aidl) that defines the methods and fields available to a client.
创建你的aidl文件,我在后面给出了一个例子,它的aidl文件定义如下:写法跟java代码类似,但是这里有一点值得注意的就是它可以引用其它aidl文件中定义的接口,但是不能够引用你的java类文件中定义的接口
[java] view plaincopy
01.package com.cao.android.demos.binder.aidl;
02.import com.cao.android.demos.binder.aidl.AIDLActivity;
03.interface AIDLService {
04. void registerTestCall(AIDLActivity cb);
05. void invokCallBack();
06.}
--2.Add the .aidl file to your makefile - (the ADT Plugin for Eclipse manages this for you). Android includes the compiler, called AIDL, in the tools/ directory.
编译你的aidl文件,这个只要是在eclipse中开发,你的adt插件会像资源文件一样把aidl文件编译成java代码生成在gen文件夹下,不用手动去编译:编译生成AIDLService.java如我例子中代码
--3.Implement your interface methods - The AIDL compiler creates an interface in the Java programming language from your AIDL interface. This interface has an inner abstract class named Stub that inherits the interface (and implements a few additional methods necessary for the IPC call). You must create a class that extends YourInterface.Stub and implements the methods you declared in your .aidl file.
实现你定义aidl接口中的内部抽象类Stub,public static abstract class Stub extends android.os.Binder implements com.cao.android.demos.binder.aidl.AIDLService
Stub类继承了Binder,并继承我们在aidl文件中定义的接口,我们需要实现接口方法,下面是我在例子中实现的Stub类:
[java:showcolumns] view plaincopy
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
01.private final AIDLService.Stub mBinder = new AIDLService.Stub() {
02.
03. @Override
04. public void invokCallBack() throws RemoteException {
05. Log("AIDLService.invokCallBack");
06. Rect1 rect = new Rect1();
07. rect.bottom=-1;
08. rect.left=-1;
09. rect.right=1;
10. rect.top=1;
11. callback.performAction(rect);
12. }
13.
14.
15. @Override
16. public void registerTestCall(AIDLActivity cb) throws RemoteException {
17. Log("AIDLService.registerTestCall");
18. callback = cb;
19. }
20.};
Stub翻译成中文是存根的意思,注意Stub对象是在被调用端进程,也就是服务端进程,至此,服务端aidl服务端得编码完成了。
--4.Expose your interface to clients - If you're writing a service, you should extend Service and override Service.onBind(Intent) to return an instance of your class that implements your interface.
第四步告诉你怎么在客户端如何调用服务端得aidl描述的接口对象,doc只告诉我们需要实现Service.onBind(Intent)方法,该方法会返回一个IBinder对象到客户端,绑定服务时不是需要一个ServiceConnection对象么,在没有了解aidl用法前一直不知道它是什么作用,其实他就是用来在客户端绑定service时接收service返回的IBinder对象的:
[java] view plaincopy
01.AIDLService mService;
02.private ServiceConnection mConnection = new ServiceConnection() {
03. public void onServiceConnected(ComponentName className, IBinder service) {
04. Log("connect service");
05. mService = AIDLService.Stub.asInterface(service);
06. try {
07. mService.registerTestCall(mCallback);
08. } catch (RemoteException e) {
09.
10. }
11. }
12.
13.
14. public void onServiceDisconnected(ComponentName className) {
15. Log("disconnect service");
16. mService = null;
17. }
18.};
mService就是AIDLService对象,具体可以看我后面提供的示例代码,需要注意在客户端需要存一个服务端实现了的aidl接口描述文件,但是客户端只是使用该aidl接口,不需要实现它的Stub类,获取服务端得aidl对象后mService = AIDLService.Stub.asInterface(service);,就可以在客户端使用它了,对mService对象方法的调用不是在客户端执行,而是在服务端执行。
4.aidl中使用java类,需要实现Parcelable接口,并且在定义类相同包下面对类进行声明:
上面我定义了Rect1类
之后你就可以在aidl接口中对该类进行使用了
package com.cao.android.demos.binder.aidl;
import com.cao.android.demos.binder.aidl.Rect1;
interface AIDLActivity {
void performAction(in Rect1 rect);
}
注意in/out的说明,我这里使用了in表示输入参数,out没有试过,为什么使用in/out暂时没有做深入研究。
5.aidl使用完整示例,为了清除说明aidl使用,我这里写了一个例子,例子参考了博客:
http://blog.csdn.net/saintswordsman/archive/2010/01/04/5130947.aspx
作出说明
例子实现了一个AIDLTestActivity,AIDLTestActivity通过bindservice绑定一个服务AIDLTestService,通过并获取AIDLTestActivity的一个aidl对象AIDLService,该对象提供两个方法,一个是registerTestCall注册一个aidl对象,通过该方法,AIDLTestActivity把本身实现的一个aidl对象AIDLActivity传到AIDLTestService,在AIDLTestService通过操作AIDLActivity这个aidl远端对象代理,使AIDLTestActivity弹出一个toast,完整例子见我上传的资源:
http://download.csdn.net/source/3284820
文章仓促而成,有什么疑问欢迎大家一起讨论。
1.什么是aidl:aidl是 Android Interface definition language的缩写,一看就明白,它是一种android内部进程通信接口的描述语言,通过它我们可以定义进程间的通信接口
icp:interprocess communication :内部进程通信
2.既然aidl可以定义并实现进程通信,那么我们怎么使用它呢?文档/android-sdk/docs/guide/developing/tools/aidl.html中对步骤作了详细描述:
--1.Create your .aidl file - This file defines an interface (YourInterface.aidl) that defines the methods and fields available to a client.
创建你的aidl文件,我在后面给出了一个例子,它的aidl文件定义如下:写法跟java代码类似,但是这里有一点值得注意的就是它可以引用其它aidl文件中定义的接口,但是不能够引用你的java类文件中定义的接口
[java] view plaincopy
01.package com.cao.android.demos.binder.aidl;
02.import com.cao.android.demos.binder.aidl.AIDLActivity;
03.interface AIDLService {
04. void registerTestCall(AIDLActivity cb);
05. void invokCallBack();
06.}
--2.Add the .aidl file to your makefile - (the ADT Plugin for Eclipse manages this for you). Android includes the compiler, called AIDL, in the tools/ directory.
编译你的aidl文件,这个只要是在eclipse中开发,你的adt插件会像资源文件一样把aidl文件编译成java代码生成在gen文件夹下,不用手动去编译:编译生成AIDLService.java如我例子中代码
--3.Implement your interface methods - The AIDL compiler creates an interface in the Java programming language from your AIDL interface. This interface has an inner abstract class named Stub that inherits the interface (and implements a few additional methods necessary for the IPC call). You must create a class that extends YourInterface.Stub and implements the methods you declared in your .aidl file.
实现你定义aidl接口中的内部抽象类Stub,public static abstract class Stub extends android.os.Binder implements com.cao.android.demos.binder.aidl.AIDLService
Stub类继承了Binder,并继承我们在aidl文件中定义的接口,我们需要实现接口方法,下面是我在例子中实现的Stub类:
[java:showcolumns] view plaincopy
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
01.private final AIDLService.Stub mBinder = new AIDLService.Stub() {
02.
03. @Override
04. public void invokCallBack() throws RemoteException {
05. Log("AIDLService.invokCallBack");
06. Rect1 rect = new Rect1();
07. rect.bottom=-1;
08. rect.left=-1;
09. rect.right=1;
10. rect.top=1;
11. callback.performAction(rect);
12. }
13.
14.
15. @Override
16. public void registerTestCall(AIDLActivity cb) throws RemoteException {
17. Log("AIDLService.registerTestCall");
18. callback = cb;
19. }
20.};
Stub翻译成中文是存根的意思,注意Stub对象是在被调用端进程,也就是服务端进程,至此,服务端aidl服务端得编码完成了。
--4.Expose your interface to clients - If you're writing a service, you should extend Service and override Service.onBind(Intent) to return an instance of your class that implements your interface.
第四步告诉你怎么在客户端如何调用服务端得aidl描述的接口对象,doc只告诉我们需要实现Service.onBind(Intent)方法,该方法会返回一个IBinder对象到客户端,绑定服务时不是需要一个ServiceConnection对象么,在没有了解aidl用法前一直不知道它是什么作用,其实他就是用来在客户端绑定service时接收service返回的IBinder对象的:
[java] view plaincopy
01.AIDLService mService;
02.private ServiceConnection mConnection = new ServiceConnection() {
03. public void onServiceConnected(ComponentName className, IBinder service) {
04. Log("connect service");
05. mService = AIDLService.Stub.asInterface(service);
06. try {
07. mService.registerTestCall(mCallback);
08. } catch (RemoteException e) {
09.
10. }
11. }
12.
13.
14. public void onServiceDisconnected(ComponentName className) {
15. Log("disconnect service");
16. mService = null;
17. }
18.};
mService就是AIDLService对象,具体可以看我后面提供的示例代码,需要注意在客户端需要存一个服务端实现了的aidl接口描述文件,但是客户端只是使用该aidl接口,不需要实现它的Stub类,获取服务端得aidl对象后mService = AIDLService.Stub.asInterface(service);,就可以在客户端使用它了,对mService对象方法的调用不是在客户端执行,而是在服务端执行。
4.aidl中使用java类,需要实现Parcelable接口,并且在定义类相同包下面对类进行声明:
上面我定义了Rect1类
之后你就可以在aidl接口中对该类进行使用了
package com.cao.android.demos.binder.aidl;
import com.cao.android.demos.binder.aidl.Rect1;
interface AIDLActivity {
void performAction(in Rect1 rect);
}
注意in/out的说明,我这里使用了in表示输入参数,out没有试过,为什么使用in/out暂时没有做深入研究。
5.aidl使用完整示例,为了清除说明aidl使用,我这里写了一个例子,例子参考了博客:
http://blog.csdn.net/saintswordsman/archive/2010/01/04/5130947.aspx
作出说明
例子实现了一个AIDLTestActivity,AIDLTestActivity通过bindservice绑定一个服务AIDLTestService,通过并获取AIDLTestActivity的一个aidl对象AIDLService,该对象提供两个方法,一个是registerTestCall注册一个aidl对象,通过该方法,AIDLTestActivity把本身实现的一个aidl对象AIDLActivity传到AIDLTestService,在AIDLTestService通过操作AIDLActivity这个aidl远端对象代理,使AIDLTestActivity弹出一个toast,完整例子见我上传的资源:
http://download.csdn.net/source/3284820
文章仓促而成,有什么疑问欢迎大家一起讨论。
发表评论
-
Android Tween动画之RotateAnimation实现图片不停旋转
2012-11-26 22:38 1044本文主要介绍Android中如何使用rotate实现图片不停旋 ... -
Android实现widget定时更新
2012-11-04 20:20 887在开发Android的widget时,第一个需要解决的问题就是 ... -
来自腾讯、谷歌、百度等名企的精选面试五十题
2012-10-07 23:08 907http://www.apkway.com/thread-90 ... -
Android 中Parcelable的作用
2012-09-24 09:53 841android提供了一种新的类型:Parcel。本类被用作封装 ... -
[Android算法] 【eoeAndroid索引】史上最牛最全android开发知识汇总
2012-09-13 09:33 1096http://www.eoeandroid.com/threa ... -
安卓航班推荐70个具有商业实战性的精品Android源码
2012-08-01 00:00 919http://www.apkway.com/thread-58 ... -
Android测试教程汇总
2012-08-02 14:51 1071http://www.apkway.com/thread-67 ... -
Service 与 Thread 的区别
2012-07-26 00:10 884Service 与 Thread 的区别 很多时候,你可能 ... -
android 使用百度地图画轨迹
2012-07-26 00:08 2586import android.content.Context ... -
android百度地图半径画圆
2012-07-26 00:07 2718Java代码 import android.content ... -
Android下获取开机时间
2012-07-26 00:05 1301我的思路是:程序里注册个广播接收器,接收开机启动的广播,当程序 ... -
android 高仿【优酷】圆盘旋转菜单 的实现
2012-07-26 00:03 1341MyAnimation.java Java代码 pack ... -
android 3D 转盘效果(附源码)
2012-07-25 23:41 1782一个仿3D的转盘效果,有倒影特效,旋转图标还可自动放大缩小。由 ... -
Android Thread
2012-07-23 10:47 1015创建新线程的常用方式: 1. 直接使用Thread创建 ... -
Android 通过手说tts中文语音包实现中文朗读
2012-07-22 17:09 1782Android 通过手说tts中文语音包实现中文朗读 ... -
Android 使用HTTPClient调用Web请求(查询手机号码区域)
2012-07-21 00:33 1205Android通过Apache HttpClient调用网上提 ... -
Android+struts2+JSON方式的手机开发
2012-07-21 00:14 1141http://topmanopensource.iteye.c ... -
android九宫格实现
2012-07-21 00:03 946android九宫格实现,开始以为很复杂,其实只要知道了如何布 ... -
Android ListView圆角实现
2012-07-20 23:59 1183在android上开发项目,如 ... -
Android 将一个Activity转化为View显示出来
2012-07-19 10:27 2067最近看到好多opengl牛人写了些立方体,卷页之类的华丽的代码 ...
相关推荐
Android AIDL Binder 实现与详解。此资源实现了 Android AIDL 通信,自定义 AIDL 数据类型。同时演示了定向 Tag 『inout in out』的区别。并且配有博文详细解释相关知识点以及需要注意的细节。
android跨进程通信之aidl详解1
里面介绍有例子用于说明aidl的作用 使菜鸟们可以更清楚的认识到这层的意义
andorid跨进程通信之aidl详解demo
摘要:本节主要来讲解Android10.0 Binder中如何使用AIDL 阅读本文大约需要花费20分钟。 文章首发微信公众号:IngresGe 专注于Android系统级源码分析,Android的平台设计,欢迎关注我,谢谢! [Android取经之路] 的...
本篇文章主要介绍了Android 进程间通信AIDL使用详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
AIDL是Android中IPC(Inter-Process Communication)方式中的一种,AIDL是Android Interface definition language的缩写(对于小白来说,AIDL的作用是让你可以在自己的APP里绑定一个其他APP的service,这样你的APP...
Android开发中实现跨进程通讯的AIDL接口的相关文章推荐 - Android... Android开发中实现跨进程通讯的AIDL接口的相关文章:在Android开发中, 每个应用程序都可以有自己的进程. 在写UI应用的时候, 经常要用到Service. ...
主要给大家介绍了关于Android AIDL中Map参数传递问题的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友下面来一起看看吧。
Android AIDL 远程服务实现跨进程通信,详解请参考:https://blog.csdn.net/Ikulm/article/details/131169568
AIDL(AndRoid接口描述语言)是一种借口描述语言; 编译器可以通过aidl文件生成一段代码,通过预先定义的接口达到两个进程内部通信进程的目的. 如果需要在一个Activity中, 访问另一个Service中的某个对象, 需要先将对象...
本篇文章主要介绍了Android进程通信之Messenger和AIDL使用详解,具有一定的参考价值,有兴趣的可以了解一下。
本篇文章主要介绍了详解Android跨进程IPC通信AIDL机制原理,详细的介绍了AIDL的概念和使用,具有一定的参考价值,有兴趣的可以了解一下