使用AIDLAndroid接口描述语言设计和使用远程接口.docx

上传人:b****7 文档编号:10576752 上传时间:2023-02-21 格式:DOCX 页数:21 大小:26.15KB
下载 相关 举报
使用AIDLAndroid接口描述语言设计和使用远程接口.docx_第1页
第1页 / 共21页
使用AIDLAndroid接口描述语言设计和使用远程接口.docx_第2页
第2页 / 共21页
使用AIDLAndroid接口描述语言设计和使用远程接口.docx_第3页
第3页 / 共21页
使用AIDLAndroid接口描述语言设计和使用远程接口.docx_第4页
第4页 / 共21页
使用AIDLAndroid接口描述语言设计和使用远程接口.docx_第5页
第5页 / 共21页
点击查看更多>>
下载资源
资源描述

使用AIDLAndroid接口描述语言设计和使用远程接口.docx

《使用AIDLAndroid接口描述语言设计和使用远程接口.docx》由会员分享,可在线阅读,更多相关《使用AIDLAndroid接口描述语言设计和使用远程接口.docx(21页珍藏版)》请在冰豆网上搜索。

使用AIDLAndroid接口描述语言设计和使用远程接口.docx

使用AIDLAndroid接口描述语言设计和使用远程接口

使用AIDL(Android接口描述语言)设计和使用远程接口

文章分类:

移动开发关键字:

aidl、android目录

1使用AIDL(AndRoid接口描述语言)设计和使用远程接口

    1.1使用AIDL实现IPC

       1.1.1创建一个AIDL文件

       1.1.2实现接口

       1.1.3向客户端公开接口

       1.1.4使用parcelables进行参数的值传递

       1.2调用一个IPC方法

使用AIDL(AndRoid接口描述语言)设计和使用远程接口

Sinceeachapplicationrunsinitsownprocess,andyoucanwriteaservicethatrunsinadifferentprocessfromyourApplication'sUI,sometimesyouneedtopassobjectsbetweenprocesses.OntheAndroidplatform,oneprocesscannotnormallyaccessthememoryofanotherprocess.Sototalk,theyneedtodecomposetheirobjectsintoprimitivesthattheoperatingsystemcanunderstand,and"marshall"theobjectacrossthatboundaryforyou.

通常每个应用程序都在它自己的进程内运行,但有时需要在进程间传递对象,你可以通过应用程序UI的方式写个运行在一个不同的进程中的service。

在AndRoid平台中,一个进程通常不能访问其他进程中的内存区域。

所以,他们需要把对象拆分成操作系统能理解的简单形式,以便伪装成对象跨越边界访问。

Thecodetodothatmarshallingistedioustowrite,soweprovidetheAIDLtooltodoitforyou.

编写这种伪装代码相当的枯燥乏味,好在我们提供了AIDL工具可以来做这件事。

AIDL(AndroidInterfaceDefinitionLanguage)isanIDLlanguageusedtogeneratecodethatenablestwoprocessesonanAndroiddevicetotalkusinginterprocesscommunication(IPC).Ifyouhavecodeinoneprocess(forexample,inanActivity)thatneedstocallmethodsonanobjectinanotherprocess(forexample,aService),youwoulduseAIDLtogeneratecodetomarshalltheparameters.

AIDL(AndRoid接口描述语言)是一个IDL语言,它可以生成一段代码,可以使在一个AndRoid设备上运行的两个进程使用内部通信进程进行交互。

如果你需要在一个进程中(例如:

在一个Activity中)访问另一个进程中(例如:

一个Service)某个对象的方法,你就可以使用AIDL来生成这样的代码来伪装传递各种参数。

TheAIDLIPCmechanismisinterface-based,similartoCOMorCorba,butlighterweight.Itusesaproxyclasstopassvaluesbetweentheclientandtheimplementation.

AIDLIPC的机制是基于接口的,和COM或Corba类似,但它是轻量级的。

它使用代理类在客户端和实现层间传递值。

Thispageincludesthefollowingmaintopics:

本页包含以下主题:

ImplementingIPCUsingAIDL

Callingan.aidl(IPC)Class

使用AIDL实现IPC

调用一个AIDL(IPC)类

使用AIDL实现IPC

FollowthesestepstoimplementanIPCserviceusingAIDL.

使用AIDL实现一个IPC有下列步骤:

1.Createyour.aidlfile-Thisfiledefinesaninterface(YourInterface.aidl)thatdefinesthemethodsandfieldsavailabletoaclient.

1、创建你的AIDL文件-这个文件定义一个接口(YourInterface.aidl),该接口定义了可供客户端访问的方法和属性。

2.Addthe.aidlfiletoyourmakefile-(theEclipsepluginmanagesthisforyou).Androidincludesthecompiler,calledAIDL,inthetools/directory.

2、添加AIDL文件到你的makefile中-(Eclipseplugin可以帮你管理)。

AndRoid包括编译器,AIDL调用,这些都能在tools/directory中找到。

3.Implementyourinterfacemethods-TheAIDLcompilercreatesaninterfaceintheJavaprogramminglanguagefromyourAIDLinterface.ThisinterfacehasaninnerabstractclassnamedStubthatinheritstheinterface(andimplementsafewadditionalmethodsnecessaryfortheIPCcall).YoumustcreateaclassthatextendsYourInterface.Stubandimplementsthemethodsyoudeclaredinyour.aidlfile.

3、实现接口方法-AIDL编译器从你的AIDL接口中使用JAVA编程语言来创建一个接口。

这个接口有一个名为Stub的内部抽象类,它继承接口(并实现供IPC调用的所必需的几个附加方法)。

你必须创建一个类来实现该接口。

4.Exposeyourinterfacetoclients-Ifyou'rewritingaservice,youshouldextendServiceandoverridegetBinder()toreturninganinstanceofyourclassthatimplementsyourinterface.

4、向客户端开放接口-如果你写个service,你应该扩展该Service并重载getBinder()方法来返回一个实现上述接口的类的实例。

[编辑]创建一个AIDL文件

AIDLisasimplesyntaxthatletsyoudeclareaninterfacewithoneormoremethods,thatcantakeparametersandreturnvalues.Theseparametersandreturnvaluescanbeofanytype,evenotherAIDL-generatedinterfaces.However,itisimportanttonotethatyoumustimportallnon-built-intypes,eveniftheyaredefinedinthesamepackageasyourinterface.HerearethedatatypesthatAIDLcansupport:

AIDL语法简单,你可以用来声明一个带一个或多个方法的接口,也可以传递参数和返回值。

这些参数和返回值可以是任何类型,甚至是其他的AIDL生成的接口。

然而,值得重视的是你必须导入所有的non-bult-in类型,即使他们已经作为接口在其他包里定义了。

下面是些AIDL支持的数据类型:

PrimitiveJavaprogramminglanguagetypes(int,boolean,etc)—Noimportstatementisneeded.

简单Java编程语言类型(int,boolean等)-不需要import声明。

Oneofthefollowingclasses(noimportstatementsneeded):

下面类之一(不需要import声明)

Java代码

  1..String  

  2. .List-AllelementsintheListmustbeoneofthetypesinthislist,includingotherAIDL-generatedinterfaces  

  3.  andparcelables.Listmayoptionallybeusedasa"generic"class(e.g.List).Theactualconcreteclass  

  4.  thattheothersidewillreceivewillalwaysbeanArrayList,althoughthemethodwillbegeneratedtousethe  

  5.  Listinterface.  

  6. .List-List中的所有元素都必须是可支持的类型中的一个,包括其他AIDL生成接口和parcelables。

List可以作为泛型类来灵活使用(比如  

  7.   List)。

而实际的接受方的类则总是ArrayList,尽管该方法将被生成来使用List接口。

 

  8. .Map-AllelementsintheMapmustbeofoneofthetypesinthislist,includingotherAIDL-generatedinterfaces 

  9.  andparcelables.Genericmaps,(e.g.oftheformMaparenotsupported.Theactualconcreteclass  

 10.  thattheothersidewillreceivewillalwaysbeaHashMap,althoughthemethodwillbegeneratedtousetheMapinterface.  

 11. .Map-Map中的所有元素都必须是可支持的类型中的一个,包括其他AIDL生成接口和parcelables。

泛型化的Maps(比如:

Map)不被支持。

 

 12.  而实际的接受方的类则总是HashMap,尽管该方法将被生成去使用Map接口。

 

 13. .CharSequence-ThisisusefulfortheCharSequencetypesusedbyTextViewandotherwidgetobjects.  

 14. .CharSequence-CharSequence的作用是可以被TextView和其他Widget对象使用。

 

.String

 .List-AllelementsintheListmustbeoneofthetypesinthislist,includingotherAIDL-generatedinterfaces

 andparcelables.Listmayoptionallybeusedasa"generic"class(e.g.List).Theactualconcreteclass

 thattheothersidewillreceivewillalwaysbeanArrayList,althoughthemethodwillbegeneratedtousethe

 Listinterface.

 .List-List中的所有元素都必须是可支持的类型中的一个,包括其他AIDL生成接口和parcelables。

List可以作为泛型类来灵活使用(比如

  List)。

而实际的接受方的类则总是ArrayList,尽管该方法将被生成来使用List接口。

 .Map-AllelementsintheMapmustbeofoneofthetypesinthislist,includingotherAIDL-generatedinterfaces

 andparcelables.Genericmaps,(e.g.oftheformMaparenotsupported.Theactualconcreteclass

 thattheothersidewillreceivewillalwaysbeaHashMap,althoughthemethodwillbegeneratedtousetheMapinterface.

 .Map-Map中的所有元素都必须是可支持的类型中的一个,包括其他AIDL生成接口和parcelables。

泛型化的Maps(比如:

Map)不被支持。

 而实际的接受方的类则总是HashMap,尽管该方法将被生成去使用Map接口。

 .CharSequence-ThisisusefulfortheCharSequencetypesusedbyTextViewandotherwidgetobjects.

 .CharSequence-CharSequence的作用是可以被TextView和其他Widget对象使用。

OtherAIDL-generatedinterfaces,whicharealwayspassedbyreference.Animportstatementisalwaysneededforthese.CustomclassesthatimplementtheParcelableprotocolandarepassedbyvalue.Animportstatementisalwaysneededforthese.

其他的AIDL生成接口通过引用方式进行传递。

所以import声明是必须的。

封装协议实现的自定义的类是值传递的方式。

所以import声明也是必须的。

HereisthebasicAIDLsyntax:

下面是基本的AIDL语法:

Java代码

  1.//MyAIDLfile,namedSomeClass.aidl 

  2.//Notethatstandardcommentsyntaxisrespected. 

  3.//Commentsbeforetheimportorpackagestatementsarenotbubbledup 

  4.//tothegeneratedinterface,butcommentsaboveinterface/method/field 

  5.//declarationsareaddedtothegeneratedinterface. 

  6.//Includeyourfully-qualifiedpackagestatement. 

  7.packagecom.google.android.sample; 

  8.//Seethelistaboveforwhichclassesneed 

  9.//importstatements(hint--mostofthem) 

 10.importcom.google.android.sample.IAtmService; 

 11.//Declaretheinterface. 

 12.interfaceIBankAccountService{ 

 13.  //Methodscantake0ormoreparameters,and 

 14.  //returnavalueorvoid. 

 15.  intgetAccountBalance(); 

 16.  voidsetOwnerNames(inListnames); 

 17.  //MethodscaneventakeotherAIDL-definedparameters. 

 18.  BankAccountcreateAccount(inStringname,intstartingDeposit,inIAtmServiceatmService); 

 19.  //Allnon-Javaprimitiveparameters(e.g.,int,bool,etc)require 

 20.  //adirectionaltagindicatingwhichwaythedatawillgo.Available 

 21.  //valuesarein,out,inout.(Primitivesareinbydefault,andcannotbeotherwise). 

 22.  //Limitthedirectiontowhatistrulyneeded,becausemarshallingparameters 

 23.  //isexpensive. 

 24.  intgetCustomerList(inStringbranch,outString[]customerList); 

 25.} 

 //MyAIDLfile,namedSomeClass.aidl

 //Notethatstandardcommentsyntaxisrespected.

 //Commentsbeforetheimportorpackagestatementsarenotbubbledup

 //tothegeneratedinterface,butcommentsaboveinterface/method/field

 //declarationsareaddedtothegeneratedinterface.

 //Includeyourfully-qualifiedpackagestatement.

 packagecom.google.android.sample;

 //Seethelistaboveforwhichclassesneed

 //importstatements(hint--mostofthem)

 importcom.google.android.sample.IAtmService;

 //Declaretheinterface.

 interfaceIBankAccountService{

  //Methodscantake0ormoreparameters,and

  //returnavalueorvoid.

  intgetAccountBalance();

  voidsetOwnerNames(inListnames);

  //MethodscaneventakeotherAIDL-definedparameters.

  BankAccountcreateAccount(inStringname,intstartingDeposit,inIAtmServiceatmService);

  //Allnon-Javaprimitiveparameters(e.g.,int,bool,etc)require

  //adirectionaltagindicatingwhichwaythedatawillgo.Available

  //valuesarein,out,inout.(Primitivesareinbydefault,andcannotbeotherwise).

  //Limitthedirectiontowhatistrulyneeded,becausemarshallingparameters

  //isexpensive.

  intgetCustomerList(inStringbranch,outString[]customerList);

 }

实现接口

AIDLgeneratesaninterfacefileforyouwiththesamenameasyour.aidlfile.IfyouareusingtheEclipseplugin,AIDLwillaut

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 哲学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1