自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

鴻蒙開源三方組件 -- 進程間通信的AndLinker組件

開源
AndLinker是一款OpenHarmony上的IPC (進程間通信) 庫,結(jié)合了[ZIDL][zidl]和[Retrofit][retrofit]的諸多特性,且可以與[RxJava][rxjava]和[RxJava2][rxjava2]的Call Adapters無縫結(jié)合使用。

[[415055]]

想了解更多內(nèi)容,請訪問:

51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)

https://harmonyos.51cto.com

1.組件

AndLinker是一款OpenHarmony上的IPC (進程間通信) 庫,結(jié)合了[ZIDL][zidl]和[Retrofit][retrofit]的諸多特性,且可以與[RxJava][rxjava]和[RxJava2][rxjava2]的Call Adapters無縫結(jié)合使用。項目的設(shè)計與部分代碼參考了偉大的[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一樣生成遠程服務(wù)接口的IPC實現(xiàn)。
  • 支持Call Adapters:Call, RxJava Observable,RxJava2 Observable & Flowable。
  • 支持遠程服務(wù)回調(diào)機制。
  • 支持AIDL的所有數(shù)據(jù)類型。
  • 支持AIDL的所有數(shù)據(jù)定向tag: in,out,inout。
  • 支持AIDL的oneway關(guān)鍵字。

4.如何使用

使用注解@RemoteInterface修飾遠程服務(wù)接口IRemoteService,并實現(xiàn)它:

  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); 

在服務(wù)端App中,創(chuàng)建AndLinkerBinder對象,并注冊上面的接口實現(xiàn)。然后在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創(chuàng)建AndLinker對象,并通過create()方法生成一個IRemoteService遠程接口的IPC實現(xiàn):

  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(); 

現(xiàn)在mRemoteService對象中的所有方法都是IPC方法。

基本使用-效果展示

AndLinker支持AIDL所有數(shù)據(jù)類型:

  • Java語言中的所有原始類型:int,long,char,boolean等等。
  • String
  • CharSequence
  • Paracelable
  • List(List中的所有元素必須是此列表中支持的數(shù)據(jù)類型)
  • Map(Map中的所有元素必須是此列表中支持的數(shù)據(jù)類型)
  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.        }); 
【全網(wǎng)首發(fā)】鴻蒙開源三方組件 -- 進程間通信的AndLinker組件-鴻蒙HarmonyOS技術(shù)社區(qū)
【全網(wǎng)首發(fā)】鴻蒙開源三方組件 -- 進程間通信的AndLinker組件-鴻蒙HarmonyOS技術(shù)社區(qū)

進階使用-效果展示

1.Call Adapters

在客戶端App中,你可以copy并修改遠程服務(wù)接口,包裝方法的返回值

  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中注冊對應(yīng)的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(); 
【全網(wǎng)首發(fā)】鴻蒙開源三方組件 -- 進程間通信的AndLinker組件-鴻蒙HarmonyOS技術(shù)社區(qū)
【全網(wǎng)首發(fā)】鴻蒙開源三方組件 -- 進程間通信的AndLinker組件-鴻蒙HarmonyOS技術(shù)社區(qū)

2.遠程服務(wù)接口回調(diào)

使用@RemoteInterface注解修飾遠程服務(wù)回調(diào)接口IRemoteCallback

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

在遠程方法中使用@Callback注解修飾callback參數(shù)

  1. void registerCallback(@Callback IRemoteCallback callback); 

在客戶端App中,實現(xiàn)上面定義的遠程服務(wù)回調(diào)接口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.     }; 
【全網(wǎng)首發(fā)】鴻蒙開源三方組件 -- 進程間通信的AndLinker組件-鴻蒙HarmonyOS技術(shù)社區(qū)

3.指定數(shù)據(jù)定向tag

可以為遠程方法的參數(shù)指定@In,@Out或者@Inout注解,標記了數(shù)據(jù)在底層Builder中的流向

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

注意:

  • 所有非原始類型必須指定數(shù)據(jù)定向tag:@In,@Out,或者@Inout,用來標記數(shù)據(jù)的流向。原始類型默認是@In類型,并且不能指定其他值。
  • 使用@Out或者@Inout修飾的Parcelable參數(shù)必須實現(xiàn)SuperParcelable接口,否則你必須手動添加此方法public void readFromParcel(Parcel in)。
【全網(wǎng)首發(fā)】鴻蒙開源三方組件 -- 進程間通信的AndLinker組件-鴻蒙HarmonyOS技術(shù)社區(qū)

4.使用@OnewWay注解

在遠程方法上使用@OneWay注解,這會修改遠程方法調(diào)用的行為。當使用它時,遠程方法調(diào)用不會堵塞,它只是簡單的發(fā)送數(shù)據(jù)并立即返回。

  1. @OneWay 
  2. void onewayMethod(String msg); 
【全網(wǎng)首發(fā)】鴻蒙開源三方組件 -- 進程間通信的AndLinker組件-鴻蒙HarmonyOS技術(shù)社區(qū)

5. 下載鏈接

5.1 IDE下載鏈接

https://developer.harmonyos.com/cn/develop/deveco-studio#download

5.2 源碼鏈接

https://gitee.com/openneusoft/and-linkers

想了解更多內(nèi)容,請訪問:

51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)

https://harmonyos.51cto.com

 

責(zé)任編輯:jianghua 來源: 鴻蒙社區(qū)
相關(guān)推薦

2021-08-09 10:24:49

鴻蒙HarmonyOS應(yīng)用

2021-08-02 14:54:50

鴻蒙HarmonyOS應(yīng)用

2021-03-10 15:03:40

鴻蒙HarmonyOS應(yīng)用

2021-04-29 14:32:24

鴻蒙HarmonyOS應(yīng)用

2021-04-28 09:56:44

鴻蒙HarmonyOS應(yīng)用

2021-04-28 15:07:06

鴻蒙HarmonyOS應(yīng)用

2021-08-04 14:16:41

鴻蒙HarmonyOS應(yīng)用

2021-08-03 10:07:41

鴻蒙HarmonyOS應(yīng)用

2021-03-24 09:30:49

鴻蒙HarmonyOS應(yīng)用

2021-11-17 15:37:43

鴻蒙HarmonyOS應(yīng)用

2021-01-27 10:04:46

鴻蒙HarmonyOS動畫

2021-08-10 15:23:08

鴻蒙HarmonyOS應(yīng)用

2021-10-19 10:04:51

鴻蒙HarmonyOS應(yīng)用

2021-08-26 16:07:46

鴻蒙HarmonyOS應(yīng)用

2021-04-08 14:57:52

鴻蒙HarmonyOS應(yīng)用

2021-04-20 15:06:42

鴻蒙HarmonyOS應(yīng)用

2021-07-06 18:21:31

鴻蒙HarmonyOS應(yīng)用

2021-08-05 15:06:30

鴻蒙HarmonyOS應(yīng)用

2021-08-30 17:55:58

鴻蒙HarmonyOS應(yīng)用

2021-03-01 14:00:11

鴻蒙HarmonyOS應(yīng)用
點贊
收藏

51CTO技術(shù)棧公眾號