• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.bundle.shortcutManager (shortcutManager模块)(系统接口)
2<!--Kit: Ability Kit-->
3<!--Subsystem: BundleManager-->
4<!--Owner: @wanghang904-->
5<!--Designer: @hanfeng6-->
6<!--Tester: @kongjing2-->
7<!--Adviser: @Brilliantry_Rui-->
8
9本模块提供系统应用对于快捷方式的增加、删除,以及查询能力,包括[ShortcutInfo](js-apis-bundleManager-shortcutInfo.md)信息的增加、删除以及查询等。
10
11> **说明:**
12>
13> 本模块首批接口从API version 12开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
14>
15> 本模块为系统接口。
16
17## 导入模块
18
19```ts
20import { shortcutManager } from '@kit.AbilityKit';
21```
22
23
24## shortcutManager.addDesktopShortcutInfo<sup>12+</sup>
25
26addDesktopShortcutInfo(shortcutInfo: [ShortcutInfo](js-apis-bundleManager-shortcutInfo.md), userId: number) : Promise\<void>
27
28增加指定用户的快捷方式信息。
29
30**需要权限:** ohos.permission.MANAGE_SHORTCUTS
31
32**系统接口:** 此接口为系统接口。
33
34**系统能力:** SystemCapability.BundleManager.BundleFramework.Launcher
35
36**参数:**
37
38| 参数名     | 类型   | 必填 | 说明         |
39| ---------- | ------ | ---- | -------------- |
40| shortcutInfo | [ShortcutInfo](js-apis-bundleManager-shortcutInfo.md) | 是   | 快捷方式信息。 |
41| userId     | number | 是   | 用户id。 |
42
43**返回值:**
44
45| 类型                                       | 说明      |
46| ---------------------------------------- | ------- |
47| Promise\<void> | 无返回结果的Promise对象。 |
48
49**错误码:**
50
51以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.bundle错误码](errorcode-bundle.md)。
52
53| 错误码ID | 错误信息                                 |
54| -------- | ---------------------------------------- |
55| 201 | Verify permission denied. |
56| 202 | Permission denied, non-system app called system api. |
57| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.|
58| 17700001 | The specified bundle name is not found.  |
59| 17700004 | The specified user ID is not found.      |
60| 17700026 | The specified bundle is disabled. |
61| 17700061 | The specified app index is invalid. |
62| 17700070 | The specified shortcut id is illegal. |
63
64**示例:**
65
66```ts
67import { shortcutManager } from '@kit.AbilityKit';
68import { BusinessError } from '@kit.BasicServicesKit';
69
70@Entry
71@Component
72struct ShortcutExample {
73  build() {
74    Column({ space: 20 }) {
75      Row({ space: 20 }) {
76        Button('add').onClick(() => {
77          let data: shortcutManager.ShortcutInfo = {
78            id: "test1",
79            bundleName: "com.example.myapplication",
80            moduleName: "hello",
81            hostAbility: "hello",
82            icon: "hello",
83            iconId: 1,
84            label: "hello",
85            labelId: 1,
86            wants: [],
87            appIndex: 0,
88            sourceType: 0,
89          }
90          try {
91            shortcutManager.addDesktopShortcutInfo(data, 100)
92              .then(() => {
93                console.info("addDesktopShortcutInfo success");
94              }).catch((err: BusinessError) => {
95              console.error(`addDesktopShortcutInfo errData is errCode:${err.code}  message:${err.message}`);
96            });
97          } catch (error) {
98            let code = (error as BusinessError).code;
99            let message = (error as BusinessError).message;
100            console.error(`addDesktopShortcutInfo error is errCode:${code}  message:${message}`);
101          }
102        })
103      }
104    }
105  }
106}
107```
108
109## shortcutManager.deleteDesktopShortcutInfo<sup>12+</sup>
110
111deleteDesktopShortcutInfo(shortcutInfo: [ShortcutInfo](js-apis-bundleManager-shortcutInfo.md), userId: number) : Promise\<void>
112
113删除指定用户的快捷方式信息。
114
115**需要权限:** ohos.permission.MANAGE_SHORTCUTS
116
117**系统接口:** 此接口为系统接口。
118
119**系统能力:** SystemCapability.BundleManager.BundleFramework.Launcher
120
121**参数:**
122
123| 参数名     | 类型   | 必填 | 说明         |
124| ---------- | ------ | ---- | -------------- |
125| shortcutInfo | [ShortcutInfo](js-apis-bundleManager-shortcutInfo.md) | 是   | 快捷方式信息。 |
126| userId     | number | 是   | 用户id。 |
127
128**返回值:**
129
130| 类型                                       | 说明      |
131| ---------------------------------------- | ------- |
132| Promise\<void> | 无返回结果的Promise对象。 |
133
134**错误码:**
135
136以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.bundle错误码](errorcode-bundle.md)。
137
138| 错误码ID | 错误信息                                 |
139| -------- | ---------------------------------------- |
140| 201 | Verify permission denied. |
141| 202 | Permission denied, non-system app called system api. |
142| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.|
143| 17700004 | The specified user ID is not found.       |
144
145**示例:**
146
147```ts
148import { shortcutManager } from '@kit.AbilityKit';
149import { BusinessError } from '@kit.BasicServicesKit';
150
151@Entry
152@Component
153struct ShortcutExample {
154  build() {
155    Column({ space: 20 }) {
156      Row({ space: 20 }) {
157        Button('delete').onClick(() => {
158          let data: shortcutManager.ShortcutInfo = {
159            id: "test1",
160            bundleName: "com.example.myapplication",
161            moduleName: "",
162            hostAbility: "",
163            icon: "",
164            iconId: 1,
165            label: "hello",
166            labelId: 1,
167            wants: [],
168            appIndex: 0,
169            sourceType: 0,
170          }
171          try {
172            shortcutManager.deleteDesktopShortcutInfo(data, 100)
173              .then(() => {
174                console.info("deleteDesktopShortcutInfo success");
175              }).catch((err: BusinessError) => {
176              console.error(`deleteDesktopShortcutInfo errData is errCode:${err.code}  message:${err.message}`);
177            });
178          } catch (error) {
179            let code = (error as BusinessError).code;
180            let message = (error as BusinessError).message;
181            console.error(`deleteDesktopShortcutInfo error is errCode:${code}  message:${message}`);
182          }
183        })
184      }
185    }
186  }
187}
188```
189
190## shortcutManager.getAllDesktopShortcutInfo<sup>12+</sup>
191
192getAllDesktopShortcutInfo(userId: number) : Promise<Array\<[ShortcutInfo](js-apis-bundleManager-shortcutInfo.md)>>
193
194查询指定用户的所有快捷方式信息。
195
196**需要权限:** ohos.permission.MANAGE_SHORTCUTS
197
198**系统接口:** 此接口为系统接口。
199
200**系统能力:** SystemCapability.BundleManager.BundleFramework.Launcher
201
202**参数:**
203
204| 参数名     | 类型   | 必填 | 说明         |
205| ---------- | ------ | ---- | -------------- |
206| userId     | number | 是   | 被查询的用户id。 |
207
208**返回值:**
209
210| 类型                                                         | 说明                                                         |
211| ------------------------------------------------------------ | ------------------------------------------------------------ |
212| Promise<Array\<[ShortcutInfo](js-apis-bundleManager-shortcutInfo.md)>> | Promise对象,返回应用配置文件中定义的快捷方式信息。 |
213
214**错误码:**
215
216以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.bundle错误码](errorcode-bundle.md)。
217
218| 错误码ID | 错误信息                                 |
219| -------- | ---------------------------------------- |
220| 201 | Verify permission denied. |
221| 202 | Permission denied, non-system app called system api. |
222| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.|
223| 17700004 | The specified user ID is not found.       |
224
225**示例:**
226
227```ts
228import { shortcutManager } from '@kit.AbilityKit';
229import { BusinessError } from '@kit.BasicServicesKit';
230
231@Entry
232@Component
233struct ShortcutExample {
234  build() {
235    Column({ space: 20 }) {
236      Row({ space: 20 }) {
237        Button('getall').onClick(() => {
238          try {
239            shortcutManager.getAllDesktopShortcutInfo(100)
240              .then((data: shortcutManager.ShortcutInfo[]) => {
241                console.info("Shortcut data is " + JSON.stringify(data));
242              }).catch((err: BusinessError) => {
243              console.error(`getAllDesktopShortcutInfo errData is errCode:${err.code}  message:${err.message}`);
244            });
245          } catch (error) {
246            let code = (error as BusinessError).code;
247            let message = (error as BusinessError).message;
248            console.error(`getAllDesktopShortcutInfo error is errCode:${code}  message:${message}`);
249          }
250        })
251      }
252    }
253  }
254}
255```
256
257
258
259