1# @ohos.bundle.shortcutManager (shortcutManager) (System API) 2 3The shortcutManager module allows system applications to add, delete, and query shortcuts, including [ShortcutInfo](js-apis-bundleManager-shortcutInfo-sys.md). 4 5> **NOTE** 6> 7> 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. 8> 9> The APIs provided by this module are system APIs. 10 11## Modules to Import 12 13```ts 14import { shortcutManager } from '@kit.AbilityKit'; 15``` 16 17 18## shortcutManager.addDesktopShortcutInfo<sup>12+</sup> 19 20addDesktopShortcutInfo(shortcutInfo: [ShortcutInfo](js-apis-bundleManager-shortcutInfo-sys.md), userId: number) : Promise\<void> 21 22Adds [ShortcutInfo](js-apis-bundleManager-shortcutInfo-sys.md) for a given user. 23 24**Required permissions**: ohos.permission.MANAGE_SHORTCUTS 25 26**System API**: This is a system API. 27 28**System capability**: SystemCapability.BundleManager.BundleFramework.Launcher 29 30**Parameters** 31 32| Name | Type | Mandatory| Description | 33| ---------- | ------ | ---- | -------------- | 34| shortcutInfo | [ShortcutInfo](js-apis-bundleManager-shortcutInfo-sys.md) | Yes | Shortcut information.| 35| userId | number | Yes | User ID.| 36 37**Error codes** 38 39For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Bundle Error Codes](errorcode-bundle.md). 40 41| ID| Error Message | 42| -------- | ---------------------------------------- | 43| 201 | Verify permission denied. | 44| 202 | Permission denied, non-system app called system api. | 45| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.| 46| 17700001 | The specified bundle name is not found. | 47| 17700004 | The specified user ID is not found. | 48| 17700026 | The specified bundle is disabled. | 49| 17700061 | The specified app index is invalid. | 50| 17700070 | The specified shortcut id is illegal. | 51 52**Example** 53 54```ts 55import { shortcutManager } from '@kit.AbilityKit'; 56import { BusinessError } from '@kit.BasicServicesKit'; 57 58@Entry 59@Component 60struct ShortcutExample { 61 build() { 62 Column({ space: 20 }) { 63 Row({ space: 20 }) { 64 Button('add').onClick(() => { 65 let data: shortcutManager.ShortcutInfo = { 66 id: "test1", 67 bundleName: "com.example.myapplication", 68 moduleName: "hello", 69 hostAbility: "hello", 70 icon: "hello", 71 iconId: 1, 72 label: "hello", 73 labelId: 1, 74 wants: [], 75 appIndex: 0, 76 sourceType: 0, 77 } 78 try { 79 shortcutManager.addDesktopShortcutInfo(data, 100) 80 .then(() => { 81 console.log("addDesktopShortcutInfo success"); 82 }).catch((err: BusinessError) => { 83 console.error(`addDesktopShortcutInfo errData is errCode:${err.code} message:${err.message}`); 84 }); 85 } catch (error) { 86 let code = (error as BusinessError).code; 87 let message = (error as BusinessError).message; 88 console.error(`addDesktopShortcutInfo error is errCode:${code} message:${message}`); 89 } 90 }) 91 } 92 } 93 } 94} 95``` 96 97## shortcutManager.deleteDesktopShortcutInfo<sup>12+</sup> 98 99deleteDesktopShortcutInfo(shortcutInfo: [ShortcutInfo](js-apis-bundleManager-shortcutInfo-sys.md), userId: number) : Promise\<void> 100 101Deletes [ShortcutInfo](js-apis-bundleManager-shortcutInfo-sys.md) for a given user. 102 103**Required permissions**: ohos.permission.MANAGE_SHORTCUTS 104 105**System API**: This is a system API. 106 107**System capability**: SystemCapability.BundleManager.BundleFramework.Launcher 108 109**Parameters** 110 111| Name | Type | Mandatory| Description | 112| ---------- | ------ | ---- | -------------- | 113| shortcutInfo | [ShortcutInfo](js-apis-bundleManager-shortcutInfo-sys.md) | Yes | Shortcut information.| 114| userId | number | Yes | User ID.| 115 116**Error codes** 117 118For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Bundle Error Codes](errorcode-bundle.md). 119 120| ID| Error Message | 121| -------- | ---------------------------------------- | 122| 201 | Verify permission denied. | 123| 202 | Permission denied, non-system app called system api. | 124| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.| 125| 17700004 | The specified user ID is not found. | 126 127**Example** 128 129```ts 130import { shortcutManager } from '@kit.AbilityKit'; 131import { BusinessError } from '@kit.BasicServicesKit'; 132 133@Entry 134@Component 135struct ShortcutExample { 136 build() { 137 Column({ space: 20 }) { 138 Row({ space: 20 }) { 139 Button('delete').onClick(() => { 140 let data: shortcutManager.ShortcutInfo = { 141 id: "test1", 142 bundleName: "com.example.myapplication", 143 moduleName: "", 144 hostAbility: "", 145 icon: "", 146 iconId: 1, 147 label: "hello", 148 labelId: 1, 149 wants: [], 150 appIndex: 0, 151 sourceType: 0, 152 } 153 try { 154 shortcutManager.deleteDesktopShortcutInfo(data, 100) 155 .then(() => { 156 console.log("deleteDesktopShortcutInfo success"); 157 }).catch((err: BusinessError) => { 158 console.error(`deleteDesktopShortcutInfo errData is errCode:${err.code} message:${err.message}`); 159 }); 160 } catch (error) { 161 let code = (error as BusinessError).code; 162 let message = (error as BusinessError).message; 163 console.error(`deleteDesktopShortcutInfo error is errCode:${code} message:${message}`); 164 } 165 }) 166 } 167 } 168 } 169} 170``` 171 172## shortcutManager.getAllDesktopShortcutInfo<sup>12+</sup> 173 174getAllDesktopShortcutInfo(userId: number) : Promise<Array\<[ShortcutInfo](js-apis-bundleManager-shortcutInfo-sys.md)>> 175 176Obtains all [ShortcutInfo](js-apis-bundleManager-shortcutInfo-sys.md) records of a given user. 177 178**Required permissions**: ohos.permission.MANAGE_SHORTCUTS 179 180**System API**: This is a system API. 181 182**System capability**: SystemCapability.BundleManager.BundleFramework.Launcher 183 184**Parameters** 185 186| Name | Type | Mandatory| Description | 187| ---------- | ------ | ---- | -------------- | 188| userId | number | Yes | User ID.| 189 190**Return value** 191 192| Type | Description | 193| ------------------------------------------------------------ | ------------------------------------------------------------ | 194| Array\<[ShortcutInfo](js-apis-bundleManager-shortcutInfo-sys.md)> | Array that holds the [ShortcutInfo](js-apis-bundleManager-shortcutInfo-sys.md) objects.| 195 196**Error codes** 197 198For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Bundle Error Codes](errorcode-bundle.md). 199 200| ID| Error Message | 201| -------- | ---------------------------------------- | 202| 201 | Verify permission denied. | 203| 202 | Permission denied, non-system app called system api. | 204| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.| 205| 17700004 | The specified user ID is not found. | 206 207**Example** 208 209```ts 210import { shortcutManager } from '@kit.AbilityKit'; 211import { BusinessError } from '@kit.BasicServicesKit'; 212 213@Entry 214@Component 215struct ShortcutExample { 216 build() { 217 Column({ space: 20 }) { 218 Row({ space: 20 }) { 219 Button('getall').onClick(() => { 220 try { 221 shortcutManager.getAllDesktopShortcutInfo(100) 222 .then((data: shortcutManager.ShortcutInfo[]) => { 223 console.log("Shortcut data is " + JSON.stringify(data)); 224 }).catch((err: BusinessError) => { 225 console.error(`getAllDesktopShortcutInfo errData is errCode:${err.code} message:${err.message}`); 226 }); 227 } catch (error) { 228 let code = (error as BusinessError).code; 229 let message = (error as BusinessError).message; 230 console.error(`getAllDesktopShortcutInfo error is errCode:${code} message:${message}`); 231 } 232 }) 233 } 234 } 235 } 236} 237``` 238