harmony 鸿蒙@ohos.bundle.bundleResourceManager (bundleResourceManager模块)(系统接口)
@ohos.bundle.bundleResourceManager (bundleResourceManager模块)(系统接口)
本模块提供应用资源数据查询能力,支持BundleResourceInfo和LauncherAbilityResourceInfo等信息的查询。
说明:
本模块首批接口从API version 11 开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
本模块从API version 12 开始支持查询被禁用应用和设备上已安装应用(不区用户)的图标和名称资源。
本模块为系统接口。
导入模块
import { bundleResourceManager } from '@kit.AbilityKit';
枚举
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对象。 |
GET_RESOURCE_INFO_ONLY_WITH_MAIN_ABILITY20+ | 0x00000020 | 用于获取仅在桌面上展示图标的Ability资源,它仅在getLauncherAbilityResourceInfo和getAllLauncherAbilityResourceInfo接口中生效。 |
接口
bundleResourceManager.getBundleResourceInfo
getBundleResourceInfo(bundleName: string, resourceFlags?: number): BundleResourceInfo
以同步方法根据给定的bundleName和resourceFlags获取当前应用的BundleResourceInfo。
系统接口: 此接口为系统接口。
需要权限: ohos.permission.GET_BUNDLE_RESOURCES
系统能力: SystemCapability.BundleManager.BundleFramework.Resource
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
bundleName | string | 是 | 指定查询应用的包名。 |
resourceFlags | number | 否 | 指定返回的BundleResourceInfo所包含的信息。 |
返回值:
类型 | 说明 |
---|---|
BundleResourceInfo | 返回指定应用的BundleResourceInfo。 |
错误码:
以下错误码的详细介绍请参见通用错误码和ohos.bundle错误码。
错误码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. |
示例:
import { bundleResourceManager } from '@kit.AbilityKit';
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): Array<LauncherAbilityResourceInfo>
以同步方法根据给定的bundleName和resourceFlags获取当前应用的LauncherAbilityResourceInfo。
系统接口: 此接口为系统接口。
需要权限: ohos.permission.GET_BUNDLE_RESOURCES
系统能力: SystemCapability.BundleManager.BundleFramework.Resource
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
bundleName | string | 是 | 指定查询应用的包名。 |
resourceFlags | number | 否 | 指定返回的LauncherAbilityResourceInfo所包含的信息,默认值为ResourceFlag.GET_RESOURCE_INFO_ALL。 |
返回值:
类型 | 说明 |
---|---|
Array<LauncherAbilityResourceInfo> | 返回指定应用的LauncherAbilityResourceInfo。 |
错误码:
以下错误码的详细介绍请参见通用错误码和ohos.bundle错误码。
错误码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. |
示例:
import { bundleResourceManager } from '@kit.AbilityKit';
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, callback: AsyncCallback
根据给定的resourceFlags获取所有应用的BundleResourceInfo。使用callback异步回调。
系统接口: 此接口为系统接口。
需要权限: ohos.permission.GET_INSTALLED_BUNDLE_LIST 和 ohos.permission.GET_BUNDLE_RESOURCES
系统能力: SystemCapability.BundleManager.BundleFramework.Resource
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
resourceFlags | number | 是 | 指定返回的BundleResourceInfo所包含的信息。 |
callback | AsyncCallback<Array<BundleResourceInfo>> | 是 | 回调函数,当获取成功时,err为null,data为获取到的BundleResourceInfo数值;否则为错误对象。 |
错误码:
以下错误码的详细介绍请参见通用错误码。
错误码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. |
示例:
import { bundleResourceManager } from '@kit.AbilityKit';
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.error(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): Promise
根据给定的resourceFlags获取所有应用的BundleResourceInfo。使用Promise异步回调。
系统接口: 此接口为系统接口。
需要权限: ohos.permission.GET_INSTALLED_BUNDLE_LIST 和 ohos.permission.GET_BUNDLE_RESOURCES
系统能力: SystemCapability.BundleManager.BundleFramework.Resource
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
resourceFlags | number | 是 | 指定返回的BundleResourceInfo所包含的信息。 |
返回值:
类型 | 说明 |
---|---|
Promise<Array<BundleResourceInfo>> | Promise对象,返回BundleResourceInfo数值。 |
错误码:
以下错误码的详细介绍请参见通用错误码。
错误码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. |
示例:
import { bundleResourceManager } from '@kit.AbilityKit';
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.error(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, callback: AsyncCallback
根据给定的resourceFlags获取当前所有应用的LauncherAbilityResourceInfo。使用callback异步回调。
系统接口: 此接口为系统接口。
需要权限: ohos.permission.GET_INSTALLED_BUNDLE_LIST 和 ohos.permission.GET_BUNDLE_RESOURCES
系统能力: SystemCapability.BundleManager.BundleFramework.Resource
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
resourceFlags | number | 是 | 指定返回的LauncherAbilityResourceInfo所包含的信息。 |
callback | AsyncCallback<Array<LauncherAbilityResourceInfo>> | 是 | 回调函数,当获取成功时,err为null,data为获取到的LauncherAbilityResourceInfo数值;否则为错误对象。 |
错误码:
以下错误码的详细介绍请参见通用错误码。
错误码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. |
示例:
import { bundleResourceManager } from '@kit.AbilityKit';
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.error(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): Promise
根据给定的resourceFlags获取当前所有应用的LauncherAbilityResourceInfo。使用Promise异步回调。
系统接口: 此接口为系统接口。
需要权限: ohos.permission.GET_INSTALLED_BUNDLE_LIST 和 ohos.permission.GET_BUNDLE_RESOURCES
系统能力: SystemCapability.BundleManager.BundleFramework.Resource
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
resourceFlags | number | 是 | 指定返回的LauncherAbilityResourceInfo所包含的信息。 |
返回值:
类型 | 说明 |
---|---|
Promise<Array<LauncherAbilityResourceInfo>> | Promise对象,返回LauncherAbilityResourceInfo数值。 |
错误码:
以下错误码的详细介绍请参见通用错误码。
错误码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. |
示例:
import { bundleResourceManager } from '@kit.AbilityKit';
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.error(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, appIndex?: number): BundleResourceInfo
以同步方法根据给定的bundleName、resourceFlags和appIndex获取当前应用的BundleResourceInfo。
系统接口: 此接口为系统接口。
需要权限: ohos.permission.GET_BUNDLE_RESOURCES
系统能力: SystemCapability.BundleManager.BundleFramework.Resource
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
bundleName | string | 是 | 指定查询应用的包名。 |
resourceFlags | number | 否 | 指定返回的BundleResourceInfo所包含的信息。 |
appIndex | number | 否 | 指定查询应用分身的ID,默认值为0。 |
返回值:
类型 | 说明 |
---|---|
BundleResourceInfo | 返回指定应用的BundleResourceInfo。 |
错误码:
以下错误码的详细介绍请参见通用错误码和ohos.bundle错误码。
错误码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. |
示例:
import { bundleResourceManager } from '@kit.AbilityKit';
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, appIndex?: number): Array<LauncherAbilityResourceInfo>
以同步方法根据给定的bundleName、resourceFlags和appIndex获取当前应用的LauncherAbilityResourceInfo。
系统接口: 此接口为系统接口。
需要权限: ohos.permission.GET_BUNDLE_RESOURCES
系统能力: SystemCapability.BundleManager.BundleFramework.Resource
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
bundleName | string | 是 | 指定查询应用的包名。 |
resourceFlags | number | 否 | 指定返回的LauncherAbilityResourceInfo所包含的信息,默认值为ResourceFlag.GET_RESOURCE_INFO_ALL。 |
appIndex | number | 否 | 指定查询应用分身的ID,默认值为0。 |
返回值:
类型 | 说明 |
---|---|
Array<LauncherAbilityResourceInfo> | 返回指定应用的LauncherAbilityResourceInfo。 |
错误码:
以下错误码的详细介绍请参见通用错误码和ohos.bundle错误码。
错误码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. |
示例:
import { bundleResourceManager } from '@kit.AbilityKit';
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);
}
bundleResourceManager.getExtensionAbilityResourceInfo20+
getExtensionAbilityResourceInfo(bundleName: string, extensionAbilityType: bundleManager.ExtensionAbilityType, resourceFlags: number, appIndex?: number): Array<LauncherAbilityResourceInfo>
根据应用包名、扩展组件类型、资源信息标志、应用分身ID获取应用的扩展组件资源。使用同步方式返回。
系统接口: 此接口为系统接口。
需要权限: ohos.permission.GET_BUNDLE_RESOURCES
系统能力: SystemCapability.BundleManager.BundleFramework.Resource
参数:
参数名 | 类型 | 必填 | 说明 |
---|---|---|---|
bundleName | string | 是 | 应用包名。 |
extensionAbilityType | bundleManager.ExtensionAbilityType | 是 | 应用的扩展组件类型,仅支持ExtensionAbilityType.INPUT_METHOD、ExtensionAbilityType.SHARE、ExtensionAbilityType.ACTION。 |
resourceFlags | number | 是 | 资源信息标志,指示需要获取的资源信息的内容。 |
appIndex | number | 否 | 应用分身的ID,默认值是0。取值范围0~5,取值为0表示主应用。 |
返回值:
类型 | 说明 |
---|---|
Array<LauncherAbilityResourceInfo> | 返回指定应用的配置入口图标和名称信息。 |
错误码:
以下错误码的详细介绍请参见通用错误码和包管理子系统通用错误码。
错误码ID | 错误信息 |
---|---|
201 | Permission denied. |
202 | Permission denied,non-system app called system api. |
17700001 | The specified bundleName is not found. |
17700061 | AppIndex not in valid range or not found. |
示例:
import { bundleManager, bundleResourceManager } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
let bundleName = "com.example.myapplication";
let extensionAbilityType = bundleManager.ExtensionAbilityType.INPUT_METHOD;
let bundleFlags = bundleResourceManager.ResourceFlag.GET_RESOURCE_INFO_ALL;
try {
let resourceInfo = bundleResourceManager.getExtensionAbilityResourceInfo(bundleName, extensionAbilityType, bundleFlags);
console.info('getExtensionAbilityResourceInfo successfully. Data label: ' + JSON.stringify(resourceInfo[0].label));
} catch (err) {
let message = (err as BusinessError).message;
let code = (err as BusinessError).code;
console.error(`getExtensionAbilityResourceInfo failed, err code:${code}, err msg: ${message}`);
}
你可能感兴趣的鸿蒙文章
harmony 鸿蒙OH_NativeBundle_ApplicationInfo
harmony 鸿蒙OH_NativeBundle_ElementName
harmony 鸿蒙ability_base_common.h
- 所属分类: 后端技术
- 本文标签:
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦