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