AndLinker是一款OpenHarmony上的IPC (进程间通信) 库,结合了[ZIDL][zidl]和[Retrofit][retrofit]的诸多特性,且可以与[RxJava][rxjava]和[RxJava2][rxjava2]的Call Adapters无缝结合使用。
想了解更多内容,请访问:
51CTO和华为官方合作共建的鸿蒙技术社区
https://HarmonyOS.51cto.com
1.组件
AndLinker是一款OpenHarmony上的IPC (进程间通信) 库,结合了[ZIDL][zidl]和[Retrofit][retrofit]的诸多特性,且可以与[RxJava][rxjava]和[RxJava2][rxjava2]的Call Adapters无缝结合使用。项目的设计与部分代码参考了伟大的[Retrofit][retrofit]项目。
2.更新Gradle配置
编辑您的build.gradle文件。您必须将以下行添加到该dependencies部分:
- dependencies{
- //yourapp'sotherdependencies
- implementation'io.github.dzsf:andlinker:1.0.0'
- }
3.功能特性
- 以普通Java接口代替AIDL接口。
- 像Retrofit一样生成远程服务接口的IPC实现。
- 支持Call Adapters:Call, RxJava Observable,RxJava2 Observable & Flowable。
- 支持远程服务回调机制。
- 支持AIDL的所有数据类型。
- 支持AIDL的所有数据定向tag: in,out,inout。
- 支持AIDL的oneway关键字。
4.如何使用
使用注解@RemoteInterface修饰远程服务接口IRemoteService,并实现它:
- @RemoteInterface
- publicinterfaceIRemoteService{
- intgetPid();
- voidbasicTypes(intanInt,longaLong,booleanaBoolean,floataFloat,
- doubleaDouble,StringaString);
- voidregisterCallback(@CallbackIRemoteCallbackcallback);
- voiddirectionalParamMethod(@Inint[]arr,@OutParcelableObjobj,@InoutRectrect);
- @OneWay
- voidonewayMethod(Stringmsg);
- }
在服务端App中,创建AndLinkerBinder对象,并注册上面的接口实现。然后在onBind()方法中返回,暴露给客户端:
- privateAndLinkerBindermLinkerBinder;
- @Override
- publicvoidonStart(Intentintent){
- super.onStart(intent);
- HiLog.debug(label,"ServiceonCreate()");
- mLinkerBinder=AndLinkerBinder.Factory.newBinder();
- mLinkerBinder.registerObject(mRemoteService);
- mLinkerBinder.registerObject(mRemoteTask);
- }
- @Override
- publicIRemoteObjectonConnect(Intentintent){
- HiLog.debug(label,"ServiceonBind()");
- return(IRemoteObject)mLinkerBinder;
- }
在客户端App中,通过Builder创建AndLinker对象,并通过create()方法生成一个IRemoteService远程接口的IPC实现:
- publicclassMainAbilitySliceextendsAbilitySliceimplementsAndLinker.BindCallback,Component.ClickedListener{
- privatestaticfinalStringTAG="BindingActivity";
- privatestaticfinalStringREMOTE_SERVICE_PKG="com.example.andlinker";
- publicstaticfinalStringREMOTE_SERVICE_ACTION="com.example.andlinker.REMOTE_SERVICE_ACTION";
- privateHiLogLabellabel=newHiLogLabel(HiLog.ERROR,0,TAG);
- privateIRemoteTaskmRemoteTask;
- privateIRemoteServicemRemoteService;
- privateAndLinkermLinker;
- @Override
- publicvoidonStart(Intentintent){
- super.onStart(intent);
- super.setUIContent(ResourceTable.Layout_ability_main);
- mLinker=newAndLinker.Builder(this)
- .packageName(REMOTE_SERVICE_PKG)
- .action(REMOTE_SERVICE_ACTION)
- .className("com.example.andlinker.RemoteService")
- .addCallAdapterFactory(OriginalCallAdapterFactory.create())//Basic
- .addCallAdapterFactory(RxJava2CallAdapterFactory.create())//RxJava2
- .build();
- mLinker.setBindCallback(this);
- mLinker.registerObject(mRemoteCallback);
- mLinker.bind();
- }
现在mRemoteService对象中的所有方法都是IPC方法。
基本使用-效果展示
AndLinker支持AIDL所有数据类型:
- Java语言中的所有原始类型:int,long,char,boolean等等。
- String
- CharSequence
- Paracelable
- List(List中的所有元素必须是此列表中支持的数据类型)
- Map(Map中的所有元素必须是此列表中支持的数据类型)
- ButtonbuttonBtnPid=(Button)findComponentById(ResourceTable.Id_btn_pid);
- buttonBtnPid.setClickedListener(newComponent.ClickedListener(){
- @Override
- publicvoidonClick(Componentcomponent){
- ToastDialogdialog=newToastDialog(MainAbilitySlice.this);
- dialog.setText("Serverpid:"+mRemoteService.getPid()).show();
- }
- });
- ButtonbuttonBtnBasicTypes=(Button)findComponentById(ResourceTable.Id_btn_basic_types);
- buttonBtnBasicTypes.setClickedListener(newComponent.ClickedListener(){
- @Override
- publicvoidonClick(Componentcomponent){
- mRemoteService.basicTypes(1,2L,true,3.0f,4.0d,"str");
- }
- });
进阶使用-效果展示
1.Call Adapters
在客户端App中,你可以copy并修改远程服务接口,包装方法的返回值
- @RemoteInterface
- publicinterfaceIRemoteService{
- intgetPid();
- voidbasicTypes(intanInt,longaLong,booleanaBoolean,floataFloat,
- doubleaDouble,StringaString);
- voidregisterCallback(@CallbackIRemoteCallbackcallback);
- voiddirectionalParamMethod(@Inint[]arr,@OutParcelableObjobj,@InoutRectrect);
- @OneWay
- voidonewayMethod(Stringmsg);
- }
在AndLinker.Builder中注册对应的Call Adapter Factory
- publicclassMainAbilitySliceextendsAbilitySliceimplementsAndLinker.BindCallback,Component.ClickedListener{
- privatestaticfinalStringTAG="BindingActivity";
- privatestaticfinalStringREMOTE_SERVICE_PKG="com.example.andlinker";
- publicstaticfinalStringREMOTE_SERVICE_ACTION="com.example.andlinker.REMOTE_SERVICE_ACTION";
- privateHiLogLabellabel=newHiLogLabel(HiLog.ERROR,0,TAG);
- privateIRemoteTaskmRemoteTask;
- privateIRemoteServicemRemoteService;
- privateAndLinkermLinker;
- @Override
- publicvoidonStart(Intentintent){
- super.onStart(intent);
- super.setUIContent(ResourceTable.Layout_ability_main);
- mLinker=newAndLinker.Builder(this)
- .packageName(REMOTE_SERVICE_PKG)
- .action(REMOTE_SERVICE_ACTION)
- .className("com.example.andlinker.RemoteService")
- .addCallAdapterFactory(OriginalCallAdapterFactory.create())//Basic
- .addCallAdapterFactory(RxJava2CallAdapterFactory.create())//RxJava2
- .build();
- mLinker.setBindCallback(this);
- mLinker.registerObject(mRemoteCallback);
- mLinker.bind();
- }
2.远程服务接口回调
使用@RemoteInterface注解修饰远程服务回调接口IRemoteCallback
- @RemoteInterface
- publicinterfaceIRemoteCallback{
- voidonStart();
- voidonValueChange(intvalue);
- }
在远程方法中使用@Callback注解修饰callback参数
- voidregisterCallback(@CallbackIRemoteCallbackcallback);
在客户端App中,实现上面定义的远程服务回调接口IRemoteCallback,并且注册到AndLinker中
- privatefinalIRemoteCallbackmRemoteCallback=newIRemoteCallback(){
- @Override
- publicvoidonStart(){
- HiLog.debug(label,"ServercallbackonStart!");
- }
- @Override
- publicvoidonValueChange(intvalue){
- //Invokewhenserversidecallback
- ToastDialogdialog=newToastDialog(MainAbilitySlice.this);
- dialog.setSize(1000,200);
- dialog.setText("Servercallbackvalue:"+value).show();
- }
- };
3.指定数据定向tag
可以为远程方法的参数指定@In,@Out或者@Inout注解,标记了数据在底层Builder中的流向
0
- @RemoteInterface
- publicinterfaceIRemoteService{
- intgetPid();
- voidbasicTypes(intanInt,longaLong,booleanaBoolean,floataFloat,
- doubleaDouble,StringaString);
- voidregisterCallback(@CallbackIRemoteCallbackcallback);
- voiddirectionalParamMethod(@Inint[]arr,@OutParcelableObjobj,@InoutRectrect);
- @OneWay
- voidonewayMethod(Stringmsg);
- }
注意:
- 所有非原始类型必须指定数据定向tag:@In,@Out,或者@Inout,用来标记数据的流向。原始类型默认是@In类型,并且不能指定其他值。
- 使用@Out或者@Inout修饰的Parcelable参数必须实现SuperParcelable接口,否则你必须手动添加此方法public void readFromParcel(Parcel in)。
4.使用@OnewWay注解
在远程方法上使用@OneWay注解,这会修改远程方法调用的行为。当使用它时,远程方法调用不会堵塞,它只是简单的发送数据并立即返回。
1
- @RemoteInterface
- publicinterfaceIRemoteService{
- intgetPid();
- voidbasicTypes(intanInt,longaLong,booleanaBoolean,floataFloat,
- doubleaDouble,StringaString);
- voidregisterCallback(@CallbackIRemoteCallbackcallback);
- voiddirectionalParamMethod(@Inint[]arr,@OutParcelableObjobj,@InoutRectrect);
- @OneWay
- voidonewayMethod(Stringmsg);
- }
5. 下载链接
5.1 IDE下载链接
https://developer.harmonyos.com/cn/develop/deveco-studio#download
5.2 源码链接
https://gitee.com/openneusoft/and-linkers
想了解更多内容,请访问:
51CTO和华为官方合作共建的鸿蒙技术社区
https://harmonyos.51cto.com
知优网 » 鸿蒙开源三方组件 -- 进程间通信的AndLinker组件