偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

HarmonyOS IDL跨進(jìn)程通信實(shí)現(xiàn)

系統(tǒng) OpenHarmony
IDL跨進(jìn)程通信,簡單來說就是讓你在一個APP中可以與另一個APP進(jìn)行通信。首先一點(diǎn),跨進(jìn)程通信,就是要有兩個應(yīng)用,一個作為服務(wù)端server,另一個作為客戶端client。

[[416898]]

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

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

https://harmonyos.51cto.com

簡介

IDL跨進(jìn)程通信,簡單來說就是讓你在一個APP中可以與另一個APP進(jìn)行通信。

首先一點(diǎn),跨進(jìn)程通信,就是要有兩個應(yīng)用,一個作為服務(wù)端server,另一個作為客戶端client。

接下來將帶領(lǐng)大家實(shí)現(xiàn)一個簡單的計算功能

1.首先在一個新項目中,創(chuàng)建兩個moudle,rpcserver和rpcclient通信作為服務(wù)端和客戶端,新建時記得選擇EmptyAbility(Java)哦。

【中軟國際】HarmonyOS IDL跨進(jìn)程通信實(shí)現(xiàn)-鴻蒙HarmonyOS技術(shù)社區(qū)

2.在rpcserver中新建一個ServiceAbility

【中軟國際】HarmonyOS IDL跨進(jìn)程通信實(shí)現(xiàn)-鴻蒙HarmonyOS技術(shù)社區(qū)

3.那么重點(diǎn)要來了,這時候需要再創(chuàng)建一個IDL文件,打開rpcserver,點(diǎn)擊任意目錄創(chuàng)建即可。

【中軟國際】HarmonyOS IDL跨進(jìn)程通信實(shí)現(xiàn)-鴻蒙HarmonyOS技術(shù)社區(qū)

創(chuàng)建完成后,系統(tǒng)會將新創(chuàng)建的IDL文件放在指定的目錄下,該目錄由系統(tǒng)自動生成。

【中軟國際】HarmonyOS IDL跨進(jìn)程通信實(shí)現(xiàn)-鴻蒙HarmonyOS技術(shù)社區(qū)

打開IDL文件,添加一個addNumber方法。

  1. int addNumber([inint numA, [inint numB); 

打開rpcclient,和rpcserver一樣,創(chuàng)建一個名稱相同的IDL文件,當(dāng)然文件中的addNumber方法也需要保持一致 。另外注意一點(diǎn),IDL文件添加完成后,需要執(zhí)行g(shù)radle中的compileDebugIdl命令。

【中軟國際】HarmonyOS IDL跨進(jìn)程通信實(shí)現(xiàn)-鴻蒙HarmonyOS技術(shù)社區(qū)

這樣系統(tǒng)就會自動給我們生成IDL需要連接需要用到的工具類。

【中軟國際】HarmonyOS IDL跨進(jìn)程通信實(shí)現(xiàn)-鴻蒙HarmonyOS技術(shù)社區(qū)

4.現(xiàn)在準(zhǔn)備工作已經(jīng)完成,下一步就是客戶端給服務(wù)端傳參數(shù),服務(wù)端根據(jù)參數(shù)返回計算結(jié)果的過程

客戶端代碼

  1. /** 
  2.  * MainAbilitySlice 
  3.  */ 
  4. public class MainAbilitySlice extends AbilitySlice { 
  5.  
  6.     @Override 
  7.     public void onStart(Intent intent) { 
  8.         super.onStart(intent); 
  9.         super.setUIContent(ResourceTable.Layout_main_blility); 
  10.         // 數(shù)字輸入框A 
  11.         TextField numFieldA = (TextField) findComponentById(ResourceTable.Id_numA); 
  12.         // 數(shù)字輸入框B 
  13.         TextField numFieldB = (TextField) findComponentById(ResourceTable.Id_numB); 
  14.         // 開始通信按鈕 
  15.         Component component = findComponentById(ResourceTable.Id_start); 
  16.         component.setClickedListener(new Component.ClickedListener() { 
  17.             @Override 
  18.             public void onClick(Component component) { 
  19.                 int numA = numFieldA.getText() == null ? 0 : Integer.parseInt(numFieldA.getText()); 
  20.                 int numB = numFieldB.getText() == null ? 0 : Integer.parseInt(numFieldB.getText()); 
  21.                 // 執(zhí)行通信 
  22.                 execute(numA, numB); 
  23.             } 
  24.         }); 
  25.     } 
  26.  
  27.     /** 
  28.      * 調(diào)用server獲取結(jié)果 
  29.      * @param numA 
  30.      * @param numB 
  31.      */ 
  32.     public void execute(int numA, int numB){ 
  33.         Intent intent = new Intent(); 
  34.         ElementName elementName = new ElementName( 
  35.         ""
  36.         "org.rpc.server",//這個值為rpcserver下config中的bundleName 
  37.         "ohos.samples.rpcserver.ServiceAbility");//這個值為rpcserver下對應(yīng)的ServiceAbility 
  38.         intent.setElement(elementName); 
  39.         connectAbility(intent, new IAbilityConnection() { 
  40.             @Override 
  41.             public void onAbilityConnectDone(ElementName elementName, IRemoteObject remoteObject, int resultCode) { 
  42.                 TestConnectProxy testConnectProxy = new TestConnectProxy(remoteObject); 
  43.                 try { 
  44.                     // 調(diào)用server獲取計算結(jié)果 
  45.                     int sum = testConnectProxy.addNumber(numA, numB); 
  46.                     // 將計算結(jié)果顯示到text組件上 
  47.                     ((Text) findComponentById(ResourceTable.Id_text_result)).setText("計算結(jié)果為:" + sum); 
  48.                 } catch (RemoteException e) { 
  49.                     e.printStackTrace(); 
  50.                 } 
  51.             } 
  52.  
  53.             @Override 
  54.             public void onAbilityDisconnectDone(ElementName elementName, int resultCode) { 
  55.                 System.out.println("------resultCode = " + resultCode); 
  56.             } 
  57.         }); 
  58.     } 

服務(wù)端代碼

  1. /** 
  2.  * ServiceAbility 
  3.  */ 
  4. public class ServiceAbility extends Ability { 
  5.     @Override 
  6.     protected IRemoteObject onConnect(Intent intent) { 
  7.         return new TestConnectStubImpl("starting ICalculatorInterface"); 
  8.     } 
  9.     /** 
  10.      * CalculatorInterfaceStubImpl 
  11.      */ 
  12.     private class TestConnectStubImpl extends TestConnectStub { 
  13.         public TestConnectStubImpl(String descriptor) { 
  14.             super(descriptor); 
  15.         } 
  16.         /** 
  17.          * 實(shí)現(xiàn) addNumber方法 
  18.          */ 
  19.         @Override 
  20.         public int addNumber(int numA, int numB) { 
  21.             return numA + numB; 
  22.         } 
  23.     } 

5.終于到了檢驗(yàn)成果的時刻,先運(yùn)行rpcserver, 運(yùn)行后,再運(yùn)行rpcclient,任意輸入兩個數(shù)字,比如33和55,然后點(diǎn)擊開始通信,這時計算結(jié)果顯示為88,通信成功~!

到此一個簡單的IDL通信demo已經(jīng)完成了

最后附上源碼,感興趣的小伙伴可以下載體驗(yàn)

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

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

https://harmonyos.51cto.com

 

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

2023-08-01 08:43:29

Python多線程

2017-03-28 10:20:24

Docker通信分析

2009-12-22 09:11:31

WCF雙向通信

2011-08-31 13:22:37

PhoneGapAndroidjavascript

2023-09-08 09:12:57

內(nèi)存緩存圖像

2020-11-04 07:17:42

Nodejs通信進(jìn)程

2009-07-15 16:05:04

IP通信捷思銳科技Zed-3

2021-09-30 10:45:33

Linux進(jìn)程通信

2013-03-28 13:14:45

AIDL進(jìn)程間通信Android使用AI

2011-06-22 17:49:35

Linux Qt 串口

2021-06-28 10:20:31

網(wǎng)絡(luò)技術(shù)Kubernetes通信

2011-09-05 10:07:03

多媒體融合通信智能化

2023-12-07 12:45:58

進(jìn)程共享數(shù)據(jù)

2017-08-06 00:05:18

進(jìn)程通信開發(fā)

2023-11-06 08:22:34

AIDLAndroid通信

2010-01-04 16:50:04

Silverlight

2009-06-23 11:49:22

跨進(jìn)程消息鉤子VB.NET

2020-11-23 13:09:42

HI3861

2024-01-03 10:17:51

Linux通信

2011-04-22 10:30:11

VMwareWindowsFTP
點(diǎn)贊
收藏

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