• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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    @Entry
40    @Component
41    struct Index {
42        @State hideAbility: string = 'hideAbility'
43
44        build() {
45            Row() {
46                Column() {
47                    Text(this.hideAbility)
48                        .fontSize(30)
49                        .fontWeight(FontWeight.Bold)
50                        .onClick(() => {
51                            let context = this.getUIContext().getHostContext() as common.UIAbilityContext;
52                            let wantParam: Record<string, Object> = {
53                                'sceneType': 1,
54                                'flightNo': 'ZH1509',
55                                'departureDate': '2024-10-01'
56                            };
57                            let abilityStartCallback: common.AbilityStartCallback = {
58                                onError: (code: number, name: string, message: string) => {
59                                    console.log(`onError code ${code} name: ${name} message: ${message}`);
60                                },
61                                onResult: (result) => {
62                                    console.log(`onResult result: ${JSON.stringify(result)}`);
63                                }
64                            }
65
66                            context.startAbilityByType("flight", wantParam, abilityStartCallback,
67                                (err) => {
68                                    if (err) {
69                                    	console.error(`startAbilityByType fail, err: ${JSON.stringify(err)}`);
70                                    } else {
71                                    	console.log(`success`);
72                                    }
73                                });
74                        });
75                }
76                .width('100%')
77            }
78            .height('100%')
79        }
80    }
81    ```
82    效果示例图:
83    ![效果示例图](./figures/start-flight-panel.png)
84
85## 目标方开发步骤
86
871. 在module.json5中配置[uris](../quick-start/module-configuration-file.md#skills标签):
88    1. 设置linkFeature属性以声明当前应用支持的特性功能,从而系统可以从设备已安装应用中找到当前支持该特性的应用,取值范围如下:
89        | 取值        | 含义                     |
90        | ----------- | ------------------------ |
91        | QueryByFlightNo  | 声明应用支持按航班号查询航班。     |
92        | QueryByLocation   | 声明应用支持按起降地查询航班。 |
93    2. 设置scheme、host、port、path/pathStartWith属性,与Want中URI相匹配,以便区分不同功能。
94        ```json
95        {
96            "abilities": [
97                {
98                    "skills": [
99                        {
100                            "uris": [
101                                {
102                                    "scheme": "flight",
103                                    "host": "queryByFlightNo",
104                                    "path": "",
105                                    "linkFeature": "QueryByFlightNo"
106                                },
107                                {
108                                    "scheme": "flight",
109                                    "host": "queryByLocation",
110                                    "path": "",
111                                    "linkFeature": "QueryByLocation"
112                                }
113                            ]
114                        }
115                    ]
116                }
117            ]
118        }
119        ```
120
1212. 解析参数并做对应处理。
122
123    ```ts
124    UIAbility.onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void
125    ```
126
127    在参数**want.uri**中会携带目标方配置的linkFeature对应的uri;
128
129    在参数**want.parameters**中会携带Caller方传入的参数,不同场景参数如下所示
130
131    - 按航班号查询场景
132
133        | 参数名               | 类型   | 必填 | 说明                                                 |
134        | -------------------- | ------ | ---- | ---------------------------------------------------- |
135        | flightNo           | string | 是   | 航班号,航司二位代码+数字。                                             |
136        | departureDate       | string | 否   | 航班出发时间:YYYY-MM-DD。不填时,Target可按当天处理。                                             |
137
138    - 按起降地查询场景
139
140        | 参数名               | 类型   | 必填 | 说明                                               |
141        | -------------------- | ------ | ---- | -------------------------------------------------- |
142        | originLocation      | string | 是   | 出发地。                                           |
143        | destinationLocation  | string  | 是   | 目的地。                                           |
144        | departureDate | string  | 否   | 航班出发时间:YYYY-MM-DD。不填时,Target可按当天处理。    |
145
146    应用可根据[linkFeature](../quick-start/module-configuration-file.md#skills标签)中定义的特性功能,比如按航班号查询和按起降地查询,结合接收到的uri和参数开发不同的样式页面。
147
148**完整示例:**
149
150```ts
151import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit';
152import { hilog } from '@kit.PerformanceAnalysisKit';
153import { window } from '@kit.ArkUI';
154
155const TAG = 'EntryAbility'
156
157export default class EntryAbility extends UIAbility {
158    windowStage: window.WindowStage | null = null;
159
160    uri?: string;
161    flightNo?: string;
162    departureDate?: string;
163    originLocation?: string;
164    destinationLocation?: string;
165
166    onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
167        hilog.info(0x0000, TAG, `onCreate, want=${JSON.stringify(want)}`);
168        super.onCreate(want, launchParam);
169        this.parseWant(want);
170    }
171
172    onNewWant(want: Want, launchParam: AbilityConstant.LaunchParam): void {
173        hilog.info(0x0000, TAG, `onNewWant, want=${JSON.stringify(want)}`);
174        super.onNewWant(want, launchParam);
175        this.parseWant(want);
176        if (!this.windowStage) {
177            hilog.error(0x0000, TAG, 'windowStage is null');
178            this.context.terminateSelf();
179            return;
180        }
181        this.loadPage(this.windowStage);
182    }
183
184    private parseWant(want: Want): void {
185        this.uri = want.uri as string | undefined;
186        this.flightNo = want.parameters?.flightNo as string | undefined;
187        this.departureDate = want.parameters?.departureDate as string | undefined;
188        this.originLocation = want.parameters?.originLocation as string | undefined;
189        this.destinationLocation = want.parameters?.destinationLocation as string | undefined;
190    }
191
192    private loadPage(windowStage: window.WindowStage): void {
193        hilog.info(0x0000, TAG, `loadPage, uri=${this.uri}`);
194        if (this.uri === 'flight://queryByFlightNo') {
195            // 构建按航班号查询场景参数
196            const storage: LocalStorage = new LocalStorage({
197                "flightNo": this.flightNo,
198                "departureDate": this.departureDate
199            } as Record<string, Object>);
200            // 拉起按航班号查询页面
201            windowStage.loadContent('pages/QueryByFlightNoPage', storage)
202        } else if (this.uri === 'flight://queryByLocation') {
203            // 构建按起降地查询场景参数
204            const storage: LocalStorage = new LocalStorage({
205                "originLocation": this.originLocation,
206                "destinationLocation": this.destinationLocation,
207                "departureDate": this.departureDate
208            } as Record<string, Object>);
209            // 拉起按起降地查询页面
210            windowStage.loadContent('pages/QueryByLocationPage', storage)
211        } else {
212            // 默认拉起首页
213            windowStage.loadContent('pages/Index', (err) => {
214                if (err.code) {
215                    hilog.error(0x0000, TAG, 'Failed to load the content. Cause: %{public}s',
216                        JSON.stringify(err) ?? '');
217                    return;
218                }
219                hilog.info(0x0000, TAG, 'Succeeded in loading the content.');
220            });
221        }
222    }
223
224    onDestroy(): void {
225        hilog.info(0x0000, TAG, `onDestroy`);
226    }
227
228    onWindowStageCreate(windowStage: window.WindowStage): void {
229        hilog.info(0x0000, TAG, `onWindowStageCreate`);
230        this.windowStage = windowStage;
231        this.loadPage(this.windowStage);
232    }
233
234    onWindowStageDestroy(): void {
235        hilog.info(0x0000, TAG, '%{public}s', 'Ability onWindowStageDestroy');
236    }
237
238    onForeground(): void {
239        hilog.info(0x0000, TAG, '%{public}s', 'Ability onForeground');
240    }
241
242    onBackground(): void {
243        hilog.info(0x0000, TAG, '%{public}s', 'Ability onBackground');
244    }
245}
246```