成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

鴻蒙開源三方組件 -- 進程間通信的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-09 10:24:49

鴻蒙HarmonyOS應用

2021-08-02 14:54:50

鴻蒙HarmonyOS應用

2021-04-29 14:32:24

鴻蒙HarmonyOS應用

2021-03-10 15:03:40

鴻蒙HarmonyOS應用

2021-04-28 09:56:44

鴻蒙HarmonyOS應用

2021-04-28 15:07:06

鴻蒙HarmonyOS應用

2021-08-04 14:16:41

鴻蒙HarmonyOS應用

2021-03-24 09:30:49

鴻蒙HarmonyOS應用

2021-08-03 10:07:41

鴻蒙HarmonyOS應用

2021-11-17 15:37:43

鴻蒙HarmonyOS應用

2021-01-27 10:04:46

鴻蒙HarmonyOS動畫

2021-08-10 15:23:08

鴻蒙HarmonyOS應用

2021-10-19 10:04:51

鴻蒙HarmonyOS應用

2021-08-26 16:07:46

鴻蒙HarmonyOS應用

2021-07-06 18:21:31

鴻蒙HarmonyOS應用

2021-04-08 14:57:52

鴻蒙HarmonyOS應用

2021-04-20 15:06:42

鴻蒙HarmonyOS應用

2021-08-30 17:55:58

鴻蒙HarmonyOS應用

2021-08-05 15:06:30

鴻蒙HarmonyOS應用

2021-03-01 14:00:11

鴻蒙HarmonyOS應用
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 日韩在线中文字幕 | 中文字幕欧美一区 | 亚洲一区二区三区在线视频 | 国产区精品在线观看 | 亚洲免费一区 | 久久久国产精品一区 | 99精品免费 | 国产一区二区三区久久久久久久久 | 日本手机在线 | 欧美性受 | 一区二区在线观看av | 一本一道久久a久久精品蜜桃 | 国产一级片网站 | 天天草天天爱 | 99精品国产一区二区三区 | 亚洲国产精品人人爽夜夜爽 | 天天草天天 | 一区二区三区在线播放 | 午夜精品久久久久久久久久久久 | 午夜精品久久久久久久久久久久久 | 激情视频一区 | 久久91精品| 国产成人精品网站 | 在线看无码的免费网站 | 国产不卡一区在线观看 | 国产日韩精品一区 | 国产成都精品91一区二区三 | 国产精品入口麻豆www | 国产精品美女www爽爽爽 | 99免费在线视频 | 精品成人av | 国产jizz女人多喷水99 | 2019天天操 | 精品国产欧美日韩不卡在线观看 | 久久久亚洲 | 日韩另类视频 | 久久伊人精品一区二区三区 | 欧美日韩精品一区二区三区四区 | 91久久久久久久久 | 日产精品久久久一区二区福利 | 影音先锋欧美资源 |