1# @ohos.bundle.bundleResourceManager (bundleResourceManager模块)(系统接口)
2
3本模块提供应用资源数据查询能力,支持[BundleResourceInfo](js-apis-bundleManager-BundleResourceInfo-sys.md)和[LauncherAbilityResourceInfo](js-apis-bundleManager-LauncherAbilityResourceInfo-sys.md)等信息的查询。
4
5> **说明:**
6>
7> 本模块首批接口从API version 11 开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8>
9> 本模块从API version 12 开始支持查询被禁用应用和设备上已安装应用(不区用户)的图标和名称资源。
10>
11> 本模块为系统接口。
12
13## 导入模块
14
15```ts
16import bundleResourceManager from '@ohos.bundle.bundleResourceManager';
17```
18
19## 权限列表
20
21| 权限                                       | 权限等级     | 描述            |
22| ------------------------------------------ | ------------ | ------------------|
23| ohos.permission.GET_BUNDLE_RESOURCES| system_basic | 允许查询应用的资源信息。 |
24| ohos.permission.GET_INSTALLED_BUNDLE_LIST | system_basic | 读取已安装应用列表。 |
25
26权限等级参考[权限APL等级说明](../../security/AccessToken/app-permission-mgmt-overview.md#权限机制中的基本概念)。
27
28## 枚举
29
30### ResourceFlag
31
32资源信息标志,指示需要获取的资源信息的内容。
33
34**系统接口:** 此接口为系统接口。
35
36 **系统能力:** 以下各项对应的系统能力均为SystemCapability.BundleManager.BundleFramework.Resource37
38| 名称                                      | 值         | 说明                                                         |
39| ----------------------------------------- | ---------- | ------------------------------------------------------------ |
40| GET_RESOURCE_INFO_ALL                   | 0x00000001 | 用于同时获取icon和label信息。|
41| GET_RESOURCE_INFO_WITH_LABEL          | 0x00000002 | 用于获取仅包含label信息,iocn信息为空。 |
42| GET_RESOURCE_INFO_WITH_ICON           | 0x00000004 | 用于获取仅包含icon信息,label信息为空。 |
43| GET_RESOURCE_INFO_WITH_SORTED_BY_LABEL| 0x00000008 | 用于获取根据label排序后的信息。它不能单独使用需要与GET_RESOURCE_INFO_ALL 或 GET_RESOURCE_INFO_WITH_LABEL一起使用。 |
44| GET_RESOURCE_INFO_WITH_DRAWABLE_DESCRIPTOR<sup>12+</sup>| 0x00000010 | 用于获取应用图标的[drawableDescriptor](../apis-arkui/js-apis-arkui-drawableDescriptor-sys.md)对象。 |
45
46
47## 接口
48
49### bundleResourceManager.getBundleResourceInfo
50
51getBundleResourceInfo(bundleName: string, resourceFlags?: [number](#resourceflag)): [BundleResourceInfo](js-apis-bundleManager-BundleResourceInfo-sys.md)
52
53以同步方法根据给定的bundleName和resourceFlags获取当前应用的BundleResourceInfo。
54
55**系统接口:** 此接口为系统接口。
56
57**需要权限:** ohos.permission.GET_BUNDLE_RESOURCES
58
59**系统能力:** SystemCapability.BundleManager.BundleFramework.Resource
60
61**参数:**
62
63| 参数名     | 类型   | 必填 | 说明                |
64| ----------- | ------ | ---- | --------------------- |
65| bundleName | string | 是   | 指定查询应用的包名。 |
66| resourceFlags | [number](#resourceflag) | 否   | 指定返回的BundleResourceInfo所包含的信息。 |
67
68**返回值:**
69
70| 类型                                                        | 说明                                  |
71| ----------------------------------------------------------- | ------------------------------------- |
72| [BundleResourceInfo](js-apis-bundleManager-BundleResourceInfo-sys.md) | 返回指定应用的BundleResourceInfo。|
73
74
75**错误码:**
76
77以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.bundle错误码](errorcode-bundle.md)。
78
79| 错误码ID | 错误信息                              |
80| -------- | ------------------------------------- |
81| 201 | Permission denied. |
82| 202 | Permission denied, non-system app called system api. |
83| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.|
84| 17700001 | The specified bundleName is not found. |
85
86
87**示例:**
88
89```ts
90import bundleResourceManager from '@ohos.bundle.bundleResourceManager';
91import { BusinessError } from '@ohos.base';
92import hilog from '@ohos.hilog';
93let bundleName = "com.example.myapplication";
94let bundleFlags = bundleResourceManager.ResourceFlag.GET_RESOURCE_INFO_ALL;
95try {
96    let resourceInfo = bundleResourceManager.getBundleResourceInfo(bundleName, bundleFlags);
97    hilog.info(0x0000, 'testTag', 'getBundleResourceInfo successfully. Data label: %{public}s', JSON.stringify(resourceInfo.label));
98} catch (err) {
99    let message = (err as BusinessError).message;
100    hilog.error(0x0000, 'testTag', 'getBundleResourceInfo failed: %{public}s', message);
101}
102```
103
104### bundleResourceManager.getLauncherAbilityResourceInfo
105
106getLauncherAbilityResourceInfo(bundleName: string, resourceFlags?: [number](#resourceflag)): Array<[LauncherAbilityResourceInfo](js-apis-bundleManager-LauncherAbilityResourceInfo-sys.md)>
107
108以同步方法根据给定的bundleName和resourceFlags获取当前应用的LauncherAbilityResourceInfo。
109
110**系统接口:** 此接口为系统接口。
111
112**需要权限:** ohos.permission.GET_BUNDLE_RESOURCES
113
114**系统能力:** SystemCapability.BundleManager.BundleFramework.Resource
115
116**参数:**
117
118| 参数名     | 类型   | 必填 | 说明                |
119| ----------- | ------ | ---- | --------------------- |
120| bundleName | string | 是   | 指定查询应用的包名。 |
121| resourceFlags | [number](#resourceflag) | 否   | 指定返回的LauncherAbilityResourceInfo所包含的信息。 |
122
123**返回值:**
124
125| 类型                                                        | 说明                                  |
126| ----------------------------------------------------------- | ------------------------------------- |
127| Array<[LauncherAbilityResourceInfo](js-apis-bundleManager-LauncherAbilityResourceInfo-sys.md)> | 返回指定应用的LauncherAbilityResourceInfo。|
128
129**错误码:**
130
131以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.bundle错误码](errorcode-bundle.md)。
132
133| 错误码ID | 错误信息                              |
134| -------- | ------------------------------------- |
135| 201 | Permission denied. |
136| 202 | Permission denied, non-system app called system api. |
137| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.|
138| 17700001 | The specified bundleName is not found. |
139
140
141**示例:**
142
143```ts
144import bundleResourceManager from '@ohos.bundle.bundleResourceManager';
145import { BusinessError } from '@ohos.base';
146import hilog from '@ohos.hilog';
147let bundleName = "com.example.myapplication";
148let bundleFlags = bundleResourceManager.ResourceFlag.GET_RESOURCE_INFO_ALL;
149try {
150    let resourceInfo = bundleResourceManager.getLauncherAbilityResourceInfo(bundleName, bundleFlags);
151    hilog.info(0x0000, 'testTag', 'getLauncherAbilityResourceInfo successfully. Data label: %{public}s', JSON.stringify(resourceInfo[0].label));
152} catch (err) {
153    let message = (err as BusinessError).message;
154    hilog.error(0x0000, 'testTag', 'getLauncherAbilityResourceInfo failed: %{public}s', message);
155}
156```
157
158### bundleResourceManager.getAllBundleResourceInfo
159
160getAllBundleResourceInfo(resourceFlags: [number](#resourceflag), callback: AsyncCallback<Array<[BundleResourceInfo](js-apis-bundleManager-BundleResourceInfo-sys.md)>>): void
161
162以异步方法根据给定的resourceFlags获取所有应用的BundleResourceInfo。使用callback异步回调。
163
164**系统接口:** 此接口为系统接口。
165
166**需要权限:** ohos.permission.GET_INSTALLED_BUNDLE_LISTohos.permission.GET_BUNDLE_RESOURCES
167
168**系统能力:** SystemCapability.BundleManager.BundleFramework.Resource
169
170**参数:**
171
172| 参数名     | 类型   | 必填 | 说明                |
173| ----------- | ------ | ---- | --------------------- |
174| resourceFlags | [number](#resourceflag) | 是   | 指定返回的BundleResourceInfo所包含的信息。 |
175| callback | AsyncCallback\<Array<[BundleResourceInfo](js-apis-bundleManager-BundleResourceInfo-sys.md)>> | 是 | 回调函数,当获取成功时,err为null,data为获取到的BundleResourceInfo数值;否则为错误对象。 |
176
177**错误码:**
178
179以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
180
181| 错误码ID | 错误信息                              |
182| -------- | ------------------------------------- |
183| 201 | Permission denied. |
184| 202 | Permission denied, non-system app called system api. |
185| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.|
186
187**示例:**
188
189```ts
190import bundleResourceManager from '@ohos.bundle.bundleResourceManager';
191import { BusinessError } from '@ohos.base';
192import hilog from '@ohos.hilog';
193let bundleFlags = bundleResourceManager.ResourceFlag.GET_RESOURCE_INFO_ALL;
194try {
195    bundleResourceManager.getAllBundleResourceInfo(bundleFlags, (err, data) => {
196        if (err) {
197            hilog.info(0x0000, 'testTag', 'getAllBundleResourceInfo failed. err: %{public}s', err.message);
198            return;
199        }
200        hilog.info(0x0000, 'testTag', 'getAllBundleResourceInfo successfully. Data length: %{public}s', JSON.stringify(data.length));
201    });
202} catch (err) {
203    let message = (err as BusinessError).message;
204    hilog.error(0x0000, 'testTag', 'getAllBundleResourceInfo failed: %{public}s', message);
205}
206```
207
208### bundleResourceManager.getAllBundleResourceInfo
209
210getAllBundleResourceInfo(resourceFlags: [number](#resourceflag)): Promise<Array<[BundleResourceInfo](js-apis-bundleManager-BundleResourceInfo-sys.md)>>;
211
212以异步方法根据给定的resourceFlags获取所有应用的BundleResourceInfo。使用Promise异步回调。
213
214**系统接口:** 此接口为系统接口。
215
216**需要权限:** ohos.permission.GET_INSTALLED_BUNDLE_LISTohos.permission.GET_BUNDLE_RESOURCES
217
218**系统能力:** SystemCapability.BundleManager.BundleFramework.Resource
219
220**参数:**
221
222| 参数名     | 类型   | 必填 | 说明                |
223| ----------- | ------ | ---- | --------------------- |
224| resourceFlags | [number](#resourceflag) | 是   | 指定返回的BundleResourceInfo所包含的信息。 |
225
226**返回值:**
227
228| 类型                                                         | 说明                             |
229| ------------------------------------------------------------ | -------------------------------- |
230| Promise\<Array<[BundleResourceInfo](js-apis-bundleManager-BundleResourceInfo-sys.md)>> | Promise对象,返回BundleResourceInfo数值。 |
231
232**错误码:**
233
234以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
235
236| 错误码ID | 错误信息                              |
237| -------- | ------------------------------------- |
238| 201 | Permission denied. |
239| 202 | Permission denied, non-system app called system api. |
240| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.|
241
242**示例:**
243
244```ts
245import bundleResourceManager from '@ohos.bundle.bundleResourceManager';
246import { BusinessError } from '@ohos.base';
247import hilog from '@ohos.hilog';
248let bundleFlags = bundleResourceManager.ResourceFlag.GET_RESOURCE_INFO_ALL;
249try {
250    bundleResourceManager.getAllBundleResourceInfo(bundleFlags).then(data=> {
251        hilog.info(0x0000, 'testTag', 'getAllBundleResourceInfo successfully. Data length: %{public}s', JSON.stringify(data.length));
252    }).catch((err: BusinessError) => {
253        hilog.info(0x0000, 'testTag', 'getAllBundleResourceInfo failed. err: %{public}s', err.message);
254    })
255} catch (err) {
256    let message = (err as BusinessError).message;
257    hilog.error(0x0000, 'testTag', 'getAllBundleResourceInfo failed: %{public}s', message);
258}
259```
260
261### bundleResourceManager.getAllLauncherAbilityResourceInfo
262
263getAllLauncherAbilityResourceInfo(resourceFlags: [number](#resourceflag), callback: AsyncCallback<Array<[LauncherAbilityResourceInfo](js-apis-bundleManager-LauncherAbilityResourceInfo-sys.md)>>): void
264
265以异步方法根据给定的resourceFlags获取当前所有应用的LauncherAbilityResourceInfo。使用callback异步回调。
266
267**系统接口:** 此接口为系统接口。
268
269**需要权限:** ohos.permission.GET_INSTALLED_BUNDLE_LISTohos.permission.GET_BUNDLE_RESOURCES
270
271**系统能力:** SystemCapability.BundleManager.BundleFramework.Resource
272
273**参数:**
274
275| 参数名     | 类型   | 必填 | 说明                |
276| ----------- | ------ | ---- | --------------------- |
277| resourceFlags | [number](#resourceflag) | 是   | 指定返回的LauncherAbilityResourceInfo所包含的信息。 |
278| callback | AsyncCallback\<Array<[LauncherAbilityResourceInfo](js-apis-bundleManager-LauncherAbilityResourceInfo-sys.md)>> | 是 | 回调函数,当获取成功时,err为null,data为获取到的LauncherAbilityResourceInfo数值;否则为错误对象。 |
279
280**错误码:**
281
282以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
283
284| 错误码ID | 错误信息                              |
285| -------- | ------------------------------------- |
286| 201 | Permission denied. |
287| 202 | Permission denied, non-system app called system api. |
288| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.|
289
290**示例:**
291
292```ts
293import bundleResourceManager from '@ohos.bundle.bundleResourceManager';
294import { BusinessError } from '@ohos.base';
295import hilog from '@ohos.hilog';
296let bundleFlags = bundleResourceManager.ResourceFlag.GET_RESOURCE_INFO_ALL;
297try {
298    bundleResourceManager.getAllLauncherAbilityResourceInfo(bundleFlags, (err, data) => {
299        if (err) {
300            hilog.info(0x0000, 'testTag', 'getAllLauncherAbilityResourceInfo failed. err: %{public}s', err.message);
301            return;
302        }
303        hilog.info(0x0000, 'testTag', 'getAllLauncherAbilityResourceInfo successfully. Data length: %{public}s', JSON.stringify(data.length));
304    });
305} catch (err) {
306    let message = (err as BusinessError).message;
307    hilog.error(0x0000, 'testTag', 'getAllLauncherAbilityResourceInfo failed: %{public}s', message);
308}
309```
310
311### bundleResourceManager.getAllLauncherAbilityResourceInfo
312
313getAllLauncherAbilityResourceInfo(resourceFlags: [number](#resourceflag)) : Promise<Array<[LauncherAbilityResourceInfo](js-apis-bundleManager-LauncherAbilityResourceInfo-sys.md)>>
314
315以异步方法根据给定的resourceFlags获取当前所有应用的LauncherAbilityResourceInfo。使用Promise异步回调。
316
317**系统接口:** 此接口为系统接口。
318
319**需要权限:** ohos.permission.GET_INSTALLED_BUNDLE_LISTohos.permission.GET_BUNDLE_RESOURCES
320
321**系统能力:** SystemCapability.BundleManager.BundleFramework.Resource
322
323**参数:**
324
325| 参数名     | 类型   | 必填 | 说明                |
326| ----------- | ------ | ---- | --------------------- |
327| resourceFlags | [number](#resourceflag) | 是   | 指定返回的LauncherAbilityResourceInfo所包含的信息。 |
328
329**返回值:**
330
331| 类型                                                         | 说明                             |
332| ------------------------------------------------------------ | -------------------------------- |
333| Promise\<Array<[LauncherAbilityResourceInfo](js-apis-bundleManager-LauncherAbilityResourceInfo-sys.md)>> | Promise对象,返回LauncherAbilityResourceInfo数值。 |
334
335**错误码:**
336
337以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
338
339| 错误码ID | 错误信息                              |
340| -------- | ------------------------------------- |
341| 201 | Permission denied. |
342| 202 | Permission denied, non-system app called system api. |
343| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.|
344
345**示例:**
346```ts
347import bundleResourceManager from '@ohos.bundle.bundleResourceManager';
348import { BusinessError } from '@ohos.base';
349import hilog from '@ohos.hilog';
350let bundleFlags = bundleResourceManager.ResourceFlag.GET_RESOURCE_INFO_ALL;
351try {
352    bundleResourceManager.getAllLauncherAbilityResourceInfo(bundleFlags).then(data=> {
353        hilog.info(0x0000, 'testTag', 'getAllLauncherAbilityResourceInfo successfully. Data length: %{public}s', JSON.stringify(data.length));
354    }).catch((err: BusinessError) => {
355        hilog.info(0x0000, 'testTag', 'getAllLauncherAbilityResourceInfo failed. err: %{public}s', err.message);
356    })
357} catch (err) {
358    let message = (err as BusinessError).message;
359    hilog.error(0x0000, 'testTag', 'getAllLauncherAbilityResourceInfo failed: %{public}s', message);
360}
361```
362
363### bundleResourceManager.getBundleResourceInfo<sup>12+</sup>
364
365getBundleResourceInfo(bundleName: string, resourceFlags?: [number](#resourceflag), appIndex?: number): [BundleResourceInfo](js-apis-bundleManager-BundleResourceInfo-sys.md)
366
367以同步方法根据给定的bundleName、resourceFlags和appIndex获取当前应用的BundleResourceInfo。
368
369**系统接口:** 此接口为系统接口。
370
371**需要权限:** ohos.permission.GET_BUNDLE_RESOURCES
372
373**系统能力:** SystemCapability.BundleManager.BundleFramework.Resource
374
375**参数:**
376
377| 参数名     | 类型   | 必填 | 说明                |
378| ----------- | ------ | ---- | --------------------- |
379| bundleName | string | 是   | 指定查询应用的包名。 |
380| resourceFlags | [number](#resourceflag) | 否   | 指定返回的BundleResourceInfo所包含的信息。 |
381| appIndex | number | 否   | 指定查询应用分身的ID。 |
382
383**返回值:**
384
385| 类型                                                        | 说明                                  |
386| ----------------------------------------------------------- | ------------------------------------- |
387| [BundleResourceInfo](js-apis-bundleManager-BundleResourceInfo-sys.md) | 返回指定应用的BundleResourceInfo。|
388
389
390**错误码:**
391
392以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.bundle错误码](errorcode-bundle.md)。
393
394| 错误码ID | 错误信息                              |
395| -------- | ------------------------------------- |
396| 201 | Permission denied. |
397| 202 | Permission denied, non-system app called system api. |
398| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.|
399| 17700001 | The specified bundleName is not found. |
400| 17700061 | AppIndex not in valid range or not found. |
401
402
403**示例:**
404
405```ts
406import bundleResourceManager from '@ohos.bundle.bundleResourceManager';
407import { BusinessError } from '@ohos.base';
408import hilog from '@ohos.hilog';
409let bundleName = "com.example.myapplication";
410let bundleFlags = bundleResourceManager.ResourceFlag.GET_RESOURCE_INFO_ALL;
411let appIndex = 1;
412try {
413    let resourceInfo = bundleResourceManager.getBundleResourceInfo(bundleName, bundleFlags, appIndex);
414    hilog.info(0x0000, 'testTag', 'getBundleResourceInfo successfully. Data label: %{public}s', JSON.stringify(resourceInfo.label));
415} catch (err) {
416    let message = (err as BusinessError).message;
417    hilog.error(0x0000, 'testTag', 'getBundleResourceInfo failed: %{public}s', message);
418}
419```
420
421### bundleResourceManager.getLauncherAbilityResourceInfo<sup>12+</sup>
422
423getLauncherAbilityResourceInfo(bundleName: string, resourceFlags?: [number](#resourceflag), appIndex?: number): Array<[LauncherAbilityResourceInfo](js-apis-bundleManager-LauncherAbilityResourceInfo-sys.md)>
424
425以同步方法根据给定的bundleName、resourceFlags和appIndex获取当前应用的LauncherAbilityResourceInfo。
426
427**系统接口:** 此接口为系统接口。
428
429**需要权限:** ohos.permission.GET_BUNDLE_RESOURCES
430
431**系统能力:** SystemCapability.BundleManager.BundleFramework.Resource
432
433**参数:**
434
435| 参数名     | 类型   | 必填 | 说明                |
436| ----------- | ------ | ---- | --------------------- |
437| bundleName | string | 是   | 指定查询应用的包名。 |
438| resourceFlags | [number](#resourceflag) | 否   | 指定返回的LauncherAbilityResourceInfo所包含的信息。 |
439| appIndex | number | 否   | 指定查询应用分身的ID。 |
440
441**返回值:**
442
443| 类型                                                        | 说明                                  |
444| ----------------------------------------------------------- | ------------------------------------- |
445| Array<[LauncherAbilityResourceInfo](js-apis-bundleManager-LauncherAbilityResourceInfo-sys.md)> | 返回指定应用的LauncherAbilityResourceInfo。|
446
447**错误码:**
448
449以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)和[ohos.bundle错误码](errorcode-bundle.md)。
450
451| 错误码ID | 错误信息                              |
452| -------- | ------------------------------------- |
453| 201 | Permission denied. |
454| 202 | Permission denied, non-system app called system api. |
455| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.|
456| 17700001 | The specified bundleName is not found. |
457| 17700061 | AppIndex not in valid range or not found. |
458
459**示例:**
460
461```ts
462import bundleResourceManager from '@ohos.bundle.bundleResourceManager';
463import { BusinessError } from '@ohos.base';
464import hilog from '@ohos.hilog';
465let bundleName = "com.example.myapplication";
466let bundleFlags = bundleResourceManager.ResourceFlag.GET_RESOURCE_INFO_ALL;
467let appIndex = 1;
468try {
469    let resourceInfo = bundleResourceManager.getLauncherAbilityResourceInfo(bundleName, bundleFlags, appIndex);
470    hilog.info(0x0000, 'testTag', 'getLauncherAbilityResourceInfo successfully. Data label: %{public}s', JSON.stringify(resourceInfo[0].label));
471} catch (err) {
472    let message = (err as BusinessError).message;
473    hilog.error(0x0000, 'testTag', 'getLauncherAbilityResourceInfo failed: %{public}s', message);
474}
475```