1# **@ohos.bundle.shortcutManager (shortcutManager模块)(系统接口)** 2 3本模块提供系统应用对于快捷方式的增加、删除,以及查询能力,包括[ShortcutInfo](js-apis-bundleManager-shortcutInfo-sys.md)信息的增加、删除以及查询等。 4 5> **说明:** 6> 7> 本模块首批接口从API version 12开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8> 9> 本模块为系统接口。 10 11## 导入模块 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 22增加指定用户的[ShortcutInfo](js-apis-bundleManager-shortcutInfo-sys.md)。 23 24**需要权限:** ohos.permission.MANAGE_SHORTCUTS 25 26**系统接口:** 此接口为系统接口。 27 28**系统能力:** SystemCapability.BundleManager.BundleFramework.Launcher 29 30**参数:** 31 32| 参数名 | 类型 | 必填 | 说明 | 33| ---------- | ------ | ---- | -------------- | 34| shortcutInfo | [ShortcutInfo](js-apis-bundleManager-shortcutInfo-sys.md) | 是 | 快捷方式信息。 | 35| userId | number | 是 | 用户id。 | 36 37**错误码:** 38 39以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.bundle错误码](errorcode-bundle.md)。 40 41| 错误码ID | 错误信息 | 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**示例:** 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 101删除指定用户的[ShortcutInfo](js-apis-bundleManager-shortcutInfo-sys.md)。 102 103**需要权限:** ohos.permission.MANAGE_SHORTCUTS 104 105**系统接口:** 此接口为系统接口。 106 107**系统能力:** SystemCapability.BundleManager.BundleFramework.Launcher 108 109**参数:** 110 111| 参数名 | 类型 | 必填 | 说明 | 112| ---------- | ------ | ---- | -------------- | 113| shortcutInfo | [ShortcutInfo](js-apis-bundleManager-shortcutInfo-sys.md) | 是 | 快捷方式信息。 | 114| userId | number | 是 | 用户id。 | 115 116**错误码:** 117 118以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.bundle错误码](errorcode-bundle.md)。 119 120| 错误码ID | 错误信息 | 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**示例:** 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 176查询指定用户的所有[ShortcutInfo](js-apis-bundleManager-shortcutInfo-sys.md)。 177 178**需要权限:** ohos.permission.MANAGE_SHORTCUTS 179 180**系统接口:** 此接口为系统接口。 181 182**系统能力:** SystemCapability.BundleManager.BundleFramework.Launcher 183 184**参数:** 185 186| 参数名 | 类型 | 必填 | 说明 | 187| ---------- | ------ | ---- | -------------- | 188| userId | number | 是 | 被查询的用户id。 | 189 190**返回值:** 191 192| 类型 | 说明 | 193| ------------------------------------------------------------ | ------------------------------------------------------------ | 194| Array\<[ShortcutInfo](js-apis-bundleManager-shortcutInfo-sys.md)> | Array形式返回[ShortcutInfo](js-apis-bundleManager-shortcutInfo-sys.md)信息。 | 195 196**错误码:** 197 198以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.bundle错误码](errorcode-bundle.md)。 199 200| 错误码ID | 错误信息 | 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**示例:** 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 239 240 241