• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.bundle.shortcutManager (shortcutManager) (System API)
2<!--Kit: Ability Kit-->
3<!--Subsystem: BundleManager-->
4<!--Owner: @wanghang904-->
5<!--Designer: @hanfeng6-->
6<!--Tester: @kongjing2-->
7<!--Adviser: @Brilliantry_Rui-->
8
9The module allows system applications to add, delete, and query shortcuts, including [ShortcutInfo](js-apis-bundleManager-shortcutInfo.md).
10
11> **NOTE**
12>
13> The initial APIs of this module are supported since API version 12. Newly added APIs will be marked with a superscript to indicate their earliest API version.
14>
15> The APIs provided by this module are system APIs.
16
17## Modules to Import
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
28Adds a shortcut for the given user.
29
30**Required permissions**: ohos.permission.MANAGE_SHORTCUTS
31
32**System API**: This is a system API.
33
34**System capability**: SystemCapability.BundleManager.BundleFramework.Launcher
35
36**Parameters**
37
38| Name    | Type  | Mandatory| Description        |
39| ---------- | ------ | ---- | -------------- |
40| shortcutInfo | [ShortcutInfo](js-apis-bundleManager-shortcutInfo.md) | Yes  | Shortcut information.|
41| userId     | number | Yes  | User ID.|
42
43**Return value**
44
45| Type                                      | Description     |
46| ---------------------------------------- | ------- |
47| Promise\<void> | Promise that returns no value.|
48
49**Error codes**
50
51For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Bundle Error Codes](errorcode-bundle.md).
52
53| ID| Error Message                                |
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**Example**
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
113Deletes a shortcut for the given user.
114
115**Required permissions**: ohos.permission.MANAGE_SHORTCUTS
116
117**System API**: This is a system API.
118
119**System capability**: SystemCapability.BundleManager.BundleFramework.Launcher
120
121**Parameters**
122
123| Name    | Type  | Mandatory| Description        |
124| ---------- | ------ | ---- | -------------- |
125| shortcutInfo | [ShortcutInfo](js-apis-bundleManager-shortcutInfo.md) | Yes  | Shortcut information.|
126| userId     | number | Yes  | User ID.|
127
128**Return value**
129
130| Type                                      | Description     |
131| ---------------------------------------- | ------- |
132| Promise\<void> | Promise that returns no value.|
133
134**Error codes**
135
136For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Bundle Error Codes](errorcode-bundle.md).
137
138| ID| Error Message                                |
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**Example**
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
194Obtains the information about all shortcuts of the given user.
195
196**Required permissions**: ohos.permission.MANAGE_SHORTCUTS
197
198**System API**: This is a system API.
199
200**System capability**: SystemCapability.BundleManager.BundleFramework.Launcher
201
202**Parameters**
203
204| Name    | Type  | Mandatory| Description        |
205| ---------- | ------ | ---- | -------------- |
206| userId     | number | Yes  | User ID.|
207
208**Return value**
209
210| Type                                                        | Description                                                        |
211| ------------------------------------------------------------ | ------------------------------------------------------------ |
212| Promise<Array\<[ShortcutInfo](js-apis-bundleManager-shortcutInfo.md)>> | Promise that returns the shortcut information defined in the application configuration file.|
213
214**Error codes**
215
216For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Bundle Error Codes](errorcode-bundle.md).
217
218| ID| Error Message                                |
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**Example**
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