openharmony 鸿蒙 js-apis-shortcutManager-sys

2025-06-12 浏览 (1)

@ohos.bundle.shortcutManager (shortcutManager模块)(系统接口)

本模块提供系统应用对于快捷方式的增加、删除,以及查询能力,包括ShortcutInfo信息的增加、删除以及查询等。

说明:

本模块首批接口从API version 12开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

本模块为系统接口。

导入模块

import { shortcutManager } from '@kit.AbilityKit';

shortcutManager.addDesktopShortcutInfo12+

addDesktopShortcutInfo(shortcutInfo: ShortcutInfo, userId: number) : Promise<void>

增加指定用户的快捷方式信息。

需要权限: ohos.permission.MANAGE_SHORTCUTS

系统接口: 此接口为系统接口。

系统能力: SystemCapability.BundleManager.BundleFramework.Launcher

参数:

参数名类型必填说明
shortcutInfoShortcutInfo快捷方式信息。
userIdnumber用户id。

返回值:

类型说明
Promise<void>无返回结果的Promise对象。

错误码:

以下错误码的详细介绍请参见通用错误码ohos.bundle错误码

错误码ID错误信息
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.

示例:

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>

删除指定用户的快捷方式信息。

需要权限: ohos.permission.MANAGE_SHORTCUTS

系统接口: 此接口为系统接口。

系统能力: SystemCapability.BundleManager.BundleFramework.Launcher

参数:

参数名类型必填说明
shortcutInfoShortcutInfo快捷方式信息。
userIdnumber用户id。

返回值:

类型说明
Promise<void>无返回结果的Promise对象。

错误码:

以下错误码的详细介绍请参见通用错误码ohos.bundle错误码

错误码ID错误信息
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.

示例:

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>>

查询指定用户的所有快捷方式信息。

需要权限: ohos.permission.MANAGE_SHORTCUTS

系统接口: 此接口为系统接口。

系统能力: SystemCapability.BundleManager.BundleFramework.Launcher

参数:

参数名类型必填说明
userIdnumber被查询的用户id。

返回值:

类型说明
Promise<Array<ShortcutInfo>>Promise对象,返回应用配置文件中定义的快捷方式信息。

错误码:

以下错误码的详细介绍请参见通用错误码ohos.bundle错误码

错误码ID错误信息
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.

示例:

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 鸿蒙AbilityBase

harmony 鸿蒙AbilityBase_Element

harmony 鸿蒙AbilityRuntime

harmony 鸿蒙bundle

harmony 鸿蒙OH_NativeBundle_ApplicationInfo

harmony 鸿蒙OH_NativeBundle_ElementName

harmony 鸿蒙ability_base_common.h

harmony 鸿蒙ability_runtime_common.h

harmony 鸿蒙application_context.h

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