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

HarmonyOS Sample之JavaDistributeAuthDemo分布式身份認(rèn)證功能

開發(fā) 前端 分布式 OpenHarmony
相信大部分關(guān)注HarmonyOS的人來說,對于HarmonyOS的特性都有一定的了解了,從官網(wǎng)我們可以看到一些關(guān)鍵的提煉:“統(tǒng)一OS,彈性部署”,“硬件互助,資源共享”,“一次開發(fā),多端部署”。

[[439852]]

想了解更多內(nèi)容,請?jiān)L問:

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

https://harmonyos.51cto.com

1.介紹

相信大部分關(guān)注HarmonyOS的人來說,對于HarmonyOS的特性都有一定的了解了,從官網(wǎng)我們可以看到一些關(guān)鍵的提煉:“統(tǒng)一OS,彈性部署”,“硬件互助,資源共享”,“一次開發(fā),多端部署”。

接下來幾期就想和大家一起就HarmonyOS的特性,來找一些案例進(jìn)行學(xué)習(xí)和實(shí)踐,目的是進(jìn)一步鞏固對特性的理解然后去靈活應(yīng)用。

這一期是通過分布式身份認(rèn)證的功能來了解一下 常用的通信方法。

分享的內(nèi)容:

  • 在設(shè)備遷移或協(xié)同時(shí)都需要顯示可用設(shè)備列表,有一種方式不需要自己單獨(dú)獲取設(shè)備,也不需要自己定義列表布局文件就可以顯示設(shè)備窗口。
  • 如何實(shí)現(xiàn)一個(gè)分布式身份認(rèn)證授權(quán)的功能。

案例來自codelabs官方示例分布式鑒權(quán)(Java) 本貼進(jìn)行了整理和分析,供學(xué)習(xí)和交流使用。

2.效果展示

HarmonyOS Sample 之 JavaDistributeAuthDemo分布式身份認(rèn)證功能-鴻蒙HarmonyOS技術(shù)社區(qū)

3.搭建環(huán)境

安裝DevEco Studio,詳情請參考DevEco Studio下載。

設(shè)置DevEco Studio開發(fā)環(huán)境,DevEco Studio開發(fā)環(huán)境需要依賴于網(wǎng)絡(luò)環(huán)境,需要連接上網(wǎng)絡(luò)才能確保工具的正常使用,可以根據(jù)如下兩種情況來配置開發(fā)環(huán)境:

如果可以直接訪問Internet,只需進(jìn)行下載HarmonyOS SDK操作

如果網(wǎng)絡(luò)不能直接訪問Internet,需要通過代理服務(wù)器才可以訪問,請參考配置開發(fā)環(huán)境。

下載源碼后,使用DevEco Studio 打開項(xiàng)目,模擬器運(yùn)行即可。

真機(jī)上運(yùn)行,參見真機(jī)運(yùn)行應(yīng)用

4.項(xiàng)目結(jié)構(gòu)

HarmonyOS Sample 之 JavaDistributeAuthDemo分布式身份認(rèn)證功能-鴻蒙HarmonyOS技術(shù)社區(qū)

5.代碼講解

5.1 一種顯示流轉(zhuǎn)設(shè)備列表的方法

這種方式不需要自己單獨(dú)獲取設(shè)備,也不需要定義對應(yīng)的布局文件就可以顯示設(shè)備窗口。

①向ContinuationRegisterManager注冊一個(gè)跳轉(zhuǎn)的能力,并獲得分配給該能力的注冊令牌

  1. /** 
  2.  * 注冊流轉(zhuǎn)能力 
  3.  * register Continuation 
  4.  * 
  5.  * @param context 
  6.  * @param deviceCallback 
  7.  * @param show           是否顯示可用流轉(zhuǎn)設(shè)備 
  8.  */ 
  9. public void registerContinuation(AbilitySlice context, DeviceCallback deviceCallback, boolean show) { 
  10.     LogUtils.info("registerContinuation"); 
  11.     if (continuationRegisterManager == null) { 
  12.         this.deviceCallback = deviceCallback; 
  13.         this.show = show; 
  14.         continuationRegisterManager = context.getContinuationRegisterManager(); 
  15.  
  16.         //支持的設(shè)備類型 
  17.         ExtraParams params = new ExtraParams(); 
  18.         String[] devTypes = new String[]{ExtraParams.DEVICETYPE_SMART_PAD, 
  19.                 ExtraParams.DEVICETYPE_SMART_WATCH, 
  20.                 ExtraParams.DEVICETYPE_SMART_PHONE}; 
  21.         params.setDevType(devTypes); 
  22.  
  23.         //向ContinuationRegisterManager注冊一個(gè)跳轉(zhuǎn)的能力,并獲得分配給該能力的注冊令牌 
  24.         //您可以使用 IContinuationDeviceCallback 來監(jiān)聽用戶選擇設(shè)備進(jìn)行能力跳躍后的設(shè)備連接狀態(tài)變化,并實(shí)現(xiàn)您自己的處理邏輯。 
  25.         continuationRegisterManager.register(context.getBundleName(), params, callback, requestCallback); 
  26.     } else { 
  27.         if (show) { 
  28.             //顯示設(shè)備列表 
  29.             showContinuationDevice(); 
  30.         } 
  31.     } 

②完成流轉(zhuǎn)后的狀態(tài)回調(diào),提供用于偵聽設(shè)備連接狀態(tài)更改的回調(diào)

  1. //完成流轉(zhuǎn)后的狀態(tài)回調(diào),提供用于偵聽設(shè)備連接狀態(tài)更改的回調(diào)。 
  2. private IContinuationDeviceCallback callback = new IContinuationDeviceCallback() { 
  3.     @Override 
  4.     public void onDeviceConnectDone(String deviceId, String val) { 
  5.         LogUtils.info("onDeviceConnectDone"); 
  6.  
  7.         //設(shè)備連接完成后,提交選中設(shè)備的任務(wù)到隊(duì)列,等同于點(diǎn)擊了要流轉(zhuǎn)的設(shè)備 
  8.         EventHandler eventHandler = new EventHandler(EventRunner.getMainEventRunner()); 
  9.         //提交任務(wù) 到事件隊(duì)列。 
  10.         eventHandler.postTask(new Runnable() { 
  11.             @Override 
  12.             public void run() { 
  13.                 if (deviceCallback != null) { 
  14.                     deviceCallback.onItemClick(deviceId); 
  15.                 } 
  16.                 //更新指定能力成功跳轉(zhuǎn)的設(shè)備的連接狀態(tài)。 
  17.                 continuationRegisterManager 
  18.                         .updateConnectStatus(abilityToken, 
  19.                                 //表示需要更新連接狀態(tài)的設(shè)備的ID。 
  20.                                 deviceId, 
  21.                                 DeviceConnectState.IDLE.getState(),null 
  22.                                 ); 
  23.             } 
  24.         }); 
  25.     } 
  26.  
  27.     @Override 
  28.     public void onDeviceDisconnectDone(String deviceId) { 
  29.         LogUtils.info("onDeviceDisconnectDone"); 
  30.     } 
  31. }; 

③完成流轉(zhuǎn)請求的回調(diào),顯示可流轉(zhuǎn)的設(shè)備

  1. //完成流轉(zhuǎn)請求的回調(diào),提供用于偵聽躍點(diǎn)任務(wù)管理服務(wù)的連接狀態(tài)變化的回調(diào)。 
  2. private RequestCallback requestCallback = new RequestCallback() { 
  3.     @Override 
  4.     public void onResult(int result) { 
  5.         abilityToken = result; 
  6.         if (show) { 
  7.             //顯示 可流轉(zhuǎn)設(shè)備 
  8.             showContinuationDevice(); 
  9.         } 
  10.     } 
  11. }; 
  12.  
  13. /** 
  14.  * 顯示 可流轉(zhuǎn)設(shè)備 
  15.  * show Continuation 
  16.  */ 
  17. private void showContinuationDevice() { 
  18.     LogUtils.info("showContinuation"); 
  19.  
  20.     ExtraParams extraParams = new ExtraParams(); 
  21.     extraParams.setDevType(new String[]{ExtraParams.DEVICETYPE_SMART_TV, 
  22.             ExtraParams.DEVICETYPE_SMART_PAD, 
  23.             ExtraParams.DEVICETYPE_SMART_WATCH, 
  24.             ExtraParams.DEVICETYPE_SMART_PHONE}); 
  25.     extraParams.setDescription("設(shè)備流轉(zhuǎn)測試"); 
  26.  
  27.     //顯示 可流轉(zhuǎn)設(shè)備 
  28.     continuationRegisterManager.showDeviceList(abilityToken, extraParams, null); 
  29.  

5.2 實(shí)現(xiàn)一個(gè)分布式身份認(rèn)證授權(quán)的功能

為了方便理解,把發(fā)送請求的設(shè)備成為 請求授權(quán)設(shè)備,進(jìn)行授權(quán)操作的設(shè)備成為 授權(quán)設(shè)備。

RegisterManager 自定義了CommonEvent 接口,MainAbilitySlice實(shí)現(xiàn)了該接口,所以RegisterManager具備了 到 MainAbilitySlice方向的通信能力。

RegisterManager 完成了對 ConstUtil.ORDER_CODE 類型公共事件的訂閱,所以就能夠接收到該類型的公共事件。

認(rèn)證授權(quán)的完整過程:

①在請求授權(quán)設(shè)備上,RegisterManager提供了注冊設(shè)備流轉(zhuǎn)能力的函數(shù),在設(shè)備連接完成的狀態(tài)回調(diào)中 提交了一個(gè)“點(diǎn)擊設(shè)備”的任務(wù)到執(zhí)行隊(duì)列。

  1. //完成流轉(zhuǎn)后的狀態(tài)回調(diào),提供用于偵聽設(shè)備連接狀態(tài)更改的回調(diào)。 
  2. private IContinuationDeviceCallback callback = new IContinuationDeviceCallback() { 
  3.     @Override 
  4.     public void onDeviceConnectDone(String deviceId, String val) { 
  5.         LogUtils.info("onDeviceConnectDone"); 
  6.  
  7.         //設(shè)備連接完成后,提交選中設(shè)備的任務(wù)到隊(duì)列,等同于點(diǎn)擊了要流轉(zhuǎn)的設(shè)備 
  8.         EventHandler eventHandler = new EventHandler(EventRunner.getMainEventRunner()); 
  9.         //提交任務(wù) 到事件隊(duì)列。 
  10.         eventHandler.postTask(new Runnable() { 
  11.             @Override 
  12.             public void run() { 
  13.                 if (deviceCallback != null) { 
  14.                     deviceCallback.onItemClick(deviceId); 
  15.                 } 
  16.                 //更新指定能力成功跳轉(zhuǎn)的設(shè)備的連接狀態(tài)。 
  17.                 continuationRegisterManager 
  18.                         .updateConnectStatus(abilityToken, 
  19.                                 //表示需要更新連接狀態(tài)的設(shè)備的ID。 
  20.                                 deviceId, 
  21.                                 DeviceConnectState.IDLE.getState(),null 
  22.                                 ); 
  23.             } 
  24.         }); 
  25.     } 

在MainAbilitySlice中,在完成流轉(zhuǎn)能力注冊完成后,在“點(diǎn)擊設(shè)備” 的回調(diào)中,打開了遠(yuǎn)端授權(quán)設(shè)備上的AuthrRemoteSlice頁,同時(shí)傳遞了ConstUtil.DEVICE_ID和ConstUtil.ORDER_CODE(ConstUtil.START_ORDER)參數(shù)過去,其中ConstUtil.START_ORDER并沒有使用。

  1. /** 
  2.  * 注冊協(xié)同能力 
  3.  * 
  4.  * @param show 
  5.  */ 
  6. private void registerContinuation(boolean show) { 
  7.     LogUtils.info("registerContinuation"); 
  8.  
  9.     registerManager.registerContinuation(this, 
  10.             new RegisterManager.DeviceCallback() { 
  11.                 @Override 
  12.                 public void onItemClick(String deviceId) { 
  13.  
  14.                     LogUtils.info("onItemClick,deviceId:" + deviceId); 
  15.  
  16.                     //啟動(dòng)遠(yuǎn)端Ablity 
  17.                     startRemoteAbility(deviceId); 
  18.                 } 
  19.             }, show); 
  20.  
  21. /** 
  22.  * 啟動(dòng)遠(yuǎn)端FA 
  23.  * 
  24.  * @param deviceId 
  25.  */ 
  26. private void startRemoteAbility(String deviceId) { 
  27.     LogUtils.info("startRemoteAbility"); 
  28.  
  29.     DialogUtil.showToast(getContext(), "請求已經(jīng)發(fā)送,等待對方確認(rèn)。"); 
  30.     // 
  31.     String localDeviceId = KvManagerFactory.getInstance().createKvManager( 
  32.             new KvManagerConfig(this)).getLocalDeviceInfo().getId(); 
  33.  
  34.     Intent intent = new Intent(); 
  35.     Operation operation = 
  36.             new Intent.OperationBuilder() 
  37.                     .withDeviceId(deviceId) 
  38.                     .withBundleName(getBundleName()) 
  39.                     .withAbilityName(MainAbility.class.getName()) 
  40.                     //指向授權(quán)設(shè)備的認(rèn)證頁面路由 
  41.                     .withAction(MainAbility.ACTION
  42.                     .withFlags(Intent.FLAG_ABILITYSLICE_MULTI_DEVICE) 
  43.                     .build(); 
  44.     intent.setOperation(operation); 
  45.     intent.setParam(ConstUtil.DEVICE_ID, localDeviceId); 
  46.     //啟動(dòng)遠(yuǎn)端FA指令代碼 
  47.     intent.setParam(ConstUtil.ORDER_CODE, ConstUtil.START_ORDER); 
  48.     startAbility(intent); 

②在授權(quán)設(shè)備上的AuthrRemoteSlice頁被打開后,點(diǎn)擊允許或不允許時(shí),請求分布式權(quán)限后,又打開了請求授權(quán)設(shè)備的 MainAbility。

  1. private void initViewData() { 
  2.     LogUtils.info("initViewData"); 
  3.  
  4.     findComponentById(ResourceTable.Id_yes_btn).setClickedListener(component -> { 
  5.         sendMessage(AUTH_TYPE1); 
  6.     }); 
  7.     findComponentById(ResourceTable.Id_no_btn).setClickedListener(component -> { 
  8.         sendMessage(AUTH_TYPE2); 
  9.     }); 
  10.  
  11. private void sendMessage(int type) { 
  12.     LogUtils.info("sendMessage"); 
  13.  
  14.     //從MainAbility獲取HPermission實(shí)例 
  15.     HPermission hPermission = ((MainAbility) getAbility()).getPermission(); 
  16.  
  17.     //如果用戶已允許分布式權(quán)限,設(shè)置按鈕可用 
  18.     hPermission.requestPermissions(this, () -> { 
  19.         // button Enabled 
  20.         findComponentById(ResourceTable.Id_yes_btn).setEnabled(false); 
  21.         findComponentById(ResourceTable.Id_no_btn).setEnabled(false); 
  22.  
  23.         //打開請求側(cè)的頁面 
  24.         startRemoteAbility(type); 
  25.     }); 
  26.  
  27. /** 
  28.  * 打開請求授權(quán)側(cè)的MainAbility 
  29.  * 
  30.  * @param type 是否同意授權(quán) 
  31.  */ 
  32. private void startRemoteAbility(int type) { 
  33.     LogUtils.info("startRemoteAbility"); 
  34.  
  35.     DialogUtil.showToast(getContext(), type == AUTH_TYPE1 ? "允許玩游戲" : "已拒絕玩游戲"); 
  36.     Intent intent = new Intent(); 
  37.     Operation operation = 
  38.             new Intent.OperationBuilder() 
  39.                     .withDeviceId(remoteDeviceId == null ? "" : remoteDeviceId) 
  40.                     .withBundleName(getBundleName()) 
  41.                     .withAbilityName(MainAbility.class.getName()) 
  42.                     .withFlags(Intent.FLAG_ABILITYSLICE_MULTI_DEVICE) 
  43.                     .build(); 
  44.     intent.setOperation(operation); 
  45.  
  46.     //授權(quán)碼 
  47.     intent.setParam(ORDER_CODE, type); 
  48.     startAbility(intent); 
  49.  
  50.     //關(guān)閉當(dāng)前宿主 Ability 
  51.     getUITaskDispatcher().delayDispatch(() -> terminateAbility(), DELAY); 

③在請求授權(quán)設(shè)備上,由于MainAbility設(shè)置為singleton模式(“launchType”: “singleton”)而且已經(jīng)實(shí)例過,所以請求進(jìn)入到onNewIntent函數(shù)。

config.json

  1.   ... 
  2.   "orientation""unspecified"
  3.   "visible"true
  4.   "name""com.buty.javadistributedemo.MainAbility"
  5.   "icon""$media:icon"
  6.   "description""$string:mainability_description"
  7.   "label""$string:entry_MainAbility"
  8.   "type""page"
  9.   "launchType""singleton" 

在onNewIntent函數(shù)中,通過 CommonEventManager發(fā)布一個(gè)ConstUtil.ORDER_CODE類型的事件,該事件被RegisterManager收到并進(jìn)行了處理,如何處理的呢,又通過RegisterManager.CommonEvent 把事件傳遞給了實(shí)現(xiàn)了RegisterManager.CommonEvent接口MainAbilitySlice,最終顯示對端設(shè)備的授權(quán)結(jié)果(允許/不允許)

  1. /** 
  2.  * Ability設(shè)置為singleton模式 
  3.  * 當(dāng)創(chuàng)建時(shí),如果實(shí)例已存在,觸發(fā)該函數(shù) 
  4.  * 
  5.  * @param intent 
  6.  */ 
  7. @Override 
  8. protected void onNewIntent(Intent intent) { 
  9.     LogUtils.info("onNewIntent"); 
  10.     super.onNewIntent(intent); 
  11.     //是否允許 
  12.     int code = intent.getIntParam(ConstUtil.ORDER_CODE, 0); 
  13.     // 
  14.     String deviceId = intent.getStringParam(ConstUtil.DEVICE_ID); 
  15.     sendCommonEvent(code, deviceId); 

 MainAbilitySlice收到消息

  1. /** 
  2.  * 實(shí)現(xiàn) RegisterManager的 CommonEvent接口 
  3.  * 
  4.  * @param code     code 
  5.  * @param deviceId deviceId 
  6.  */ 
  7. @Override 
  8. public void onReceiveEvent(int code, String deviceId) { 
  9.     LogUtils.info("onReceiveEvent,code:"+code); 
  10.     switch (code) { 
  11.         // Agree to allow games to be played 
  12.         case ConstUtil.AUTH_TYPE1: 
  13.             //start.setVisibility(Component.HIDE); 
  14.             tips.setVisibility(Component.VISIBLE); 
  15.             tips.setText("已授權(quán),可以開始游戲"); 
  16.  
  17.             break; 
  18.         // Refuse to play games 
  19.         case ConstUtil.AUTH_TYPE2: 
  20.             tips.setVisibility(Component.VISIBLE); 
  21.             //DialogUtil.exitDialog(getAbility()); 
  22.             tips.setText("已拒絕,不可以游戲"); 
  23.             break; 
  24.         default
  25.             break; 
  26.     } 

6.思考總結(jié)

分布式中常用的通信方式:

1.Intent 直接傳遞參數(shù)(intent.setParam(ORDER_CODE, type))

2.公共事件訂閱/發(fā)布的方式(Intent封裝到CommonEventData)

3.自定義接口的方式(RegisterManager.CommonEvent)

文章相關(guān)附件可以點(diǎn)擊下面的原文鏈接前往下載

https://harmonyos.51cto.com/resource/1574

想了解更多內(nèi)容,請?jiān)L問:

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

https://harmonyos.51cto.com

 

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

2021-12-02 10:11:44

鴻蒙HarmonyOS應(yīng)用

2021-08-24 15:13:06

鴻蒙HarmonyOS應(yīng)用

2019-11-20 15:34:16

區(qū)塊鏈金融去中心化

2021-08-31 22:52:40

區(qū)塊鏈互聯(lián)網(wǎng)技術(shù)

2018-07-17 08:14:22

分布式分布式鎖方位

2021-09-24 09:25:01

鴻蒙HarmonyOS應(yīng)用

2021-10-21 10:03:09

鴻蒙HarmonyOS應(yīng)用

2021-11-16 09:38:10

鴻蒙HarmonyOS應(yīng)用

2022-03-06 21:43:05

Citus架構(gòu)PostgreSQL

2021-05-28 09:52:00

鴻蒙HarmonyOS應(yīng)用

2019-02-13 13:41:07

MemCache分布式HashMap

2019-09-26 15:43:52

Hadoop集群防火墻

2019-10-10 09:16:34

Zookeeper架構(gòu)分布式

2023-05-29 14:07:00

Zuul網(wǎng)關(guān)系統(tǒng)

2017-09-01 05:35:58

分布式計(jì)算存儲(chǔ)

2019-06-19 15:40:06

分布式鎖RedisJava

2021-07-22 10:20:21

鴻蒙HarmonyOS應(yīng)用

2021-11-02 10:10:49

鴻蒙HarmonyOS應(yīng)用

2019-07-04 15:13:16

分布式緩存Redis

2019-12-26 08:59:20

Redis主從架構(gòu)
點(diǎn)贊
收藏

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