1# 拉起航班类应用(startAbilityByType) 2 3<!--Kit: Ability Kit--> 4<!--Subsystem: AGC--> 5<!--Owner: @liusu23--> 6<!--Designer: @xukeke--> 7<!--Tester: @lusq--> 8<!--Adviser: @huipeizi--> 9 10本章节介绍如何拉起航班类应用扩展面板。 11 12例如,在行程安排类App中,当用户记录了某次行程的航班号,应用能够识别航班号信息并提供航班动态查询的链接。用户点击链接后,应用将通过调用[UIAbilityContext.startAbilityByType](../reference/apis-ability-kit/js-apis-inner-application-uiAbilityContext.md#startabilitybytype11)或[UIExtensionContentSession.startAbilityByType](../reference/apis-ability-kit/js-apis-app-ability-uiExtensionContentSession.md#startabilitybytype11)接口,拉起航班类应用的扩展面板。面板上将展示设备上所有支持航班查询的应用,供用户选择并跳转至所需应用。 13 14## 航班类应用扩展面板参数说明 15 16startAbilityByType接口中type字段为flight,支持按航班号查询、按起降地查询两种意图场景,对应的wantParam参数如下: 17 18- 按航班号查询场景 19 20 | 参数名 | 类型 | 必填 | 说明 | 21 | ------------- | ------ | ---- | ------------------------------------------------------------ | 22 | sceneType | number | 否 | 意图场景,表明本次请求对应的操作意图。默认为1,按航班号查询场景填1或不填。 | 23 | flightNo | string | 是 | 航班号,航司二位代码+数字。 | 24 | departureDate | string | 否 | 航班出发时间:YYYY-MM-DD。 | 25 26- 按起降地查询场景 27 28 | 参数名 | 类型 | 必填 | 说明 | 29 | -------------------- | ---------------------- | ---- | -------------------------------------------------------- | 30 | sceneType | number | 是 | 意图场景,表明本次请求对应的操作意图。按起降地查询场景填2。 | 31 | originLocation | string | 是 | 出发地。 | 32 | destinationLocation | string | 是 | 目的地。 | 33 | departureDate | string | 否 | 航班出发时间:YYYY-MM-DD。 | 34 35 36## 拉起方开发步骤 37 381. 导入相关模块。 39 ```ts 40 import { common } from '@kit.AbilityKit'; 41 ``` 42 432. 构造接口参数并调用startAbilityByType接口。 44 45 ```ts 46 @Entry 47 @Component 48 struct Index { 49 @State hideAbility: string = 'hideAbility' 50 51 build() { 52 Row() { 53 Column() { 54 Text(this.hideAbility) 55 .fontSize(30) 56 .fontWeight(FontWeight.Bold) 57 .onClick(() => { 58 let context = this.getUIContext().getHostContext() as common.UIAbilityContext; 59 let wantParam: Record<string, Object> = { 60 'sceneType': 1, 61 'flightNo': 'ZH1509', 62 'departureDate': '2024-10-01' 63 }; 64 let abilityStartCallback: common.AbilityStartCallback = { 65 onError: (code: number, name: string, message: string) => { 66 console.error(`onError code ${code} name: ${name} message: ${message}`); 67 }, 68 onResult: (result) => { 69 console.info(`onResult result: ${JSON.stringify(result)}`); 70 } 71 } 72 73 context.startAbilityByType("flight", wantParam, abilityStartCallback, 74 (err) => { 75 if (err) { 76 console.error(`startAbilityByType fail, err: ${JSON.stringify(err)}`); 77 } else { 78 console.info(`success`); 79 } 80 }); 81 }); 82 } 83 .width('100%') 84 } 85 .height('100%') 86 } 87 } 88 ``` 89 效果示例图: 90  91 92## 目标方开发步骤 93 941. 在module.json5中配置[uris](../quick-start/module-configuration-file.md#skills标签): 95 1. 设置linkFeature属性以声明当前应用支持的特性功能,从而系统可以从设备已安装应用中找到当前支持该特性的应用,取值范围如下: 96 | 取值 | 含义 | 97 | ----------- | ------------------------ | 98 | QueryByFlightNo | 声明应用支持按航班号查询航班。 | 99 | QueryByLocation | 声明应用支持按起降地查询航班。 | 100 2. 设置scheme、host、port、path/pathStartWith属性,与Want中URI相匹配,以便区分不同功能。 101 ```json 102 { 103 "abilities": [ 104 { 105 "skills": [ 106 { 107 "uris": [ 108 { 109 "scheme": "flight", 110 "host": "queryByFlightNo", 111 "path": "", 112 "linkFeature": "QueryByFlightNo" 113 }, 114 { 115 "scheme": "flight", 116 "host": "queryByLocation", 117 "path": "", 118 "linkFeature": "QueryByLocation" 119 } 120 ] 121 } 122 ] 123 } 124 ] 125 } 126 ``` 127 1282. 解析参数并做对应处理。 129 130 ```ts 131 UIAbility.onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void 132 ``` 133 134 在参数**want.uri**中会携带目标方配置的linkFeature对应的uri。 135 136 在参数**want.parameters**中会携带Caller方传入的参数,不同场景参数如下所示 137 138 - 按航班号查询场景 139 140 | 参数名 | 类型 | 必填 | 说明 | 141 | -------------------- | ------ | ---- | ---------------------------------------------------- | 142 | flightNo | string | 是 | 航班号,航司二位代码+数字。 | 143 | departureDate | string | 否 | 航班出发时间:YYYY-MM-DD。不填时,Target可按当天处理。 | 144 145 - 按起降地查询场景 146 147 | 参数名 | 类型 | 必填 | 说明 | 148 | -------------------- | ------ | ---- | -------------------------------------------------- | 149 | originLocation | string | 是 | 出发地。 | 150 | destinationLocation | string | 是 | 目的地。 | 151 | departureDate | string | 否 | 航班出发时间:YYYY-MM-DD。不填时,Target可按当天处理。 | 152 153 应用可根据[linkFeature](../quick-start/module-configuration-file.md#skills标签)中定义的特性功能,比如按航班号查询和按起降地查询,结合接收到的uri和参数开发不同的样式页面。 154 155**完整示例:** 156 157```ts 158import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 159import { hilog } from '@kit.PerformanceAnalysisKit'; 160import { window } from '@kit.ArkUI'; 161 162const TAG = 'EntryAbility'; 163 164export default class EntryAbility extends UIAbility { 165 windowStage: window.WindowStage | null = null; 166 167 uri?: string; 168 flightNo?: string; 169 departureDate?: string; 170 originLocation?: string; 171 destinationLocation?: string; 172 173 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 174 hilog.info(0x0000, TAG, `onCreate, want=${JSON.stringify(want)}`); 175 super.onCreate(want, launchParam); 176 this.parseWant(want); 177 } 178 179 onNewWant(want: Want, launchParam: AbilityConstant.LaunchParam): void { 180 hilog.info(0x0000, TAG, `onNewWant, want=${JSON.stringify(want)}`); 181 super.onNewWant(want, launchParam); 182 this.parseWant(want); 183 if (!this.windowStage) { 184 hilog.error(0x0000, TAG, 'windowStage is null'); 185 this.context.terminateSelf(); 186 return; 187 } 188 this.loadPage(this.windowStage); 189 } 190 191 private parseWant(want: Want): void { 192 this.uri = want.uri as string | undefined; 193 this.flightNo = want.parameters?.flightNo as string | undefined; 194 this.departureDate = want.parameters?.departureDate as string | undefined; 195 this.originLocation = want.parameters?.originLocation as string | undefined; 196 this.destinationLocation = want.parameters?.destinationLocation as string | undefined; 197 } 198 199 private loadPage(windowStage: window.WindowStage): void { 200 hilog.info(0x0000, TAG, `loadPage, uri=${this.uri}`); 201 if (this.uri === 'flight://queryByFlightNo') { 202 // 构建按航班号查询场景参数 203 const storage: LocalStorage = new LocalStorage({ 204 "flightNo": this.flightNo, 205 "departureDate": this.departureDate 206 } as Record<string, Object>); 207 // 拉起按航班号查询页面 208 windowStage.loadContent('pages/QueryByFlightNoPage', storage) 209 } else if (this.uri === 'flight://queryByLocation') { 210 // 构建按起降地查询场景参数 211 const storage: LocalStorage = new LocalStorage({ 212 "originLocation": this.originLocation, 213 "destinationLocation": this.destinationLocation, 214 "departureDate": this.departureDate 215 } as Record<string, Object>); 216 // 拉起按起降地查询页面 217 windowStage.loadContent('pages/QueryByLocationPage', storage) 218 } else { 219 // 默认拉起首页 220 windowStage.loadContent('pages/Index', (err) => { 221 if (err.code) { 222 hilog.error(0x0000, TAG, 'Failed to load the content. Cause: %{public}s', 223 JSON.stringify(err) ?? ''); 224 return; 225 } 226 hilog.info(0x0000, TAG, 'Succeeded in loading the content.'); 227 }); 228 } 229 } 230 231 onDestroy(): void { 232 hilog.info(0x0000, TAG, `onDestroy`); 233 } 234 235 onWindowStageCreate(windowStage: window.WindowStage): void { 236 hilog.info(0x0000, TAG, `onWindowStageCreate`); 237 this.windowStage = windowStage; 238 this.loadPage(this.windowStage); 239 } 240 241 onWindowStageDestroy(): void { 242 hilog.info(0x0000, TAG, '%{public}s', 'Ability onWindowStageDestroy'); 243 } 244 245 onForeground(): void { 246 hilog.info(0x0000, TAG, '%{public}s', 'Ability onForeground'); 247 } 248 249 onBackground(): void { 250 hilog.info(0x0000, TAG, '%{public}s', 'Ability onBackground'); 251 } 252} 253```