• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 拉起邮件类应用(startAbilityByType)
2
3本章节介绍如何拉起邮件类应用扩展面板。
4> **说明:**
5>
6> 如果拉起方的参数为mailto协议字符串,可以[使用mailto方式拉起邮件应用](start-email-apps-by-mailto.md)。邮件应用会解析收到的mailto协议字符串,并填充发件人、收件人、邮件内容等信息。
7
8## 邮件类应用扩展面板参数说明
9
10startAbilityByType接口中type字段为mail,对应的wantParam参数:
11
12| 参数名                                | 类型                                                         | 必填 | 说明                                                         |
13| ------------------------------------- | ------------------------------------------------------------ | ------ | ------------------------------------------------------------ |
14| email                                 | string[ ]                                                    | 否   | 收件人邮箱地址(支持多个且以逗号分隔)。                       |
15| cc                                    | string[ ]                                                    | 否   | 抄收人邮箱地址(支持多个且以逗号分隔)。                       |
16| bcc                                   | string[ ]                                                    | 否   | 密送人邮箱地址(支持多个且以逗号分隔)。                       |
17| subject                               | string                                                       | 否   | 邮件主题。                                                     |
18| body                                  | string                                                       | 否   | 邮件内容。                                                     |
19| ability.params.stream                 | string[ ]                                                    | 否   | 邮件附件(附件的uri地址列表)。                                |
20| ability.want.params.uriPermissionFlag | [wantConstant.Flags](../reference/apis-ability-kit/js-apis-app-ability-wantConstant.md#flags) | 否   | 给邮件附件赋予至少读权限。邮件附件参数存在时,该参数也必须要传。 |
21| sceneType                             | number                                                       | 否   | 意图场景,表明本次请求对应的操作意图。1:发邮件。默认为1。                              |
22
23> **说明:**
24>
25> * 邮件类应用扩展面板中的类型为string的参数,都要经过encodeURI编码。
26>
27> * 邮件类应用扩展面板中的类型为string[]的参数,数组中的元素都要经过encodeURI编码。
28
29## 拉起方开发步骤
301. 导入相关模块。
31    ```ts
32    import { common, wantConstant } from '@kit.AbilityKit';
33    ```
342. 构造接口参数并调用startAbilityByType接口。
35
36    ```ts
37    @Entry
38    @Component
39    struct Index {
40        @State hideAbility: string = 'hideAbility'
41
42        build() {
43            Row() {
44                Column() {
45                    Text(this.hideAbility)
46                        .fontSize(30)
47                        .fontWeight(FontWeight.Bold)
48                        .onClick(() => {
49                            let context = this.getUIContext().getHostContext() as common.UIAbilityContext;
50                            let wantParam: Record<string, Object> = {
51                                'sceneType': 1,
52                                'email': [encodeURI('xxx@example.com'), encodeURI('xxx@example.com')], // 收件人邮箱地址,多值以逗号分隔,对数组内容使用encodeURI()方法进行url编码
53                                'cc': [encodeURI('xxx@example.com'), encodeURI('xxx@example.com')], // 抄收人邮箱地址,多值以逗号分隔,对数组内容使用encodeURI()方法进行url编码
54                                'bcc': [encodeURI('xxx@example.com'), encodeURI('xxx@example.com')], // 密送人邮箱地址,多值以逗号分隔,对数组内容使用encodeURI()方法进行url编码
55                                'subject': encodeURI('邮件主题'), // 邮件主题,对内容使用encodeURI()方法进行url编码
56                                'body': encodeURI('邮件正文'), // 邮件正文,对内容使用encodeURI()方法进行url编码
57                                'ability.params.stream': [encodeURI('附件uri1'), encodeURI('附件uri2')], // 附件uri,多值以逗号分隔,对数组内容使用encodeURI()方法进行url编码
58                                'ability.want.params.uriPermissionFlag': wantConstant.Flags.FLAG_AUTH_READ_URI_PERMISSION
59                            };
60                            let abilityStartCallback: common.AbilityStartCallback = {
61                                onError: (code: number, name: string, message: string) => {
62                                    console.log(`onError code ${code} name: ${name} message: ${message}`);
63                                },
64                                onResult: (result) => {
65                                    console.log(`onResult result: ${JSON.stringify(result)}`);
66                                }
67                            }
68
69                            context.startAbilityByType("mail", wantParam, abilityStartCallback,
70                                (err) => {
71                                    if (err) {
72                                        console.error(`startAbilityByType fail, err: ${JSON.stringify(err)}`);
73                                    } else {
74                                        console.log(`success`);
75                                    }
76                                });
77                        });
78                }
79                .width('100%')
80            }
81            .height('100%')
82        }
83    }
84    ```
85    效果示例图:
86
87    ![效果示例图](./figures/start-mail-panel.png)
88
89## 目标方开发步骤
90
911. 在module.json5中新增[linkFeature](../quick-start/module-configuration-file.md#skills标签)属性并设置声明当前应用支持的特性功能,从而系统可以从设备已安装应用中找到当前支持该特性的应用,取值范围如下:
92
93    | 取值           | 含义                      |
94    | --------------| ------------------------- |
95    | ComposeMail   | 声明应用支持撰写邮件功能		|
96
97    ```json
98    {
99      "abilities": [
100          {
101          "skills": [
102              {
103              "uris": [
104                  {
105                  "scheme": "mailto", // 这里仅示意,应用需确保这里声明的的uri能被外部正常拉起
106                  "host": "",
107                  "path": "",
108                  "linkFeature": "ComposeMail" // 声明应用支持撰写邮件功能
109                  }
110                ]
111              }
112          ]
113          }
114      ]
115    }
116    ```
117
1182. 解析面板传过来的参数并做对应处理。
119
120    ```ts
121    UIAbility.onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void
122    ```
123
124    在参数**want.parameters**中会携带Caller方传入的参数(与调用方传入的有些差异),如下表所示:
125
126    | 参数名  | 类型      | 必填 | 说明                                   |
127    | ------- | --------- | ---- | -------------------------------------- |
128    | email   | string[ ] | 否   | 收件人邮箱地址(支持多个且以逗号分隔)。 |
129    | cc      | string[ ] | 否   | 抄收人邮箱地址(支持多个且以逗号分隔)。 |
130    | bcc     | string[ ] | 否   | 密送人邮箱地址(支持多个且以逗号分隔)。 |
131    | subject | string    | 否   | 邮件主题。                               |
132    | body    | string    | 否   | 邮件内容。                               |
133    | stream  | string[ ] | 否   | 邮件附件列表(附件的uri地址列表)。      |
134
135    > **说明:**
136    >
137    > * 目标方接收的类型为string的参数,都要经过decodeURI解码。
138    >
139    > * 目标方接收的类型为string[]的参数,数组中的元素都要经过decodeURI解码。
140
141**完整示例:**
142
143```ts
144import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit';
145import { hilog } from '@kit.PerformanceAnalysisKit';
146import { window } from '@kit.ArkUI';
147
148const TAG = 'MailTarget1.EntryAbility'
149
150export default class EntryAbility extends UIAbility {
151    windowStage: window.WindowStage | null = null;
152
153    email: string[] | undefined;
154    cc: string[] | undefined;
155    bcc: string[] | undefined;
156    subject: string | undefined;
157    body: string | undefined;
158    stream: string[] | undefined;
159
160    onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
161        hilog.info(0x0000, TAG, `onCreate, want=${JSON.stringify(want)}`);
162        super.onCreate(want, launchParam);
163        this.parseWant(want);
164    }
165
166    onNewWant(want: Want, launchParam: AbilityConstant.LaunchParam): void {
167        hilog.info(0x0000, TAG, `onNewWant, want=${JSON.stringify(want)}`);
168        super.onNewWant(want, launchParam);
169        this.parseWant(want);
170        if (!this.windowStage) {
171            hilog.error(0x0000, TAG, 'windowStage is null');
172            this.context.terminateSelf();
173            return;
174        }
175        this.loadPage(this.windowStage);
176    }
177
178    private parseWant(want: Want): void {
179        this.email = this.decodeStringArr(want.parameters?.email as string[]);
180        this.cc = this.decodeStringArr(want.parameters?.cc as string[]);
181        this.bcc = this.decodeStringArr(want.parameters?.bcc as string[]);
182        this.subject = decodeURI(want.parameters?.subject as string);// 使用decodeURI()方法对邮件主题进行url解码,其他字段处理方法相同
183        this.body = decodeURI(want.parameters?.body as string);// 使用decodeURI()方法对邮件内容进行url解码,其他字段处理方法相同
184        this.stream = this.decodeStringArr(want.parameters?.stream as string[]);
185    }
186
187    // 使用decodeURI()方法对string数组内容进行解码
188    private decodeStringArr(source: string[] | undefined): string[] {
189        let target: string[] = [];
190        source?.forEach(e => {
191            target.push(decodeURI(e));
192        })
193        return target;
194    }
195
196    private loadPage(windowStage: window.WindowStage): void {
197        const storage: LocalStorage = new LocalStorage({
198            "email": this.email,
199            "cc": this.cc,
200            "bcc": this.bcc,
201            "subject": this.subject,
202            "body": this.body,
203            "stream": this.stream
204        } as Record<string, Object>);
205
206        windowStage.loadContent('pages/ComposeMailPage', storage);
207
208    }
209
210    onDestroy(): void {
211        hilog.info(0x0000, TAG, `onDestroy`);
212    }
213
214    onWindowStageCreate(windowStage: window.WindowStage): void {
215        hilog.info(0x0000, TAG, `onWindowStageCreate`);
216        this.windowStage = windowStage;
217        this.loadPage(this.windowStage);
218    }
219
220    onWindowStageDestroy(): void {
221        hilog.info(0x0000, TAG, `onWindowStageDestroy`);
222    }
223
224    onForeground(): void {
225        hilog.info(0x0000, TAG, `onForeground`);
226    }
227
228    onBackground(): void {
229        hilog.info(0x0000, TAG, `onBackground`);
230    }
231}
232```
233