harmony 鸿蒙拉起航班类应用(startAbilityByType)
拉起航班类应用(startAbilityByType)
本章节介绍如何拉起航班类应用扩展面板。
例如,在行程安排类App中,当用户记录了某次行程的航班号,应用能够识别航班号信息并提供航班动态查询的链接。用户点击链接后,应用将通过调用UIAbilityContext.startAbilityByType或UIExtensionContentSession.startAbilityByType接口,拉起航班类应用的扩展面板。面板上将展示设备上所有支持航班查询的应用,供用户选择并跳转至所需应用。
航班类应用扩展面板参数说明
startAbilityByType接口中type字段为flight,支持按航班号查询、按起降地查询两种意图场景,对应的wantParam参数如下:
- 按航班号查询场景
|参数名 |类型 |必填|说明 | |————-|——|—-|————————————————————| |sceneType |number|否 |意图场景,表明本次请求对应的操作意图。默认为1,按航班号查询场景填1或不填。 | |flightNo |string|是 |航班号,航司二位代码+数字。| |departureDate|string|否 |航班出发时间:YYYY-MM-DD。 |
- 按起降地查询场景
|参数名 |类型 |必填|说明 | |——————–|———————-|—-|——————————————————–| |sceneType |number |是 |意图场景,表明本次请求对应的操作意图。按起降地查询场景填2。 | |originLocation |string |是 |出发地。 | |destinationLocation|string |是 |目的地。 | |departureDate|string |否 |航班出发时间:YYYY-MM-DD。 |
拉起方开发步骤
导入相关模块。
import { common } from '@kit.AbilityKit';
构造接口参数并调用startAbilityByType接口。
@Entry @Component struct Index { @State hideAbility: string = 'hideAbility' build() { Row() { Column() { Text(this.hideAbility) .fontSize(30) .fontWeight(FontWeight.Bold) .onClick(() => { let context = this.getUIContext().getHostContext() as common.UIAbilityContext; let wantParam: Record<string, Object> = { 'sceneType': 1, 'flightNo': 'ZH1509', 'departureDate': '2024-10-01' }; let abilityStartCallback: common.AbilityStartCallback = { onError: (code: number, name: string, message: string) => { console.log(`onError code ${code} name: ${name} message: ${message}`); }, onResult: (result) => { console.log(`onResult result: ${JSON.stringify(result)}`); } } context.startAbilityByType("flight", wantParam, abilityStartCallback, (err) => { if (err) { console.error(`startAbilityByType fail, err: ${JSON.stringify(err)}`); } else { console.log(`success`); } }); }); } .width('100%') } .height('100%') } }
效果示例图:
目标方开发步骤
在module.json5中配置uris:
- 设置linkFeature属性以声明当前应用支持的特性功能,从而系统可以从设备已安装应用中找到当前支持该特性的应用,取值范围如下: |取值 |含义 | |———–|————————| |QueryByFlightNo|声明应用支持按航班号查询航班。 | |QueryByLocation |声明应用支持按起降地查询航班。|
- 设置scheme、host、port、path/pathStartWith属性,与Want中URI相匹配,以便区分不同功能。
json { "abilities": [ { "skills": [ { "uris": [ { "scheme": "flight", "host": "queryByFlightNo", "path": "", "linkFeature": "QueryByFlightNo" }, { "scheme": "flight", "host": "queryByLocation", "path": "", "linkFeature": "QueryByLocation" } ] } ] } ] }
解析参数并做对应处理。
UIAbility.onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void
在参数want.uri中会携带目标方配置的linkFeature对应的uri;
在参数want.parameters中会携带Caller方传入的参数,不同场景参数如下所示
- 按航班号查询场景
|参数名 |类型 |必填|说明 | |——————–|——|—-|—————————————————-| |flightNo |string|是 |航班号,航司二位代码+数字。 | |departureDate |string|否 |航班出发时间:YYYY-MM-DD。不填时,Target可按当天处理。 |
- 按起降地查询场景
|参数名 |类型 |必填|说明 | |——————–|——|—-|————————————————–| |originLocation |string|是 |出发地。 | |destinationLocation|string|是 |目的地。 | |departureDate|string|否 |航班出发时间:YYYY-MM-DD。不填时,Target可按当天处理。 |
应用可根据linkFeature中定义的特性功能,比如按航班号查询和按起降地查询,结合接收到的uri和参数开发不同的样式页面。
完整示例:
import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { window } from '@kit.ArkUI';
const TAG = 'EntryAbility'
export default class EntryAbility extends UIAbility {
windowStage: window.WindowStage|null = null;
uri?: string;
flightNo?: string;
departureDate?: string;
originLocation?: string;
destinationLocation?: string;
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
hilog.info(0x0000, TAG, `onCreate, want=${JSON.stringify(want)}`);
super.onCreate(want, launchParam);
this.parseWant(want);
}
onNewWant(want: Want, launchParam: AbilityConstant.LaunchParam): void {
hilog.info(0x0000, TAG, `onNewWant, want=${JSON.stringify(want)}`);
super.onNewWant(want, launchParam);
this.parseWant(want);
if (!this.windowStage) {
hilog.error(0x0000, TAG, 'windowStage is null');
this.context.terminateSelf();
return;
}
this.loadPage(this.windowStage);
}
private parseWant(want: Want): void {
this.uri = want.uri as string|undefined;
this.flightNo = want.parameters?.flightNo as string|undefined;
this.departureDate = want.parameters?.departureDate as string|undefined;
this.originLocation = want.parameters?.originLocation as string|undefined;
this.destinationLocation = want.parameters?.destinationLocation as string|undefined;
}
private loadPage(windowStage: window.WindowStage): void {
hilog.info(0x0000, TAG, `loadPage, uri=${this.uri}`);
if (this.uri === 'flight://queryByFlightNo') {
// 构建按航班号查询场景参数
const storage: LocalStorage = new LocalStorage({
"flightNo": this.flightNo,
"departureDate": this.departureDate
} as Record<string, Object>);
// 拉起按航班号查询页面
windowStage.loadContent('pages/QueryByFlightNoPage', storage)
} else if (this.uri === 'flight://queryByLocation') {
// 构建按起降地查询场景参数
const storage: LocalStorage = new LocalStorage({
"originLocation": this.originLocation,
"destinationLocation": this.destinationLocation,
"departureDate": this.departureDate
} as Record<string, Object>);
// 拉起按起降地查询页面
windowStage.loadContent('pages/QueryByLocationPage', storage)
} else {
// 默认拉起首页
windowStage.loadContent('pages/Index', (err) => {
if (err.code) {
hilog.error(0x0000, TAG, 'Failed to load the content. Cause: %{public}s',
JSON.stringify(err) ?? '');
return;
}
hilog.info(0x0000, TAG, 'Succeeded in loading the content.');
});
}
}
onDestroy(): void {
hilog.info(0x0000, TAG, `onDestroy`);
}
onWindowStageCreate(windowStage: window.WindowStage): void {
hilog.info(0x0000, TAG, `onWindowStageCreate`);
this.windowStage = windowStage;
this.loadPage(this.windowStage);
}
onWindowStageDestroy(): void {
hilog.info(0x0000, TAG, '%{public}s', 'Ability onWindowStageDestroy');
}
onForeground(): void {
hilog.info(0x0000, TAG, '%{public}s', 'Ability onForeground');
}
onBackground(): void {
hilog.info(0x0000, TAG, '%{public}s', 'Ability onBackground');
}
}
你可能感兴趣的鸿蒙文章
harmony 鸿蒙FA模型访问Stage模型DataShareExtensionAbility
- 所属分类: 后端技术
- 本文标签:
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦