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

PageAbility跨設(shè)備遷移開發(fā)實(shí)戰(zhàn)—問答互動

系統(tǒng)
跨設(shè)備遷移是指將應(yīng)用中的Page頁遷移到另一設(shè)備中。可以同步應(yīng)用數(shù)據(jù),甚至可以在的不同設(shè)備間遷移,是HarmonyOS特色之一。于是,我以官方給了分布式郵件系統(tǒng)為例,寫了一個簡單的問答互動應(yīng)用。

[[441035]]

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

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

https://harmonyos.51cto.com

跨設(shè)備遷移是指將應(yīng)用中的Page頁遷移到另一設(shè)備中??梢酝綉?yīng)用數(shù)據(jù),甚至可以在的不同設(shè)備間遷移,是HarmonyOS特色之一。于是,我以官方給了分布式郵件系統(tǒng)為例,寫了一個簡單的問答互動應(yīng)用。用戶在設(shè)備A上提問,在設(shè)備B上回答,信息通過遷移傳遞,并且能查看問答記錄。

Table of Contents

效果展示

#星光計(jì)劃2.0# PageAbility跨設(shè)備遷移開發(fā)實(shí)戰(zhàn)——問答互動-鴻蒙HarmonyOS技術(shù)社區(qū)

主要功能

實(shí)現(xiàn)問答界面,通過發(fā)送按鈕將問題、答題等信息轉(zhuǎn)遞到另一設(shè)備上。

實(shí)現(xiàn)問題記錄界面,對每個完整的問答進(jìn)行記錄,方便查看。

設(shè)備間的數(shù)據(jù)進(jìn)行同步,擁有相同的問答記錄。

遷移的主要步驟

  1. 設(shè)備A上的Page請求遷移。
  2. HarmonyOS處理遷移任務(wù),并回調(diào)設(shè)備A上Page的保存數(shù)據(jù)方法,用于保存遷移必須的數(shù)據(jù)。
  3. HarmonyOS在設(shè)備B上啟動同一個Page,并回調(diào)其恢復(fù)數(shù)據(jù)方法。

PageAbility實(shí)現(xiàn)遷移是需要實(shí)現(xiàn)IAbilityContinuation接口的,該接口如下:

  1. // 
  2. // Source code recreated from a .class file by IntelliJ IDEA 
  3. // (powered by FernFlower decompiler) 
  4. // 
  5.  
  6. package ohos.aafwk.ability; 
  7.  
  8. import ohos.aafwk.content.IntentParams; 
  9.  
  10. public interface IAbilityContinuation { 
  11.     int ERR_ABILITY_QUERY_FAILED = -2; 
  12.     int ERR_CONTINUE_TIMEOUT = -8; 
  13.     int ERR_DEVICE_OFFLINE = -9; 
  14.     int ERR_INSTALL_FREE_NOT_SUPPORTED = -4; 
  15.     int ERR_NETWORK_UNAVAILABLE = -3; 
  16.     int ERR_PARAMETER_INVALID = -6; 
  17.     int ERR_PERMISSION_DENIED = -5; 
  18.     int ERR_REMOTE_DEVICE_INCOMPATIBLE = -7; 
  19.     int ERR_UNKNOWN = -1; 
  20.     int SUCCESS = 0; 
  21.  
  22.     boolean onStartContinuation(); 
  23.  
  24.     boolean onSaveData(IntentParams var1); 
  25.  
  26.     boolean onRestoreData(IntentParams var1); 
  27.  
  28.     void onCompleteContinuation(int var1); 
  29.  
  30.     default void onRemoteTerminated() { 
  31.         throw new RuntimeException("Stub!"); 
  32.     } 
  33.  
  34.     default void onFailedContinuation(int errorCode) { 
  35.         throw new RuntimeException("Stub!"); 
  36.     } 

 除了一些異常碼枚舉外,都是遷移中需要用到的主要接口,onStartContinuation()是遷移開始前的預(yù)處理函數(shù),可以在這加一些條件檢測,提示等。但是在開始請求遷移前,需要申請權(quán)限ohos.permission.DISTRIBUTED_DATASYNC。config.json中的配置如下:

config.json

  1. "reqPermissions": [ 
  2.     { 
  3.         "name""ohos.permission.DISTRIBUTED_DATASYNC" 
  4.     } 

接下來只需要PageAbility實(shí)現(xiàn)Ability中的onRequestPermissionsFromUserResult接口,就能在啟用遷移之前完成權(quán)限申請了。

  1. @Override 
  2. public void onRequestPermissionsFromUserResult(int requestCode, String[] permissions, int[] grantResults) { 
  3.     if (permissions == null || permissions.length == 0 || grantResults == null || grantResults.length == 0) { 
  4.         return
  5.     } 
  6.     if (requestCode == 0) { 
  7.         if (grantResults[0] == IBundleManager.PERMISSION_DENIED) { 
  8.             terminateAbility(); 
  9.         } 
  10.     } 

完成權(quán)限申請后,只需要通過事件來觸發(fā)遷移開關(guān)就行了??梢酝ㄟ^按鈕的點(diǎn)擊事件的來觸發(fā)遷移開關(guān)continueAbility(),如下:

  1. private void initComponents() { 
  2.     questionTextField = (TextField) findComponentById(ResourceTable.Id_question_content); 
  3.  
  4.     answerTextField = (TextField) findComponentById(ResourceTable.Id_answer_content); 
  5.  
  6.     findComponentById(ResourceTable.Id_send_button).setClickedListener(this::migrateAbility); 
  7.     findComponentById(ResourceTable.Id_return_button).setClickedListener(component->terminate()); 
  8.  
  9. private void migrateAbility(Component component) { 
  10.     String questionSend = questionTextField.getText(); 
  11.     String answerSend = answerTextField.getText(); 
  12.     if (questionSend.isEmpty() && answerSend.isEmpty()) { 
  13.         new ToastDialog(this).setText("Text can not be null").show(); 
  14.         return
  15.     } 
  16.  
  17.     try { 
  18.         continueAbility(); 
  19.     } catch (IllegalStateException illegalStateException) { 
  20.         HiLog.error(LABEL_LOG, "%{public}s""migrateAbility: IllegalStateException"); 
  21.     } 

最重要的兩個接口莫過于onSaveData、onRestoreData了,一個是在遷移的時候,將設(shè)備A的需要輸入的數(shù)據(jù)存儲,另一個是在設(shè)備B進(jìn)行遷移時,恢復(fù)數(shù)據(jù)。

  1. @Override 
  2. public boolean onSaveData(IntentParams intentParams) { 
  3.     intentParams.setParam(QUESTION_KEY, questionTextField.getText()); 
  4.     intentParams.setParam(ANSWER_KEY, answerTextField.getText()); 
  5.  
  6.     return true
  7.  
  8. @Override 
  9. public boolean onRestoreData(IntentParams intentParams) { 
  10.     if (intentParams.getParam(QUESTION_KEY) instanceof String) { 
  11.         questionText = (String) intentParams.getParam(QUESTION_KEY); 
  12.     } 
  13.  
  14.     if (intentParams.getParam(ANSWER_KEY) instanceof String) { 
  15.         answerText = (String) intentParams.getParam(ANSWER_KEY); 
  16.     } 
  17.  
  18.     if (!questionText.isEmpty() && ! answerText.isEmpty()) { 
  19.         AskRecordSlice.UpdateContent("Q:" + questionText + "\n"); 
  20.         AskRecordSlice.UpdateContent("A:" + answerText + "\n"); 
  21.     } 
  22.  
  23.     return true

其中的IntentParams是遷移的數(shù)據(jù)包,提供了setParam、getParam,來傳輸Key-Value數(shù)據(jù)。

設(shè)備B上只要正常運(yùn)行了onRestoreData后,那就會回調(diào)設(shè)備A上的onCompleteContinuation,表示遷移順利完成,否則回調(diào)onFailedContinuation,通過捕捉異常碼可進(jìn)行異常處理。而我在正常遷移完成后,進(jìn)行了問答記錄的本地存儲:

  1. @Override 
  2. public void onCompleteContinuation(int code) { 
  3.     questionText = questionTextField.getText(); 
  4.     answerText = answerTextField.getText(); 
  5.     if (!questionText.isEmpty() && ! answerText.isEmpty()) { 
  6.         AskRecordSlice.UpdateContent("Q:" + questionText + "\n"); 
  7.         AskRecordSlice.UpdateContent("A:" + answerText + "\n"); 
  8.     } 

 具體代碼

由于目錄樹中文件較多,整個工程文件的git路徑為:

https://gitee.com/baboon-chen/harmony-osexample.git

需要特殊注意的點(diǎn):

  1. //1 跨不同設(shè)備時,需要在配置文件中添加上支持的設(shè)備類型 config.json 
  2. "deviceType": [ 
  3.       "phone"
  4.       "tablet" 
  5.     ], 
  6. //2 要實(shí)現(xiàn)接口的類有哪些? 
  7. 一個應(yīng)用可能包含多個Page,都有自己的PageSlice棧。僅需要在支持遷移的Page中通過以下方法實(shí)現(xiàn)IAbilityContinuation接口。同時,此Page所包含的所有AbilitySlice也需要實(shí)現(xiàn)此接口。 

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

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

https://harmonyos.51cto.com

 

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

2021-08-12 10:02:08

鴻蒙HarmonyOS應(yīng)用

2021-01-06 11:21:56

鴻蒙HarmonyOS應(yīng)用開發(fā)

2021-06-16 15:18:03

鴻蒙HarmonyOS應(yīng)用

2010-11-30 16:21:15

Exchange遷移

2020-11-05 10:05:25

App

2022-05-19 15:47:24

碰一碰連接設(shè)備開發(fā)鴻蒙

2021-08-13 13:53:23

鴻蒙HarmonyOS應(yīng)用

2019-03-13 11:03:06

騰訊金融數(shù)據(jù)機(jī)房

2022-08-15 22:20:46

應(yīng)用開發(fā)華為IoT平臺

2021-11-03 09:51:45

鴻蒙HarmonyOS應(yīng)用

2011-03-09 10:21:35

2014-12-11 11:03:20

Qt跨平臺開發(fā)

2012-04-26 10:48:01

iOS開發(fā)互動廣告

2014-12-08 10:31:45

華為

2022-08-15 22:09:37

設(shè)備開發(fā)開發(fā)筆記

2021-08-17 10:20:14

鴻蒙HarmonyOS應(yīng)用

2018-06-03 09:43:47

iOSAndroid谷歌

2014-10-15 16:11:16

易信互動微訪談

2022-08-27 15:23:52

開發(fā)者大會谷歌Android 應(yīng)用

2010-03-09 09:49:01

Oracle跨平臺遷移
點(diǎn)贊
收藏

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