openharmony 鸿蒙 js-apis-shortcutManager-sys

2025-06-12 浏览 (1)

@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 a shortcut for the given user.

Required permissions: ohos.permission.MANAGE_SHORTCUTS

System API: This is a system API.

System capability: SystemCapability.BundleManager.BundleFramework.Launcher

Parameters

NameTypeMandatoryDescription
shortcutInfoShortcutInfoYesShortcut information.
userIdnumberYesUser ID.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

For details about the error codes, see Universal Error Codes and Bundle Error Codes.

IDError Message
201Verify permission denied.
202Permission denied, non-system app called system api.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.
17700001The specified bundle name is not found.
17700004The specified user ID is not found.
17700026The specified bundle is disabled.
17700061The specified app index is invalid.
17700070The 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 a shortcut for the given user.

Required permissions: ohos.permission.MANAGE_SHORTCUTS

System API: This is a system API.

System capability: SystemCapability.BundleManager.BundleFramework.Launcher

Parameters

NameTypeMandatoryDescription
shortcutInfoShortcutInfoYesShortcut information.
userIdnumberYesUser ID.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

For details about the error codes, see Universal Error Codes and Bundle Error Codes.

IDError Message
201Verify permission denied.
202Permission denied, non-system app called system api.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.
17700004The 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 the information about all shortcuts of the given user.

Required permissions: ohos.permission.MANAGE_SHORTCUTS

System API: This is a system API.

System capability: SystemCapability.BundleManager.BundleFramework.Launcher

Parameters

NameTypeMandatoryDescription
userIdnumberYesUser ID.

Return value

TypeDescription
Promise<Array<ShortcutInfo>>Promise that returns the shortcut information defined in the application configuration file.

Error codes

For details about the error codes, see Universal Error Codes and Bundle Error Codes.

IDError Message
201Verify permission denied.
202Permission denied, non-system app called system api.
401Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.
17700004The 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}`);
          }
        })
      }
    }
  }
}

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Ability Kit

harmony 鸿蒙AbilityAccessControl

harmony 鸿蒙AbilityBase

harmony 鸿蒙AbilityBase_Element

harmony 鸿蒙AbilityRuntime

harmony 鸿蒙bundle

harmony 鸿蒙OH_NativeBundle_ApplicationInfo

harmony 鸿蒙OH_NativeBundle_ElementName

harmony 鸿蒙ability_access_control.h

harmony 鸿蒙ability_base_common.h

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/PlUVEl