鸿蒙开源三方组件 -- 进程间通信的AndLinker组件

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

[[415055]]

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

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.    // your app's other dependencies 
  3.     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,并实现它:

  1. @RemoteInterface 
  2. public interface IRemoteService { 
  3.  
  4.     int getPid(); 
  5.  
  6.     void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, 
  7.                     double aDouble, String aString); 
  8.  
  9.     void registerCallback(@Callback IRemoteCallback callback); 
  10.  
  11.     void directionalParamMethod(@In int[] arr, @Out ParcelableObj obj, @Inout Rect rect); 
  12.  
  13.     @OneWay 
  14.     void onewayMethod(String msg); 

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

  1. private AndLinkerBinder mLinkerBinder; 
  2.   @Override 
  3.   public void onStart(Intent intent) { 
  4.       super.onStart(intent); 
  5.       HiLog.debug(label, "Service onCreate()"); 
  6.       mLinkerBinder = AndLinkerBinder.Factory.newBinder(); 
  7.       mLinkerBinder.registerObject(mRemoteService); 
  8.       mLinkerBinder.registerObject(mRemoteTask); 
  9.   } 
  10.  
  11.   @Override 
  12.   public IRemoteObject onConnect(Intent intent) { 
  13.       HiLog.debug(label, "Service onBind()"); 
  14.       return (IRemoteObject) mLinkerBinder; 
  15.   } 

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

  1. public class MainAbilitySlice extends AbilitySlice implements AndLinker.BindCallback,Component.ClickedListener{ 
  2.     private static final String TAG = "BindingActivity"
  3.     private static final String REMOTE_SERVICE_PKG = "com.example.andlinker"
  4.     public static final String REMOTE_SERVICE_ACTION = "com.example.andlinker.REMOTE_SERVICE_ACTION"
  5.     private HiLogLabel label = new HiLogLabel(HiLog.ERROR,0,TAG); 
  6.     private IRemoteTask mRemoteTask; 
  7.     private IRemoteService mRemoteService; 
  8.     private AndLinker mLinker; 
  9.  
  10.     @Override 
  11.     public void onStart(Intent intent) { 
  12.         super.onStart(intent); 
  13.         super.setUIContent(ResourceTable.Layout_ability_main); 
  14.         mLinker = new AndLinker.Builder(this) 
  15.                 .packageName(REMOTE_SERVICE_PKG) 
  16.                 .action(REMOTE_SERVICE_ACTION) 
  17.                 .className("com.example.andlinker.RemoteService"
  18.                 .addCallAdapterFactory(OriginalCallAdapterFactory.create()) // Basic 
  19.                 .addCallAdapterFactory(RxJava2CallAdapterFactory.create())  // RxJava2 
  20.                 .build(); 
  21.         mLinker.setBindCallback(this); 
  22.         mLinker.registerObject(mRemoteCallback); 
  23.         mLinker.bind(); 

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

基本使用-效果展示

AndLinker支持AIDL所有数据类型:

  • Java语言中的所有原始类型:int,long,char,boolean等等。
  • String
  • CharSequence
  • Paracelable
  • List(List中的所有元素必须是此列表中支持的数据类型)
  • Map(Map中的所有元素必须是此列表中支持的数据类型)
  1. Button buttonBtnPid = (Button) findComponentById(ResourceTable.Id_btn_pid); 
  2.        buttonBtnPid.setClickedListener(new Component.ClickedListener() { 
  3.            @Override 
  4.            public void onClick(Component component) { 
  5.                ToastDialog dialog = new ToastDialog(MainAbilitySlice.this); 
  6.                dialog.setText("Server pid: " + mRemoteService.getPid()).show(); 
  7.            } 
  8.        }); 
  9.  
  10. Button buttonBtnBasicTypes = (Button) findComponentById(ResourceTable.Id_btn_basic_types); 
  11.        buttonBtnBasicTypes.setClickedListener(new Component.ClickedListener() { 
  12.            @Override 
  13.            public void onClick(Component component) { 
  14.                mRemoteService.basicTypes(1, 2L, true, 3.0f, 4.0d, "str"); 
  15.            } 
  16.        }); 
【全网首发】鸿蒙开源三方组件 -- 进程间通信的AndLinker组件-鸿蒙HarmonyOS技术社区
【全网首发】鸿蒙开源三方组件 -- 进程间通信的AndLinker组件-鸿蒙HarmonyOS技术社区

进阶使用-效果展示

1.Call Adapters

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

  1. @RemoteInterface 
  2. public interface IRemoteService { 
  3.  
  4.     int getPid(); 
  5.  
  6.     void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, 
  7.                     double aDouble, String aString); 
  8.  
  9.     void registerCallback(@Callback IRemoteCallback callback); 
  10.  
  11.     void directionalParamMethod(@In int[] arr, @Out ParcelableObj obj, @Inout Rect rect); 
  12.  
  13.     @OneWay 
  14.     void onewayMethod(String msg); 

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

  1. public class MainAbilitySlice extends AbilitySlice implements AndLinker.BindCallback,Component.ClickedListener{ 
  2.     private static final String TAG = "BindingActivity"
  3.     private static final String REMOTE_SERVICE_PKG = "com.example.andlinker"
  4.     public static final String REMOTE_SERVICE_ACTION = "com.example.andlinker.REMOTE_SERVICE_ACTION"
  5.     private HiLogLabel label = new HiLogLabel(HiLog.ERROR,0,TAG); 
  6.     private IRemoteTask mRemoteTask; 
  7.     private IRemoteService mRemoteService; 
  8.     private AndLinker mLinker; 
  9.  
  10.     @Override 
  11.     public void onStart(Intent intent) { 
  12.         super.onStart(intent); 
  13.         super.setUIContent(ResourceTable.Layout_ability_main); 
  14.         mLinker = new AndLinker.Builder(this) 
  15.                 .packageName(REMOTE_SERVICE_PKG) 
  16.                 .action(REMOTE_SERVICE_ACTION) 
  17.                 .className("com.example.andlinker.RemoteService"
  18.                 .addCallAdapterFactory(OriginalCallAdapterFactory.create()) // Basic 
  19.                 .addCallAdapterFactory(RxJava2CallAdapterFactory.create())  // RxJava2 
  20.                 .build(); 
  21.         mLinker.setBindCallback(this); 
  22.         mLinker.registerObject(mRemoteCallback); 
  23.         mLinker.bind(); 
【全网首发】鸿蒙开源三方组件 -- 进程间通信的AndLinker组件-鸿蒙HarmonyOS技术社区
【全网首发】鸿蒙开源三方组件 -- 进程间通信的AndLinker组件-鸿蒙HarmonyOS技术社区

2.远程服务接口回调

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

  1. @RemoteInterface 
  2. public interface IRemoteCallback { 
  3.  
  4.     void onStart(); 
  5.  
  6.     void onValueChange(int value); 

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

  1. void registerCallback(@Callback IRemoteCallback callback); 

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

  1. private final IRemoteCallback mRemoteCallback = new IRemoteCallback() { 
  2.  
  3.         @Override 
  4.         public void onStart() { 
  5.             HiLog.debug(label,"Server callback onStart!"); 
  6.         } 
  7.  
  8.         @Override 
  9.         public void onValueChange(int value) { 
  10.             // Invoke when server side callback 
  11.             ToastDialog dialog = new ToastDialog(MainAbilitySlice.this); 
  12.             dialog.setSize(1000,200); 
  13.             dialog.setText( "Server callback value: " + value).show(); 
  14.         } 
  15.     }; 
【全网首发】鸿蒙开源三方组件 -- 进程间通信的AndLinker组件-鸿蒙HarmonyOS技术社区

3.指定数据定向tag

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

  1. void directionalParamMethod(@In int[] arr, @Out ParcelableObj obj, @Inout Rect rect); 

注意:

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

4.使用@OnewWay注解

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

  1. @OneWay 
  2. void onewayMethod(String msg); 
【全网首发】鸿蒙开源三方组件 -- 进程间通信的AndLinker组件-鸿蒙HarmonyOS技术社区

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

 

责任编辑:jianghua 来源: 鸿蒙社区
相关推荐

2021-08-02 14:54:50

鸿蒙HarmonyOS应用

2021-04-28 15:07:06

鸿蒙HarmonyOS应用

2021-08-09 10:24:49

鸿蒙HarmonyOS应用

2021-03-24 09:30:49

鸿蒙HarmonyOS应用

2021-04-29 14:32:24

鸿蒙HarmonyOS应用

2021-03-10 15:03:40

鸿蒙HarmonyOS应用

2021-03-03 09:42:26

鸿蒙HarmonyOS图片裁剪

2021-04-28 09:56:44

鸿蒙HarmonyOS应用

2021-04-20 15:06:42

鸿蒙HarmonyOS应用

2021-07-06 18:21:31

鸿蒙HarmonyOS应用

2021-04-08 14:57:52

鸿蒙HarmonyOS应用

2021-08-05 15:06:30

鸿蒙HarmonyOS应用

2021-08-30 17:55:58

鸿蒙HarmonyOS应用

2021-03-01 14:00:11

鸿蒙HarmonyOS应用

2021-07-20 15:20:40

鸿蒙HarmonyOS应用

2021-04-15 17:47:38

鸿蒙HarmonyOS应用

2021-08-03 10:07:41

鸿蒙HarmonyOS应用

2021-07-30 14:54:54

鸿蒙HarmonyOS应用

2021-08-26 16:07:46

鸿蒙HarmonyOS应用

2021-03-12 16:35:33

鸿蒙HarmonyOS应用
点赞
收藏

51CTO技术栈公众号