# @ohos.bundle.bundleResourceManager (bundleResourceManager模块)(系统接口) 本模块提供应用资源数据查询能力,支持[BundleResourceInfo](js-apis-bundleManager-BundleResourceInfo-sys.md)和[LauncherAbilityResourceInfo](js-apis-bundleManager-LauncherAbilityResourceInfo-sys.md)等信息的查询。 > **说明:** > > 本模块首批接口从API version 11 开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 > > 本模块从API version 12 开始支持查询被禁用应用和设备上已安装应用(不区用户)的图标和名称资源。 > > 本模块为系统接口。 ## 导入模块 ```ts import bundleResourceManager from '@ohos.bundle.bundleResourceManager'; ``` ## 权限列表 | 权限 | 权限等级 | 描述 | | ------------------------------------------ | ------------ | ------------------| | ohos.permission.GET_BUNDLE_RESOURCES| system_basic | 允许查询应用的资源信息。 | | ohos.permission.GET_INSTALLED_BUNDLE_LIST | system_basic | 读取已安装应用列表。 | 权限等级参考[权限APL等级说明](../../security/AccessToken/app-permission-mgmt-overview.md#权限机制中的基本概念)。 ## 枚举 ### ResourceFlag 资源信息标志,指示需要获取的资源信息的内容。 **系统接口:** 此接口为系统接口。 **系统能力:** 以下各项对应的系统能力均为SystemCapability.BundleManager.BundleFramework.Resource。 | 名称 | 值 | 说明 | | ----------------------------------------- | ---------- | ------------------------------------------------------------ | | GET_RESOURCE_INFO_ALL | 0x00000001 | 用于同时获取icon和label信息。| | GET_RESOURCE_INFO_WITH_LABEL | 0x00000002 | 用于获取仅包含label信息,iocn信息为空。 | | GET_RESOURCE_INFO_WITH_ICON | 0x00000004 | 用于获取仅包含icon信息,label信息为空。 | | GET_RESOURCE_INFO_WITH_SORTED_BY_LABEL| 0x00000008 | 用于获取根据label排序后的信息。它不能单独使用需要与GET_RESOURCE_INFO_ALL 或 GET_RESOURCE_INFO_WITH_LABEL一起使用。 | | GET_RESOURCE_INFO_WITH_DRAWABLE_DESCRIPTOR12+| 0x00000010 | 用于获取应用图标的[drawableDescriptor](../apis-arkui/js-apis-arkui-drawableDescriptor-sys.md)对象。 | ## 接口 ### bundleResourceManager.getBundleResourceInfo getBundleResourceInfo(bundleName: string, resourceFlags?: [number](#resourceflag)): [BundleResourceInfo](js-apis-bundleManager-BundleResourceInfo-sys.md) 以同步方法根据给定的bundleName和resourceFlags获取当前应用的BundleResourceInfo。 **系统接口:** 此接口为系统接口。 **需要权限:** ohos.permission.GET_BUNDLE_RESOURCES **系统能力:** SystemCapability.BundleManager.BundleFramework.Resource **参数:** | 参数名 | 类型 | 必填 | 说明 | | ----------- | ------ | ---- | --------------------- | | bundleName | string | 是 | 指定查询应用的包名。 | | resourceFlags | [number](#resourceflag) | 否 | 指定返回的BundleResourceInfo所包含的信息。 | **返回值:** | 类型 | 说明 | | ----------------------------------------------------------- | ------------------------------------- | | [BundleResourceInfo](js-apis-bundleManager-BundleResourceInfo-sys.md) | 返回指定应用的BundleResourceInfo。| **错误码:** 以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.bundle错误码](errorcode-bundle.md)。 | 错误码ID | 错误信息 | | -------- | ------------------------------------- | | 201 | 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 bundleName is not found. | **示例:** ```ts import bundleResourceManager from '@ohos.bundle.bundleResourceManager'; import { BusinessError } from '@ohos.base'; import hilog from '@ohos.hilog'; let bundleName = "com.example.myapplication"; let bundleFlags = bundleResourceManager.ResourceFlag.GET_RESOURCE_INFO_ALL; try { let resourceInfo = bundleResourceManager.getBundleResourceInfo(bundleName, bundleFlags); hilog.info(0x0000, 'testTag', 'getBundleResourceInfo successfully. Data label: %{public}s', JSON.stringify(resourceInfo.label)); } catch (err) { let message = (err as BusinessError).message; hilog.error(0x0000, 'testTag', 'getBundleResourceInfo failed: %{public}s', message); } ``` ### bundleResourceManager.getLauncherAbilityResourceInfo getLauncherAbilityResourceInfo(bundleName: string, resourceFlags?: [number](#resourceflag)): Array<[LauncherAbilityResourceInfo](js-apis-bundleManager-LauncherAbilityResourceInfo-sys.md)> 以同步方法根据给定的bundleName和resourceFlags获取当前应用的LauncherAbilityResourceInfo。 **系统接口:** 此接口为系统接口。 **需要权限:** ohos.permission.GET_BUNDLE_RESOURCES **系统能力:** SystemCapability.BundleManager.BundleFramework.Resource **参数:** | 参数名 | 类型 | 必填 | 说明 | | ----------- | ------ | ---- | --------------------- | | bundleName | string | 是 | 指定查询应用的包名。 | | resourceFlags | [number](#resourceflag) | 否 | 指定返回的LauncherAbilityResourceInfo所包含的信息。 | **返回值:** | 类型 | 说明 | | ----------------------------------------------------------- | ------------------------------------- | | Array<[LauncherAbilityResourceInfo](js-apis-bundleManager-LauncherAbilityResourceInfo-sys.md)> | 返回指定应用的LauncherAbilityResourceInfo。| **错误码:** 以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.bundle错误码](errorcode-bundle.md)。 | 错误码ID | 错误信息 | | -------- | ------------------------------------- | | 201 | 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 bundleName is not found. | **示例:** ```ts import bundleResourceManager from '@ohos.bundle.bundleResourceManager'; import { BusinessError } from '@ohos.base'; import hilog from '@ohos.hilog'; let bundleName = "com.example.myapplication"; let bundleFlags = bundleResourceManager.ResourceFlag.GET_RESOURCE_INFO_ALL; try { let resourceInfo = bundleResourceManager.getLauncherAbilityResourceInfo(bundleName, bundleFlags); hilog.info(0x0000, 'testTag', 'getLauncherAbilityResourceInfo successfully. Data label: %{public}s', JSON.stringify(resourceInfo[0].label)); } catch (err) { let message = (err as BusinessError).message; hilog.error(0x0000, 'testTag', 'getLauncherAbilityResourceInfo failed: %{public}s', message); } ``` ### bundleResourceManager.getAllBundleResourceInfo getAllBundleResourceInfo(resourceFlags: [number](#resourceflag), callback: AsyncCallback>): void 以异步方法根据给定的resourceFlags获取所有应用的BundleResourceInfo。使用callback异步回调。 **系统接口:** 此接口为系统接口。 **需要权限:** ohos.permission.GET_INSTALLED_BUNDLE_LIST 和 ohos.permission.GET_BUNDLE_RESOURCES **系统能力:** SystemCapability.BundleManager.BundleFramework.Resource **参数:** | 参数名 | 类型 | 必填 | 说明 | | ----------- | ------ | ---- | --------------------- | | resourceFlags | [number](#resourceflag) | 是 | 指定返回的BundleResourceInfo所包含的信息。 | | callback | AsyncCallback\> | 是 | 回调函数,当获取成功时,err为null,data为获取到的BundleResourceInfo数值;否则为错误对象。 | **错误码:** 以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 | 错误码ID | 错误信息 | | -------- | ------------------------------------- | | 201 | 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.| **示例:** ```ts import bundleResourceManager from '@ohos.bundle.bundleResourceManager'; import { BusinessError } from '@ohos.base'; import hilog from '@ohos.hilog'; let bundleFlags = bundleResourceManager.ResourceFlag.GET_RESOURCE_INFO_ALL; try { bundleResourceManager.getAllBundleResourceInfo(bundleFlags, (err, data) => { if (err) { hilog.info(0x0000, 'testTag', 'getAllBundleResourceInfo failed. err: %{public}s', err.message); return; } hilog.info(0x0000, 'testTag', 'getAllBundleResourceInfo successfully. Data length: %{public}s', JSON.stringify(data.length)); }); } catch (err) { let message = (err as BusinessError).message; hilog.error(0x0000, 'testTag', 'getAllBundleResourceInfo failed: %{public}s', message); } ``` ### bundleResourceManager.getAllBundleResourceInfo getAllBundleResourceInfo(resourceFlags: [number](#resourceflag)): Promise>; 以异步方法根据给定的resourceFlags获取所有应用的BundleResourceInfo。使用Promise异步回调。 **系统接口:** 此接口为系统接口。 **需要权限:** ohos.permission.GET_INSTALLED_BUNDLE_LIST 和 ohos.permission.GET_BUNDLE_RESOURCES **系统能力:** SystemCapability.BundleManager.BundleFramework.Resource **参数:** | 参数名 | 类型 | 必填 | 说明 | | ----------- | ------ | ---- | --------------------- | | resourceFlags | [number](#resourceflag) | 是 | 指定返回的BundleResourceInfo所包含的信息。 | **返回值:** | 类型 | 说明 | | ------------------------------------------------------------ | -------------------------------- | | Promise\> | Promise对象,返回BundleResourceInfo数值。 | **错误码:** 以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 | 错误码ID | 错误信息 | | -------- | ------------------------------------- | | 201 | 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.| **示例:** ```ts import bundleResourceManager from '@ohos.bundle.bundleResourceManager'; import { BusinessError } from '@ohos.base'; import hilog from '@ohos.hilog'; let bundleFlags = bundleResourceManager.ResourceFlag.GET_RESOURCE_INFO_ALL; try { bundleResourceManager.getAllBundleResourceInfo(bundleFlags).then(data=> { hilog.info(0x0000, 'testTag', 'getAllBundleResourceInfo successfully. Data length: %{public}s', JSON.stringify(data.length)); }).catch((err: BusinessError) => { hilog.info(0x0000, 'testTag', 'getAllBundleResourceInfo failed. err: %{public}s', err.message); }) } catch (err) { let message = (err as BusinessError).message; hilog.error(0x0000, 'testTag', 'getAllBundleResourceInfo failed: %{public}s', message); } ``` ### bundleResourceManager.getAllLauncherAbilityResourceInfo getAllLauncherAbilityResourceInfo(resourceFlags: [number](#resourceflag), callback: AsyncCallback>): void 以异步方法根据给定的resourceFlags获取当前所有应用的LauncherAbilityResourceInfo。使用callback异步回调。 **系统接口:** 此接口为系统接口。 **需要权限:** ohos.permission.GET_INSTALLED_BUNDLE_LIST 和 ohos.permission.GET_BUNDLE_RESOURCES **系统能力:** SystemCapability.BundleManager.BundleFramework.Resource **参数:** | 参数名 | 类型 | 必填 | 说明 | | ----------- | ------ | ---- | --------------------- | | resourceFlags | [number](#resourceflag) | 是 | 指定返回的LauncherAbilityResourceInfo所包含的信息。 | | callback | AsyncCallback\> | 是 | 回调函数,当获取成功时,err为null,data为获取到的LauncherAbilityResourceInfo数值;否则为错误对象。 | **错误码:** 以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 | 错误码ID | 错误信息 | | -------- | ------------------------------------- | | 201 | 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.| **示例:** ```ts import bundleResourceManager from '@ohos.bundle.bundleResourceManager'; import { BusinessError } from '@ohos.base'; import hilog from '@ohos.hilog'; let bundleFlags = bundleResourceManager.ResourceFlag.GET_RESOURCE_INFO_ALL; try { bundleResourceManager.getAllLauncherAbilityResourceInfo(bundleFlags, (err, data) => { if (err) { hilog.info(0x0000, 'testTag', 'getAllLauncherAbilityResourceInfo failed. err: %{public}s', err.message); return; } hilog.info(0x0000, 'testTag', 'getAllLauncherAbilityResourceInfo successfully. Data length: %{public}s', JSON.stringify(data.length)); }); } catch (err) { let message = (err as BusinessError).message; hilog.error(0x0000, 'testTag', 'getAllLauncherAbilityResourceInfo failed: %{public}s', message); } ``` ### bundleResourceManager.getAllLauncherAbilityResourceInfo getAllLauncherAbilityResourceInfo(resourceFlags: [number](#resourceflag)) : Promise> 以异步方法根据给定的resourceFlags获取当前所有应用的LauncherAbilityResourceInfo。使用Promise异步回调。 **系统接口:** 此接口为系统接口。 **需要权限:** ohos.permission.GET_INSTALLED_BUNDLE_LIST 和 ohos.permission.GET_BUNDLE_RESOURCES **系统能力:** SystemCapability.BundleManager.BundleFramework.Resource **参数:** | 参数名 | 类型 | 必填 | 说明 | | ----------- | ------ | ---- | --------------------- | | resourceFlags | [number](#resourceflag) | 是 | 指定返回的LauncherAbilityResourceInfo所包含的信息。 | **返回值:** | 类型 | 说明 | | ------------------------------------------------------------ | -------------------------------- | | Promise\> | Promise对象,返回LauncherAbilityResourceInfo数值。 | **错误码:** 以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 | 错误码ID | 错误信息 | | -------- | ------------------------------------- | | 201 | 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.| **示例:** ```ts import bundleResourceManager from '@ohos.bundle.bundleResourceManager'; import { BusinessError } from '@ohos.base'; import hilog from '@ohos.hilog'; let bundleFlags = bundleResourceManager.ResourceFlag.GET_RESOURCE_INFO_ALL; try { bundleResourceManager.getAllLauncherAbilityResourceInfo(bundleFlags).then(data=> { hilog.info(0x0000, 'testTag', 'getAllLauncherAbilityResourceInfo successfully. Data length: %{public}s', JSON.stringify(data.length)); }).catch((err: BusinessError) => { hilog.info(0x0000, 'testTag', 'getAllLauncherAbilityResourceInfo failed. err: %{public}s', err.message); }) } catch (err) { let message = (err as BusinessError).message; hilog.error(0x0000, 'testTag', 'getAllLauncherAbilityResourceInfo failed: %{public}s', message); } ``` ### bundleResourceManager.getBundleResourceInfo12+ getBundleResourceInfo(bundleName: string, resourceFlags?: [number](#resourceflag), appIndex?: number): [BundleResourceInfo](js-apis-bundleManager-BundleResourceInfo-sys.md) 以同步方法根据给定的bundleName、resourceFlags和appIndex获取当前应用的BundleResourceInfo。 **系统接口:** 此接口为系统接口。 **需要权限:** ohos.permission.GET_BUNDLE_RESOURCES **系统能力:** SystemCapability.BundleManager.BundleFramework.Resource **参数:** | 参数名 | 类型 | 必填 | 说明 | | ----------- | ------ | ---- | --------------------- | | bundleName | string | 是 | 指定查询应用的包名。 | | resourceFlags | [number](#resourceflag) | 否 | 指定返回的BundleResourceInfo所包含的信息。 | | appIndex | number | 否 | 指定查询应用分身的ID。 | **返回值:** | 类型 | 说明 | | ----------------------------------------------------------- | ------------------------------------- | | [BundleResourceInfo](js-apis-bundleManager-BundleResourceInfo-sys.md) | 返回指定应用的BundleResourceInfo。| **错误码:** 以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.bundle错误码](errorcode-bundle.md)。 | 错误码ID | 错误信息 | | -------- | ------------------------------------- | | 201 | 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 bundleName is not found. | | 17700061 | AppIndex not in valid range or not found. | **示例:** ```ts import bundleResourceManager from '@ohos.bundle.bundleResourceManager'; import { BusinessError } from '@ohos.base'; import hilog from '@ohos.hilog'; let bundleName = "com.example.myapplication"; let bundleFlags = bundleResourceManager.ResourceFlag.GET_RESOURCE_INFO_ALL; let appIndex = 1; try { let resourceInfo = bundleResourceManager.getBundleResourceInfo(bundleName, bundleFlags, appIndex); hilog.info(0x0000, 'testTag', 'getBundleResourceInfo successfully. Data label: %{public}s', JSON.stringify(resourceInfo.label)); } catch (err) { let message = (err as BusinessError).message; hilog.error(0x0000, 'testTag', 'getBundleResourceInfo failed: %{public}s', message); } ``` ### bundleResourceManager.getLauncherAbilityResourceInfo12+ getLauncherAbilityResourceInfo(bundleName: string, resourceFlags?: [number](#resourceflag), appIndex?: number): Array<[LauncherAbilityResourceInfo](js-apis-bundleManager-LauncherAbilityResourceInfo-sys.md)> 以同步方法根据给定的bundleName、resourceFlags和appIndex获取当前应用的LauncherAbilityResourceInfo。 **系统接口:** 此接口为系统接口。 **需要权限:** ohos.permission.GET_BUNDLE_RESOURCES **系统能力:** SystemCapability.BundleManager.BundleFramework.Resource **参数:** | 参数名 | 类型 | 必填 | 说明 | | ----------- | ------ | ---- | --------------------- | | bundleName | string | 是 | 指定查询应用的包名。 | | resourceFlags | [number](#resourceflag) | 否 | 指定返回的LauncherAbilityResourceInfo所包含的信息。 | | appIndex | number | 否 | 指定查询应用分身的ID。 | **返回值:** | 类型 | 说明 | | ----------------------------------------------------------- | ------------------------------------- | | Array<[LauncherAbilityResourceInfo](js-apis-bundleManager-LauncherAbilityResourceInfo-sys.md)> | 返回指定应用的LauncherAbilityResourceInfo。| **错误码:** 以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.bundle错误码](errorcode-bundle.md)。 | 错误码ID | 错误信息 | | -------- | ------------------------------------- | | 201 | 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 bundleName is not found. | | 17700061 | AppIndex not in valid range or not found. | **示例:** ```ts import bundleResourceManager from '@ohos.bundle.bundleResourceManager'; import { BusinessError } from '@ohos.base'; import hilog from '@ohos.hilog'; let bundleName = "com.example.myapplication"; let bundleFlags = bundleResourceManager.ResourceFlag.GET_RESOURCE_INFO_ALL; let appIndex = 1; try { let resourceInfo = bundleResourceManager.getLauncherAbilityResourceInfo(bundleName, bundleFlags, appIndex); hilog.info(0x0000, 'testTag', 'getLauncherAbilityResourceInfo successfully. Data label: %{public}s', JSON.stringify(resourceInfo[0].label)); } catch (err) { let message = (err as BusinessError).message; hilog.error(0x0000, 'testTag', 'getLauncherAbilityResourceInfo failed: %{public}s', message); } ```