• 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字段为express,支持查询快递意图,对应的wantParam参数如下:
10
11
12| 参数名    | 类型   | 必填 | 说明                                   |
13| --------- | ------ | ---- | -------------------------------------- |
14| sceneType | number | 否   | 意图场景,表明本次请求对应的操作意图。默认为1,查询快递填场景填1或不填。 |
15| expressNo | string | 是   | 快递单号。                               |
16
17
18## 拉起方开发步骤
19
201. 导入相关模块。
21    ```ts
22    import { common } from '@kit.AbilityKit';
23    ```
242. 构造接口参数并调用startAbilityByType接口。
25
26    ```ts
27    @Entry
28    @Component
29    struct Index {
30        @State hideAbility: string = 'hideAbility'
31
32        build() {
33            Row() {
34                Column() {
35                    Text(this.hideAbility)
36                    .fontSize(30)
37                    .fontWeight(FontWeight.Bold)
38                        .onClick(() => {
39                            let context = this.getUIContext().getHostContext() as common.UIAbilityContext;
40                            let wantParam: Record<string, Object> = {
41                                'sceneType': 1,
42                                'expressNo': 'SF123456'
43                            };
44                            let abilityStartCallback: common.AbilityStartCallback = {
45                                onError: (code: number, name: string, message: string) => {
46                                    console.log(`onError code ${code} name: ${name} message: ${message}`);
47                                },
48                                onResult: (result) => {
49                                    console.log(`onResult result: ${JSON.stringify(result)}`);
50                                }
51                            }
52
53                            context.startAbilityByType("express", wantParam, abilityStartCallback,
54                                (err) => {
55                                    if (err) {
56                                        console.error(`startAbilityByType fail, err: ${JSON.stringify(err)}`);
57                                    } else {
58                                        console.log(`success`);
59                                    }
60                                });
61                        });
62                }
63                .width('100%')
64            }
65            .height('100%')
66        }
67    }
68    ```
69
70    效果示例图:
71    ![效果示例图](./figures/start-express-panel.png)
72
73## 目标方开发步骤
74
751. 在module.json5中配置[uris](../quick-start/module-configuration-file.md#skills标签):
76    1. 设置linkFeature属性以声明当前应用支持的特性功能,从而系统可以从设备已安装应用中找到当前支持该特性的应用,取值范围如下:
77        | 取值         | 含义                 |
78        | ------------ | -------------------- |
79        | QueryExpress | 声明应用支持快递查询。 |
80    2. 设置scheme、host、port、path/pathStartWith属性,与Want中URI相匹配,以便区分不同功能。
81        ```json
82        {
83            "abilities": [
84                {
85                    "skills": [
86                        {
87                            "uris": [
88                                {
89                                    "scheme": "express",
90                                    "host": "queryExpress",
91                                    "path": "",
92                                    "linkFeature": "QueryExpress"
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    | expressNo | string | 是   | 快递单号。 |
115
116
117
118**完整示例:**
119
120```ts
121import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit';
122import { hilog } from '@kit.PerformanceAnalysisKit';
123import { window } from '@kit.ArkUI';
124
125const TAG = 'EntryAbility'
126
127export default class EntryAbility extends UIAbility {
128    windowStage: window.WindowStage | null = null;
129
130    uri?: string;
131    expressNo?: string;
132
133    onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
134        hilog.info(0x0000, TAG, `onCreate, want=${JSON.stringify(want)}`);
135        super.onCreate(want, launchParam);
136        this.parseWant(want);
137    }
138
139    onNewWant(want: Want, launchParam: AbilityConstant.LaunchParam): void {
140        hilog.info(0x0000, TAG, `onNewWant, want=${JSON.stringify(want)}`);
141        super.onNewWant(want, launchParam);
142        this.parseWant(want);
143        if (!this.windowStage) {
144            hilog.error(0x0000, TAG, 'windowStage is null');
145            this.context.terminateSelf();
146            return;
147        }
148        this.loadPage(this.windowStage);
149    }
150
151    private parseWant(want: Want): void {
152        this.uri = want.uri as string | undefined;
153        this.expressNo = want.parameters?.expressNo as string | undefined;
154    }
155
156    private loadPage(windowStage: window.WindowStage): void {
157        hilog.info(0x0000, TAG, `loadPage, uri=${this.uri}`);
158        if (this.uri === 'express://queryExpress') {
159            // 构建快递查询参数
160            const storage: LocalStorage = new LocalStorage({
161                "expressNo": this.expressNo
162            } as Record<string, Object>);
163            // 拉起快递查询页面
164            windowStage.loadContent('pages/QueryExpressPage', storage)
165        } else {
166            // 默认拉起首页
167            windowStage.loadContent('pages/Index', (err) => {
168                if (err.code) {
169                    hilog.error(0x0000, TAG, 'Failed to load the content. Cause: %{public}s',
170                        JSON.stringify(err) ?? '');
171                    return;
172                }
173                hilog.info(0x0000, TAG, 'Succeeded in loading the content.');
174            });
175        }
176    }
177
178    onDestroy(): void {
179        hilog.info(0x0000, TAG, `onDestroy`);
180    }
181
182    onWindowStageCreate(windowStage: window.WindowStage): void {
183        hilog.info(0x0000, TAG, `onWindowStageCreate`);
184        this.windowStage = windowStage;
185        this.loadPage(this.windowStage);
186    }
187
188    onWindowStageDestroy(): void {
189        hilog.info(0x0000, TAG, '%{public}s', 'Ability onWindowStageDestroy');
190    }
191
192    onForeground(): void {
193        hilog.info(0x0000, TAG, '%{public}s', 'Ability onForeground');
194    }
195
196    onBackground(): void {
197        hilog.info(0x0000, TAG, '%{public}s', 'Ability onBackground');
198    }
199}
200```