鴻蒙HarmonyOS實戰-Stage模型(資訊傳遞載體Want)

蜀道山QAQ發表於2024-05-24

🚀前言

應用中的資訊傳遞是為了實現各種功能和互動。資訊傳遞可以幫助使用者和應用之間進行有效的溝通和交流。透過資訊傳遞,應用可以向使用者傳遞重要的訊息、通知和提示,以提供及時的反饋和指導。同時,使用者也可以透過資訊傳遞向應用傳送指令、請求和反饋,以實現個性化的需求和操作。

資訊傳遞還可以幫助應用之間實現資料的共享和互動。透過資訊傳遞,不同應用可以實現資料的互通,以實現更多的功能和服務。例如,一個購物應用可以透過資訊傳遞與支付應用進行資料互動,以實現支付功能;一個社交媒體應用可以透過資訊傳遞與地圖應用進行資料互動,以實現位置分享功能。

此外,資訊傳遞還可以幫助應用之間實現聯動和協作。透過資訊傳遞,應用可以實現多個功能的組合和協同,以提供更加豐富和綜合的服務。例如,一個音樂應用可以與鬧鐘應用進行資訊傳遞,以實現在特定時間播放特定的音樂。

🚀一、資訊傳遞載體Want

🔎1.Want概述

🦋1.1 Want的定義與用途

HarmonyOS中的"Want"是一個用於定義和控制應用程式之間通訊的基本概念。它可以用來描述一個應用程式對某個特定操作的需求或意願,比如獲取某個裝置的位置資訊、訪問某個感測器的資料等。

使用"Want"可以實現應用程式之間的無縫協作和互操作。透過定義和使用"Wants",應用程式可以根據自身的需求傳送請求,並且可以接收和處理其他應用程式傳送的請求。這種機制能夠促進應用程式之間的互動和共享,並且使得整個系統更加智慧和高效。

"Wants"的使用可以帶來許多好處。首先,它可以簡化應用程式之間的通訊和協作,減少開發人員的工作量。其次,它可以增強系統的靈活性和可擴充套件性,使得應用程式能夠動態地適應不同的環境和裝置。最後,它可以提供更加個性化和智慧化的使用者體驗,使得應用程式能夠更好地理解使用者的需求並作出相應的反應。

image

🦋1.2 Want的型別

在HarmonyOS中,資訊傳遞載體Want的型別可以分為兩種:顯式Want和隱式Want。

顯式Want:顯式Want是指明確指定要操作的元件或服務的Want。透過顯式Want,可以精確地指定要傳遞給目標元件或服務的資訊,並指定具體的要執行的操作。顯式Want會包含元件名和操作型別等明確的指令資訊。例如,可以使用顯式Want來啟動指定的Activity或呼叫指定的服務。

let wantInfo = {
    deviceId: '', // deviceId為空表示本裝置
    bundleName: 'com.example.myapplication',
    abilityName: 'FuncAbility',
}

隱式Want:隱式Want是指不明確指定要操作的元件或服務的Want,而是根據一定的規則和條件來進行匹配。透過隱式Want,可以實現元件之間的解耦和靈活性。隱式Want一般包含一組動作、類別、資料型別等條件,系統會根據這些條件來匹配合適的元件或服務。例如,可以使用隱式Want來處理某個特定型別的資料或根據某個特定的動作執行相應的操作。

let wantInfo = {
    // uncomment line below if wish to implicitly query only in the specific bundle.
    // bundleName: 'com.example.myapplication',
    action: 'ohos.want.action.search',
    // entities can be omitted
    entities: [ 'entity.system.browsable' ],
    uri: 'https://www.test.com:8080/query/student',
    type: 'text/plain',
};

🔎2.顯式Want與隱式Want匹配規則

🦋2.1 隱式Want匹配原理詳解

系統將呼叫方傳入的want引數(包含action、entities、uri和type屬性)與已安裝待匹配的應用Ability的skills配置(包含actions、entities、uris和type屬性)依次進行匹配。當四個屬性匹配均透過,則此應用才會被應用選擇器展示給使用者進行選擇。

☀️2.1.1 want引數的action匹配規則

image

image

☀️2.1.2 want引數的entities匹配規則

image

image

☀️2.1.3 want引數的uri和type匹配規則

image

image

image

☀️2.1.4 uri匹配規則

以下是根據給定匹配規則展示的表格:

image

待匹配Ability的skills配置的uris中scheme、host、port、path、pathStartWith和pathRegex屬性拼接,如果依次宣告瞭path、pathStartWith和pathRegex屬性時,uris將分別拼接為如下三種表示式:

  • 全路徑表示式:scheme://host:port/path

  • 字首表示式:scheme://host:port/pathStartWith

  • 正規表示式:scheme://host:port/pathRegex

☀️2.1.5 type匹配規則

image

🔎3.常見action與entities

🦋3.1 action

表示呼叫方要執行的通用操作(如檢視、分享、應用詳情)

image

🦋3.2 entities

表示目標Ability的類別資訊(如瀏覽器、影片播放器)

image

🔎4.使用顯式Want啟動Ability

1、啟動方

新建callerAbility
image

2、被啟動方

同理新建calleeAbility
image

3、啟動方UI

import common from '@ohos.app.ability.common';
@Entry
@Component
struct Index {
  @State message: string = 'callerAbility'

  build() {
    Row() {
      Column() {
        Text('hello')
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
        // A new button with will call explicitStartAbility() when clicked.
        Button("CLICKME")
          .onClick(this.explicitStartAbility) // explicitStartAbility見下面示例程式碼
        // ...
      }
      .width('100%')
    }
    .height('100%')
  }
  async explicitStartAbility() {
    try {
      // Explicit want with abilityName specified.
      let want = {
        deviceId: "",
        bundleName: "com.example.myapplication",
        abilityName: "calleeAbility"
      };
      let context = getContext(this) as common.UIAbilityContext;
      await context.startAbility(want);
      console.info(`explicit start ability succeed`);
    } catch (error) {
      console.info(`explicit start ability failed with ${error.code}`);
    }
  }
}

4、執行

image

🔎5.使用隱式Want開啟網址

1、module.json5配置

"skills": [
  {
    "entities": [
      "entity.system.browsable"
      // ...
    ],
    "actions": [
        "ohos.want.action.viewData"
        // ...
    ],
    "uris": [
      {
        "scheme": "https",
        "host": "www.test.com",
        "port": "8080",
        // prefix matching
        "pathStartWith": "query",
        "type": "text/*"
      },
      {
        "scheme": "http",
        // ...
      }
      // ...
    ]
  },
]

image

2、定義跳轉函式

async implicitStartAbility() {
    try {
        let want = {
            // uncomment line below if wish to implicitly query only in the specific bundle.
            // bundleName: "com.example.myapplication",
            "action": "ohos.want.action.viewData",
            // entities can be omitted.
            "entities": [ "entity.system.browsable" ],
            "uri": "https://www.test.com:8080/query/student",
            "type": "text/plain"
        }
        let context = getContext(this) as common.UIAbilityContext;
        await context.startAbility(want)
        console.info(`explicit start ability succeed`)
    } catch (error) {
        console.info(`explicit start ability failed with ${error.code}`)
    }
 }

image

image

3、執行

image

🔎6.應用間使用Want分享資料

1、分享方

讀取檔案

import fileIO from '@ohos.fileio';

// let path = ...
// file open where path is a variable contains the file path.
let fileFd = fileIO.openSync(path, 0o102, 0o666);

傳輸檔案資訊構造

import wantConstant from '@ohos.ability.wantConstant';

// let path = ...
// let fileFd = ...
// let fileSize = ...
let want = {
    // This action is used to implicitly match the application selctor.
    action: wantConstant.Action.ACTION_SELECT,
    // This is the custom parameter in the first layer of want
    // which is intended to add info to application selector.
    parameters: {
        // The MIME type of pdf
        "ability.picker.type": "application/pdf",
        "ability.picker.fileNames": [path],
        "ability.picker.fileSizes": [fileSize],
        // This a nested want which will be directly send to the user selected application.         
        "ability.want.params.INTENT": {
            "action": "ohos.want.action.sendData",
            "type": "application/pdf",
            "parameters": {
               "keyFd": {"type": "FD", "value": fileFd}
            }
        }
    }
}

image

image

2、被分享方

定義skills

"skills": [
  {
    "entities": [
      // ...
    ],
    "actions": [
        "ohos.want.action.sendData"
        // ...
    ],
    "uris": [
      {
        "type": "application/pdf"
      },
      // ...
    ]
  },
]

2、接收資料

onCreate(want, launchParam) {
  // note when keyFd is undefined, app crash will happen.
  if (want["parameters"]["keyFd"] !== undefined) {
    // receive file descriptor
    let fd = want["parameters"]["keyFd"].value;
    // ...
  }
}

🚀寫在最後

  • 如果你覺得這篇內容對你還蠻有幫助,我想邀請你幫我三個小忙:
  • 點贊,轉發,有你們的 『點贊和評論』,才是我創造的動力。
  • 關注小編,同時可以期待後續文章ing🚀,不定期分享原創知識。
  • 更多鴻蒙最新技術知識點,請關注作者部落格:https://t.doruo.cn/14DjR1rEY

image

相關文章