@ohos.bundle.shortcutManager (shortcutManager) (System API)
The shortcutManager module allows system applications to add, delete, and query shortcuts, including ShortcutInfo.
NOTE
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.
The APIs provided by this module are system APIs.
Modules to Import
import { shortcutManager } from '@kit.AbilityKit';
shortcutManager.addDesktopShortcutInfo12+
addDesktopShortcutInfo(shortcutInfo: ShortcutInfo, userId: number) : Promise<void>
Adds ShortcutInfo for a given user.
Required permissions: ohos.permission.MANAGE_SHORTCUTS
System API: This is a system API.
System capability: SystemCapability.BundleManager.BundleFramework.Launcher
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| shortcutInfo | ShortcutInfo | Yes | Shortcut information. |
| userId | number | Yes | User ID. |
Error codes
For details about the error codes, see Universal Error Codes and Bundle Error Codes.
| ID | Error Message |
|---|---|
| 201 | Verify permission denied. |
| 202 | Permission denied, non-system app called system api. |
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
| 17700001 | The specified bundle name is not found. |
| 17700004 | The specified user ID is not found. |
| 17700026 | The specified bundle is disabled. |
| 17700061 | The specified app index is invalid. |
| 17700070 | The specified shortcut id is illegal. |
Example
import { shortcutManager } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct ShortcutExample {
build() {
Column({ space: 20 }) {
Row({ space: 20 }) {
Button('add').onClick(() => {
let data: shortcutManager.ShortcutInfo = {
id: "test1",
bundleName: "com.example.myapplication",
moduleName: "hello",
hostAbility: "hello",
icon: "hello",
iconId: 1,
label: "hello",
labelId: 1,
wants: [],
appIndex: 0,
sourceType: 0,
}
try {
shortcutManager.addDesktopShortcutInfo(data, 100)
.then(() => {
console.log("addDesktopShortcutInfo success");
}).catch((err: BusinessError) => {
console.error(`addDesktopShortcutInfo errData is errCode:${err.code} message:${err.message}`);
});
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`addDesktopShortcutInfo error is errCode:${code} message:${message}`);
}
})
}
}
}
}
shortcutManager.deleteDesktopShortcutInfo12+
deleteDesktopShortcutInfo(shortcutInfo: ShortcutInfo, userId: number) : Promise<void>
Deletes ShortcutInfo for a given user.
Required permissions: ohos.permission.MANAGE_SHORTCUTS
System API: This is a system API.
System capability: SystemCapability.BundleManager.BundleFramework.Launcher
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| shortcutInfo | ShortcutInfo | Yes | Shortcut information. |
| userId | number | Yes | User ID. |
Error codes
For details about the error codes, see Universal Error Codes and Bundle Error Codes.
| ID | Error Message |
|---|---|
| 201 | Verify permission denied. |
| 202 | Permission denied, non-system app called system api. |
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
| 17700004 | The specified user ID is not found. |
Example
import { shortcutManager } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct ShortcutExample {
build() {
Column({ space: 20 }) {
Row({ space: 20 }) {
Button('delete').onClick(() => {
let data: shortcutManager.ShortcutInfo = {
id: "test1",
bundleName: "com.example.myapplication",
moduleName: "",
hostAbility: "",
icon: "",
iconId: 1,
label: "hello",
labelId: 1,
wants: [],
appIndex: 0,
sourceType: 0,
}
try {
shortcutManager.deleteDesktopShortcutInfo(data, 100)
.then(() => {
console.log("deleteDesktopShortcutInfo success");
}).catch((err: BusinessError) => {
console.error(`deleteDesktopShortcutInfo errData is errCode:${err.code} message:${err.message}`);
});
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`deleteDesktopShortcutInfo error is errCode:${code} message:${message}`);
}
})
}
}
}
}
shortcutManager.getAllDesktopShortcutInfo12+
getAllDesktopShortcutInfo(userId: number) : Promise<Array<ShortcutInfo>>
Obtains all ShortcutInfo records of a given user.
Required permissions: ohos.permission.MANAGE_SHORTCUTS
System API: This is a system API.
System capability: SystemCapability.BundleManager.BundleFramework.Launcher
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| userId | number | Yes | User ID. |
Return value
| Type | Description |
|---|---|
| Array<ShortcutInfo> | Array that holds the ShortcutInfo objects. |
Error codes
For details about the error codes, see Universal Error Codes and Bundle Error Codes.
| ID | Error Message |
|---|---|
| 201 | Verify permission denied. |
| 202 | Permission denied, non-system app called system api. |
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
| 17700004 | The specified user ID is not found. |
Example
import { shortcutManager } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct ShortcutExample {
build() {
Column({ space: 20 }) {
Row({ space: 20 }) {
Button('getall').onClick(() => {
try {
shortcutManager.getAllDesktopShortcutInfo(100)
.then((data: shortcutManager.ShortcutInfo[]) => {
console.log("Shortcut data is " + JSON.stringify(data));
}).catch((err: BusinessError) => {
console.error(`getAllDesktopShortcutInfo errData is errCode:${err.code} message:${err.message}`);
});
} catch (error) {
let code = (error as BusinessError).code;
let message = (error as BusinessError).message;
console.error(`getAllDesktopShortcutInfo error is errCode:${code} message:${message}`);
}
})
}
}
}
}