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

HarmonyOS Sample之JavaDistributeAuthDemo分布式身份認證功能

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

[[439852]]

想了解更多內容,請訪問:

51CTO和華為官方合作共建的鴻蒙技術社區

https://harmonyos.51cto.com

1.介紹

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

接下來幾期就想和大家一起就HarmonyOS的特性,來找一些案例進行學習和實踐,目的是進一步鞏固對特性的理解然后去靈活應用。

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

分享的內容:

  • 在設備遷移或協同時都需要顯示可用設備列表,有一種方式不需要自己單獨獲取設備,也不需要自己定義列表布局文件就可以顯示設備窗口。
  • 如何實現一個分布式身份認證授權的功能。

案例來自codelabs官方示例分布式鑒權(Java) 本貼進行了整理和分析,供學習和交流使用。

2.效果展示

HarmonyOS Sample 之 JavaDistributeAuthDemo分布式身份認證功能-鴻蒙HarmonyOS技術社區

3.搭建環境

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

設置DevEco Studio開發環境,DevEco Studio開發環境需要依賴于網絡環境,需要連接上網絡才能確保工具的正常使用,可以根據如下兩種情況來配置開發環境:

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

如果網絡不能直接訪問Internet,需要通過代理服務器才可以訪問,請參考配置開發環境

下載源碼后,使用DevEco Studio 打開項目,模擬器運行即可。

真機上運行,參見真機運行應用

4.項目結構

HarmonyOS Sample 之 JavaDistributeAuthDemo分布式身份認證功能-鴻蒙HarmonyOS技術社區

5.代碼講解

5.1 一種顯示流轉設備列表的方法

這種方式不需要自己單獨獲取設備,也不需要定義對應的布局文件就可以顯示設備窗口。

①向ContinuationRegisterManager注冊一個跳轉的能力,并獲得分配給該能力的注冊令牌

  1. /** 
  2.  * 注冊流轉能力 
  3.  * register Continuation 
  4.  * 
  5.  * @param context 
  6.  * @param deviceCallback 
  7.  * @param show           是否顯示可用流轉設備 
  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.         //支持的設備類型 
  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注冊一個跳轉的能力,并獲得分配給該能力的注冊令牌 
  24.         //您可以使用 IContinuationDeviceCallback 來監聽用戶選擇設備進行能力跳躍后的設備連接狀態變化,并實現您自己的處理邏輯。 
  25.         continuationRegisterManager.register(context.getBundleName(), params, callback, requestCallback); 
  26.     } else { 
  27.         if (show) { 
  28.             //顯示設備列表 
  29.             showContinuationDevice(); 
  30.         } 
  31.     } 

②完成流轉后的狀態回調,提供用于偵聽設備連接狀態更改的回調

  1. //完成流轉后的狀態回調,提供用于偵聽設備連接狀態更改的回調。 
  2. private IContinuationDeviceCallback callback = new IContinuationDeviceCallback() { 
  3.     @Override 
  4.     public void onDeviceConnectDone(String deviceId, String val) { 
  5.         LogUtils.info("onDeviceConnectDone"); 
  6.  
  7.         //設備連接完成后,提交選中設備的任務到隊列,等同于點擊了要流轉的設備 
  8.         EventHandler eventHandler = new EventHandler(EventRunner.getMainEventRunner()); 
  9.         //提交任務 到事件隊列。 
  10.         eventHandler.postTask(new Runnable() { 
  11.             @Override 
  12.             public void run() { 
  13.                 if (deviceCallback != null) { 
  14.                     deviceCallback.onItemClick(deviceId); 
  15.                 } 
  16.                 //更新指定能力成功跳轉的設備的連接狀態。 
  17.                 continuationRegisterManager 
  18.                         .updateConnectStatus(abilityToken, 
  19.                                 //表示需要更新連接狀態的設備的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. }; 

③完成流轉請求的回調,顯示可流轉的設備

  1. //完成流轉請求的回調,提供用于偵聽躍點任務管理服務的連接狀態變化的回調。 
  2. private RequestCallback requestCallback = new RequestCallback() { 
  3.     @Override 
  4.     public void onResult(int result) { 
  5.         abilityToken = result; 
  6.         if (show) { 
  7.             //顯示 可流轉設備 
  8.             showContinuationDevice(); 
  9.         } 
  10.     } 
  11. }; 
  12.  
  13. /** 
  14.  * 顯示 可流轉設備 
  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("設備流轉測試"); 
  26.  
  27.     //顯示 可流轉設備 
  28.     continuationRegisterManager.showDeviceList(abilityToken, extraParams, null); 
  29.  

5.2 實現一個分布式身份認證授權的功能

為了方便理解,把發送請求的設備成為 請求授權設備,進行授權操作的設備成為 授權設備。

RegisterManager 自定義了CommonEvent 接口,MainAbilitySlice實現了該接口,所以RegisterManager具備了 到 MainAbilitySlice方向的通信能力。

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

認證授權的完整過程:

①在請求授權設備上,RegisterManager提供了注冊設備流轉能力的函數,在設備連接完成的狀態回調中 提交了一個“點擊設備”的任務到執行隊列。

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

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

  1. /** 
  2.  * 注冊協同能力 
  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.                     //啟動遠端Ablity 
  17.                     startRemoteAbility(deviceId); 
  18.                 } 
  19.             }, show); 
  20.  
  21. /** 
  22.  * 啟動遠端FA 
  23.  * 
  24.  * @param deviceId 
  25.  */ 
  26. private void startRemoteAbility(String deviceId) { 
  27.     LogUtils.info("startRemoteAbility"); 
  28.  
  29.     DialogUtil.showToast(getContext(), "請求已經發送,等待對方確認。"); 
  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.                     //指向授權設備的認證頁面路由 
  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.     //啟動遠端FA指令代碼 
  47.     intent.setParam(ConstUtil.ORDER_CODE, ConstUtil.START_ORDER); 
  48.     startAbility(intent); 

②在授權設備上的AuthrRemoteSlice頁被打開后,點擊允許或不允許時,請求分布式權限后,又打開了請求授權設備的 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實例 
  15.     HPermission hPermission = ((MainAbility) getAbility()).getPermission(); 
  16.  
  17.     //如果用戶已允許分布式權限,設置按鈕可用 
  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.         //打開請求側的頁面 
  24.         startRemoteAbility(type); 
  25.     }); 
  26.  
  27. /** 
  28.  * 打開請求授權側的MainAbility 
  29.  * 
  30.  * @param type 是否同意授權 
  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.     //授權碼 
  47.     intent.setParam(ORDER_CODE, type); 
  48.     startAbility(intent); 
  49.  
  50.     //關閉當前宿主 Ability 
  51.     getUITaskDispatcher().delayDispatch(() -> terminateAbility(), DELAY); 

③在請求授權設備上,由于MainAbility設置為singleton模式(“launchType”: “singleton”)而且已經實例過,所以請求進入到onNewIntent函數。

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函數中,通過 CommonEventManager發布一個ConstUtil.ORDER_CODE類型的事件,該事件被RegisterManager收到并進行了處理,如何處理的呢,又通過RegisterManager.CommonEvent 把事件傳遞給了實現了RegisterManager.CommonEvent接口MainAbilitySlice,最終顯示對端設備的授權結果(允許/不允許)

  1. /** 
  2.  * Ability設置為singleton模式 
  3.  * 當創建時,如果實例已存在,觸發該函數 
  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.  * 實現 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("已授權,可以開始游戲"); 
  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.思考總結

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

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

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

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

文章相關附件可以點擊下面的原文鏈接前往下載

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

想了解更多內容,請訪問:

51CTO和華為官方合作共建的鴻蒙技術社區

https://harmonyos.51cto.com

 

責任編輯:jianghua 來源: 鴻蒙社區
相關推薦

2021-12-02 10:11:44

鴻蒙HarmonyOS應用

2021-08-24 15:13:06

鴻蒙HarmonyOS應用

2019-11-20 15:34:16

區塊鏈金融去中心化

2021-08-31 22:52:40

區塊鏈互聯網技術

2018-07-17 08:14:22

分布式分布式鎖方位

2021-09-24 09:25:01

鴻蒙HarmonyOS應用

2021-10-21 10:03:09

鴻蒙HarmonyOS應用

2021-11-16 09:38:10

鴻蒙HarmonyOS應用

2021-05-28 09:52:00

鴻蒙HarmonyOS應用

2025-06-13 07:30:51

2022-03-06 21:43:05

Citus架構PostgreSQL

2019-02-13 13:41:07

MemCache分布式HashMap

2019-09-26 15:43:52

Hadoop集群防火墻

2021-07-22 10:20:21

鴻蒙HarmonyOS應用

2017-09-01 05:35:58

分布式計算存儲

2019-06-19 15:40:06

分布式鎖RedisJava

2023-05-29 14:07:00

Zuul網關系統

2021-11-02 10:10:49

鴻蒙HarmonyOS應用

2019-10-10 09:16:34

Zookeeper架構分布式

2021-12-28 17:03:29

數據質量分布式
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 日韩一区二区三区视频在线观看 | 免费视频一区二区 | 久久国产精品精品国产色婷婷 | 欧美一区二区三区在线观看 | 久久久久1 | 看片网站在线 | 亚洲精品一二区 | 桃花av在线| 亚洲精品国产区 | 国产欧美一区二区久久性色99 | 神马久久av| 免费黄色的网站 | 久久999 | 丁香久久| 黄色国产视频 | 男女爱爱福利视频 | 一区二区免费在线 | 成人在线观看网址 | 热久久久| 欧美精品一二三 | 91精品国产自产精品男人的天堂 | 天天精品综合 | 日韩中文在线观看 | 中文天堂在线一区 | 久久这里只有精品首页 | 久久99精品久久久97夜夜嗨 | 国产一区二区日韩 | 三a毛片| 亚洲免费观看视频 | 国产人成在线观看 | 九九久久国产精品 | 一区二区欧美在线 | 精品三级在线观看 | 九九热免费视频在线观看 | 国产成人精品免费视频大全最热 | 国产美女在线观看 | 午夜在线小视频 | 久久成人精品视频 | 91视频在线 | 狠狠做六月爱婷婷综合aⅴ 国产精品视频网 | 日韩一区二区三区视频在线观看 |