AndLinker是一款OpenHarmony上的IPC (进程间通信) 库,结合了[ZIDL][zidl]和[Retrofit][retrofit]的诸多特性,且可以与[RxJava][rxjava]和[RxJava2][rxjava2]的Call Adapters无缝结合使用。

 鸿蒙开源三方组件 -- 进程间通信的AndLinker组件 鸿蒙 HarmonyOS 应用 第1张

想了解更多内容,请访问:

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部分:

  1. dependencies{
  2. //yourapp'sotherdependencies
  3. implementation'io.github.dzsf:andlinker:1.0.0'
  4. }

3.功能特性

  • 以普通Java接口代替AIDL接口。
  • 像Retrofit一样生成远程服务接口的IPC实现。
  • 支持Call Adapters:Call, RxJava Observable,RxJava2 Observable & Flowable。
  • 支持远程服务回调机制。
  • 支持AIDL的所有数据类型。
  • 支持AIDL的所有数据定向tag: in,out,inout。
  • 支持AIDL的oneway关键字。

4.如何使用

使用注解@RemoteInterface修饰远程服务接口IRemoteService,并实现它:

  1. @RemoteInterface
  2. publicinterfaceIRemoteService{
  3. intgetPid();
  4. voidbasicTypes(intanInt,longaLong,booleanaBoolean,floataFloat,
  5. doubleaDouble,StringaString);
  6. voidregisterCallback(@CallbackIRemoteCallbackcallback);
  7. voiddirectionalParamMethod(@Inint[]arr,@OutParcelableObjobj,@InoutRectrect);
  8. @OneWay
  9. voidonewayMethod(Stringmsg);
  10. }

在服务端App中,创建AndLinkerBinder对象,并注册上面的接口实现。然后在onBind()方法中返回,暴露给客户端:

  1. privateAndLinkerBindermLinkerBinder;
  2. @Override
  3. publicvoidonStart(Intentintent){
  4. super.onStart(intent);
  5. HiLog.debug(label,"ServiceonCreate()");
  6. mLinkerBinder=AndLinkerBinder.Factory.newBinder();
  7. mLinkerBinder.registerObject(mRemoteService);
  8. mLinkerBinder.registerObject(mRemoteTask);
  9. }
  10. @Override
  11. publicIRemoteObjectonConnect(Intentintent){
  12. HiLog.debug(label,"ServiceonBind()");
  13. return(IRemoteObject)mLinkerBinder;
  14. }

在客户端App中,通过Builder创建AndLinker对象,并通过create()方法生成一个IRemoteService远程接口的IPC实现:

  1. publicclassMainAbilitySliceextendsAbilitySliceimplementsAndLinker.BindCallback,Component.ClickedListener{
  2. privatestaticfinalStringTAG="BindingActivity";
  3. privatestaticfinalStringREMOTE_SERVICE_PKG="com.example.andlinker";
  4. publicstaticfinalStringREMOTE_SERVICE_ACTION="com.example.andlinker.REMOTE_SERVICE_ACTION";
  5. privateHiLogLabellabel=newHiLogLabel(HiLog.ERROR,0,TAG);
  6. privateIRemoteTaskmRemoteTask;
  7. privateIRemoteServicemRemoteService;
  8. privateAndLinkermLinker;
  9. @Override
  10. publicvoidonStart(Intentintent){
  11. super.onStart(intent);
  12. super.setUIContent(ResourceTable.Layout_ability_main);
  13. mLinker=newAndLinker.Builder(this)
  14. .packageName(REMOTE_SERVICE_PKG)
  15. .action(REMOTE_SERVICE_ACTION)
  16. .className("com.example.andlinker.RemoteService")
  17. .addCallAdapterFactory(OriginalCallAdapterFactory.create())//Basic
  18. .addCallAdapterFactory(RxJava2CallAdapterFactory.create())//RxJava2
  19. .build();
  20. mLinker.setBindCallback(this);
  21. mLinker.registerObject(mRemoteCallback);
  22. mLinker.bind();
  23. }

现在mRemoteService对象中的所有方法都是IPC方法。

基本使用-效果展示

AndLinker支持AIDL所有数据类型:

  • Java语言中的所有原始类型:int,long,char,boolean等等。
  • String
  • CharSequence
  • Paracelable
  • List(List中的所有元素必须是此列表中支持的数据类型)
  • Map(Map中的所有元素必须是此列表中支持的数据类型)
  1. ButtonbuttonBtnPid=(Button)findComponentById(ResourceTable.Id_btn_pid);
  2. buttonBtnPid.setClickedListener(newComponent.ClickedListener(){
  3. @Override
  4. publicvoidonClick(Componentcomponent){
  5. ToastDialogdialog=newToastDialog(MainAbilitySlice.this);
  6. dialog.setText("Serverpid:"+mRemoteService.getPid()).show();
  7. }
  8. });
  9. ButtonbuttonBtnBasicTypes=(Button)findComponentById(ResourceTable.Id_btn_basic_types);
  10. buttonBtnBasicTypes.setClickedListener(newComponent.ClickedListener(){
  11. @Override
  12. publicvoidonClick(Componentcomponent){
  13. mRemoteService.basicTypes(1,2L,true,3.0f,4.0d,"str");
  14. }
  15. });
鸿蒙开源三方组件 -- 进程间通信的AndLinker组件  鸿蒙 HarmonyOS 应用 第2张
鸿蒙开源三方组件 -- 进程间通信的AndLinker组件  鸿蒙 HarmonyOS 应用 第3张

进阶使用-效果展示

1.Call Adapters

在客户端App中,你可以copy并修改远程服务接口,包装方法的返回值

  1. @RemoteInterface
  2. publicinterfaceIRemoteService{
  3. intgetPid();
  4. voidbasicTypes(intanInt,longaLong,booleanaBoolean,floataFloat,
  5. doubleaDouble,StringaString);
  6. voidregisterCallback(@CallbackIRemoteCallbackcallback);
  7. voiddirectionalParamMethod(@Inint[]arr,@OutParcelableObjobj,@InoutRectrect);
  8. @OneWay
  9. voidonewayMethod(Stringmsg);
  10. }

在AndLinker.Builder中注册对应的Call Adapter Factory

  1. publicclassMainAbilitySliceextendsAbilitySliceimplementsAndLinker.BindCallback,Component.ClickedListener{
  2. privatestaticfinalStringTAG="BindingActivity";
  3. privatestaticfinalStringREMOTE_SERVICE_PKG="com.example.andlinker";
  4. publicstaticfinalStringREMOTE_SERVICE_ACTION="com.example.andlinker.REMOTE_SERVICE_ACTION";
  5. privateHiLogLabellabel=newHiLogLabel(HiLog.ERROR,0,TAG);
  6. privateIRemoteTaskmRemoteTask;
  7. privateIRemoteServicemRemoteService;
  8. privateAndLinkermLinker;
  9. @Override
  10. publicvoidonStart(Intentintent){
  11. super.onStart(intent);
  12. super.setUIContent(ResourceTable.Layout_ability_main);
  13. mLinker=newAndLinker.Builder(this)
  14. .packageName(REMOTE_SERVICE_PKG)
  15. .action(REMOTE_SERVICE_ACTION)
  16. .className("com.example.andlinker.RemoteService")
  17. .addCallAdapterFactory(OriginalCallAdapterFactory.create())//Basic
  18. .addCallAdapterFactory(RxJava2CallAdapterFactory.create())//RxJava2
  19. .build();
  20. mLinker.setBindCallback(this);
  21. mLinker.registerObject(mRemoteCallback);
  22. mLinker.bind();
  23. }
鸿蒙开源三方组件 -- 进程间通信的AndLinker组件  鸿蒙 HarmonyOS 应用 第4张
鸿蒙开源三方组件 -- 进程间通信的AndLinker组件  鸿蒙 HarmonyOS 应用 第5张

2.远程服务接口回调

使用@RemoteInterface注解修饰远程服务回调接口IRemoteCallback

  1. @RemoteInterface
  2. publicinterfaceIRemoteCallback{
  3. voidonStart();
  4. voidonValueChange(intvalue);
  5. }

在远程方法中使用@Callback注解修饰callback参数

  1. voidregisterCallback(@CallbackIRemoteCallbackcallback);

在客户端App中,实现上面定义的远程服务回调接口IRemoteCallback,并且注册到AndLinker中

  1. privatefinalIRemoteCallbackmRemoteCallback=newIRemoteCallback(){
  2. @Override
  3. publicvoidonStart(){
  4. HiLog.debug(label,"ServercallbackonStart!");
  5. }
  6. @Override
  7. publicvoidonValueChange(intvalue){
  8. //Invokewhenserversidecallback
  9. ToastDialogdialog=newToastDialog(MainAbilitySlice.this);
  10. dialog.setSize(1000,200);
  11. dialog.setText("Servercallbackvalue:"+value).show();
  12. }
  13. };
鸿蒙开源三方组件 -- 进程间通信的AndLinker组件  鸿蒙 HarmonyOS 应用 第6张

3.指定数据定向tag

可以为远程方法的参数指定@In,@Out或者@Inout注解,标记了数据在底层Builder中的流向

  1. @RemoteInterface
  2. publicinterfaceIRemoteService{
  3. intgetPid();
  4. voidbasicTypes(intanInt,longaLong,booleanaBoolean,floataFloat,
  5. doubleaDouble,StringaString);
  6. voidregisterCallback(@CallbackIRemoteCallbackcallback);
  7. voiddirectionalParamMethod(@Inint[]arr,@OutParcelableObjobj,@InoutRectrect);
  8. @OneWay
  9. voidonewayMethod(Stringmsg);
  10. }
0

注意:

  • 所有非原始类型必须指定数据定向tag:@In,@Out,或者@Inout,用来标记数据的流向。原始类型默认是@In类型,并且不能指定其他值。
  • 使用@Out或者@Inout修饰的Parcelable参数必须实现SuperParcelable接口,否则你必须手动添加此方法public void readFromParcel(Parcel in)。
鸿蒙开源三方组件 -- 进程间通信的AndLinker组件  鸿蒙 HarmonyOS 应用 第7张

4.使用@OnewWay注解

在远程方法上使用@OneWay注解,这会修改远程方法调用的行为。当使用它时,远程方法调用不会堵塞,它只是简单的发送数据并立即返回。

  1. @RemoteInterface
  2. publicinterfaceIRemoteService{
  3. intgetPid();
  4. voidbasicTypes(intanInt,longaLong,booleanaBoolean,floataFloat,
  5. doubleaDouble,StringaString);
  6. voidregisterCallback(@CallbackIRemoteCallbackcallback);
  7. voiddirectionalParamMethod(@Inint[]arr,@OutParcelableObjobj,@InoutRectrect);
  8. @OneWay
  9. voidonewayMethod(Stringmsg);
  10. }
1
鸿蒙开源三方组件 -- 进程间通信的AndLinker组件  鸿蒙 HarmonyOS 应用 第8张

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组件 鸿蒙 HarmonyOS 应用 第9张

转载请说明出处
知优网 » 鸿蒙开源三方组件 -- 进程间通信的AndLinker组件

发表评论

您需要后才能发表评论