• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.bundle (Bundle模块)(系统接口)
2<!--Kit: Ability Kit-->
3<!--Subsystem: BundleManager-->
4<!--Owner: @wanghang904-->
5<!--Designer: @hanfeng6-->
6<!--Tester: @kongjing2-->
7<!--Adviser: @Brilliantry_Rui-->
8
9本模块提供应用信息查询能力,支持[包信息](js-apis-bundle-BundleInfo.md)、[应用信息](js-apis-bundle-ApplicationInfo.md)、[Ability组件信息](js-apis-bundle-AbilityInfo.md)等信息的查询,以及应用禁用状态的查询、设置等。
10
11> **说明:**
12>
13> 本模块首批接口从API version 7开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
14>
15> 从API Version 9开始,该模块不再维护,系统接口建议使用[`@ohos.bundle.bundleManager`](js-apis-bundleManager-sys.md)替代。
16>
17> 当前页面仅包含本模块的系统接口,其他公开接口参见[`@ohos.bundle`](js-apis-Bundle.md)。
18
19## 导入模块
20
21```ts
22import bundle from '@ohos.bundle';
23```
24
25## 权限列表
26
27| 权限                                         | 权限等级         | 描述            |
28|--------------------------------------------|--------------|---------------|
29| ohos.permission.CHANGE_ABILITY_ENABLED_STATE | system_basic | 设置禁用使能所需的权限。 |
30| ohos.permission.GET_BUNDLE_INFO | normal | 查询指定应用信息。 |
31| ohos.permission.GET_BUNDLE_INFO_PRIVILEGED | system_basic | 可查询所有应用信息。     |
32| ohos.permission.INSTALL_BUNDLE             | system_core  | 可安装、卸载应用。      |
33| ohos.permission.REMOVE_CACHE_FILES | system_basic | 清理应用缓存。 |
34
35权限等级参考[权限APL等级说明](../../security/AccessToken/app-permission-mgmt-overview.md#权限机制中的基本概念)。
36
37## bundle.getBundleInstaller<sup>deprecated<sup>
38
39> 从API version 9开始不再维护,建议使用[installer.getBundleInstaller](js-apis-installer-sys.md#bundleinstallergetbundleinstaller)替代。
40
41getBundleInstaller(): Promise&lt;BundleInstaller&gt;
42
43获取用于安装包的接口,使用Promise异步回调,返回安装接口对象。
44
45**需要权限:**
46
47ohos.permission.INSTALL_BUNDLE
48
49**系统能力:**
50
51SystemCapability.BundleManager.BundleFramework
52
53**系统接口:**
54
55此接口为系统接口。
56
57**返回值:**
58
59| 类型                                                         | 说明                                         |
60| ------------------------------------------------------------ | -------------------------------------------- |
61| Promise<[BundleInstaller](js-apis-bundle-BundleInstaller-sys.md)> | Promise对象,返回安装接口对象。 |
62
63**示例:**
64
65```ts
66import bundle from '@ohos.bundle';
67import { BusinessError } from '@ohos.base';
68
69bundle.getBundleInstaller().then((data) => {
70  console.info('getBundleInstaller successfully.');
71}).catch((error: BusinessError) => {
72  console.error('getBundleInstaller failed.');
73});
74```
75
76## bundle.getBundleInstaller<sup>deprecated<sup>
77
78> 从API version 9开始不再维护,建议使用[installer.getBundleInstaller](js-apis-installer-sys.md#bundleinstallergetbundleinstaller)替代。
79
80getBundleInstaller(callback: AsyncCallback&lt;BundleInstaller&gt;): void
81
82获取用于安装包的接口,使用callback异步回调。
83
84**需要权限:**
85
86ohos.permission.INSTALL_BUNDLE
87
88**系统能力:**
89
90SystemCapability.BundleManager.BundleFramework
91
92**系统接口:**
93
94此接口为系统接口。
95
96**参数:**
97
98| 参数名   | 类型                                                         | 必填 | 说明             |
99| -------- | ------------------------------------------------------------ | ---- | ---------------- |
100| callback | AsyncCallback<[BundleInstaller](js-apis-bundle-BundleInstaller-sys.md)> | 是   | 回调函数,返回安装接口对象。 |
101
102**示例:**
103
104```ts
105import bundle from '@ohos.bundle';
106
107bundle.getBundleInstaller((err, data) => {
108  if (err.code == 0) {
109    console.error('getBundleInstaller successfully.');
110  } else {
111    console.info('getBundleInstaller failed.');
112  }
113});
114```
115## bundle.cleanBundleCacheFiles<sup>8+</sup> <sup>deprecated<sup>
116
117> 从API version 9开始不再维护,建议使用[bundleManager.cleanBundleCacheFiles](js-apis-bundleManager-sys.md#bundlemanagercleanbundlecachefiles)替代。
118
119cleanBundleCacheFiles(bundleName: string, callback: AsyncCallback&lt;void&gt;): void
120
121清除指定应用程序的缓存数据,使用callback异步回调。
122
123**需要权限:**
124
125ohos.permission.REMOVE_CACHE_FILES
126
127**系统能力:**
128
129SystemCapability.BundleManager.BundleFramework
130
131**系统接口:**
132
133此接口为系统接口。
134
135**参数:**
136
137| 参数名      | 类型                | 必填 | 说明                                  |
138| ---------- | ------------------- | ---- | ------------------------------------- |
139| bundleName | string              | 是   | 指示要清除其缓存数据的应用Bundle名称。 |
140| callback   | AsyncCallback\<void> | 是   | 回调函数。          |
141
142**示例:**
143
144```ts
145import bundle from '@ohos.bundle';
146
147let bundleName: string = "com.example.myapplication";
148
149bundle.cleanBundleCacheFiles(bundleName, err => {
150  if (err) {
151    console.error('cleanBundleCacheFiles failed.');
152  } else {
153    console.info('cleanBundleCacheFiles successfully.');
154  }
155});
156```
157
158## bundle.cleanBundleCacheFiles<sup>8+</sup> <sup>deprecated<sup>
159
160> 从API version 9开始不再维护,建议使用[bundleManager.cleanBundleCacheFiles](js-apis-bundleManager-sys.md#bundlemanagercleanbundlecachefiles)替代。
161
162cleanBundleCacheFiles(bundleName: string): Promise&lt;void&gt;
163
164清除指定应用程序的缓存数据,使用Promise异步回调。
165
166**需要权限:**
167
168ohos.permission.REMOVE_CACHE_FILES
169
170**系统能力:**
171
172SystemCapability.BundleManager.BundleFramework
173
174**系统接口:**
175
176此接口为系统接口。
177
178**参数:**
179
180| 参数名     | 类型   | 必填 | 说明                                  |
181| ---------- | ------ | ---- | ------------------------------------- |
182| bundleName | string | 是   | 指示要清除其缓存数据的应用Bundle名称。 |
183
184**返回值:**
185
186| 类型          | 说明                                 |
187| ------------- | ------------------------------------ |
188| Promise\<void> | Promise对象,无返回结果的Promise对象。 |
189
190**示例:**
191
192```ts
193import bundle from '@ohos.bundle';
194import { BusinessError } from '@ohos.base';
195
196let bundleName: string = "com.example.myapplication";
197
198bundle.cleanBundleCacheFiles(bundleName).then(() => {
199  console.info('cleanBundleCacheFiles successfully.');
200}).catch((error: BusinessError) => {
201  console.error('cleanBundleCacheFiles failed.');
202});
203```
204
205## bundle.setApplicationEnabled<sup>8+</sup> <sup>deprecated<sup>
206
207> 从API version 9开始不再维护,建议使用[bundleManager.setApplicationEnabled](js-apis-bundleManager-sys.md#bundlemanagersetapplicationenabled)替代。
208
209setApplicationEnabled(bundleName: string, isEnable: boolean, callback: AsyncCallback&lt;void&gt;): void
210
211设置是否启用指定的应用程序,使用callback异步回调。
212
213**需要权限:**
214
215ohos.permission.CHANGE_ABILITY_ENABLED_STATE
216
217**系统能力:**
218
219SystemCapability.BundleManager.BundleFramework
220
221**系统接口:**
222
223此接口为系统接口。
224
225**参数:**
226
227| 参数名      | 类型                | 必填 | 说明                             |
228| ---------- | ------------------- | ---- |--------------------------------|
229| bundleName | string              | 是   | 指示需要启用或禁用的应用Bundle名称。          |
230| isEnable   | boolean             | 是   | 指定是否启用应用程序。true表示启用,false表示禁用。 |
231| callback   | AsyncCallback\<void> | 是   | 回调函数。                          |
232
233**示例:**
234
235```ts
236import bundle from '@ohos.bundle';
237
238let bundleName: string = "com.example.myapplication";
239
240bundle.setApplicationEnabled(bundleName, false, err => {
241  if (err) {
242    console.error('setApplicationEnabled failed.');
243  } else {
244    console.info('setApplicationEnabled successfully.');
245  }
246});
247```
248
249## bundle.setApplicationEnabled<sup>8+</sup> <sup>deprecated<sup>
250
251> 从API version 9开始不再维护,建议使用[bundleManager.setApplicationEnabled](js-apis-bundleManager-sys.md#bundlemanagersetapplicationenabled)替代。
252
253setApplicationEnabled(bundleName: string, isEnable: boolean): Promise&lt;void&gt;
254
255设置是否启用指定的应用程序,使用Promise异步回调。
256
257**需要权限:**
258
259ohos.permission.CHANGE_ABILITY_ENABLED_STATE
260
261**系统能力:**
262
263SystemCapability.BundleManager.BundleFramework
264
265**系统接口:**
266
267此接口为系统接口。
268
269**参数:**
270
271| 参数名     | 类型    | 必填 | 说明                                            |
272| ---------- | ------- | ---- | ----------------------------------------------- |
273| bundleName | string  | 是   | 指示需要启用或禁用的应用Bundle名称。            |
274| isEnable   | boolean | 是   | 指定是否启用应用程序。true表示启用,false禁用。 |
275
276**返回值:**
277
278| 类型          | 说明                                 |
279| ------------- | ------------------------------------ |
280| Promise\<void> | Promise对象,无返回结果的Promise对象。 |
281
282**示例:**
283
284```ts
285import bundle from '@ohos.bundle';
286import { BusinessError } from '@ohos.base';
287
288let bundleName: string = "com.example.myapplication";
289
290bundle.setApplicationEnabled(bundleName, false).then(() => {
291  console.info('setApplicationEnabled successfully.');
292}).catch((error: BusinessError) => {
293  console.error('setApplicationEnabled failed.');
294});
295```
296
297## bundle.setAbilityEnabled<sup>8+</sup> <sup>deprecated<sup>
298
299> 从API version 9开始不再维护,建议使用[bundleManager.setAbilityEnabled](js-apis-bundleManager-sys.md#bundlemanagersetabilityenabled)替代。
300
301setAbilityEnabled(info: AbilityInfo, isEnable: boolean, callback: AsyncCallback&lt;void&gt;): void
302
303设置是否启用指定的Ability组件,使用callback异步回调。
304
305**需要权限:**
306
307ohos.permission.CHANGE_ABILITY_ENABLED_STATE
308
309**系统能力:**
310
311SystemCapability.BundleManager.BundleFramework
312
313**系统接口:**
314
315此接口为系统接口。
316
317**参数:**
318
319| 参数名   | 类型                                         | 必填 | 说明                                            |
320| -------- | -------------------------------------------- | ---- | ----------------------------------------------- |
321| info     | [AbilityInfo](js-apis-bundle-AbilityInfo.md) | 是   | Ability信息,指示需要设置启用状态的Ability。                                   |
322| isEnable | boolean                                      | 是   | 指定是否启用应用程序。true表示启用,false禁用。 |
323| callback | AsyncCallback\<void>                         | 是   | 为返回操作结果而调用的回调。                    |
324
325## bundle.setAbilityEnabled<sup>8+</sup> <sup>deprecated<sup>
326
327> 从API version 9开始不再维护,建议使用[bundleManager.setAbilityEnabled](js-apis-bundleManager-sys.md#bundlemanagersetabilityenabled)替代。
328
329setAbilityEnabled(info: AbilityInfo, isEnable: boolean): Promise&lt;void&gt;
330
331设置是否启用指定的Ability组件,使用Promise异步回调。
332
333**需要权限:**
334
335ohos.permission.CHANGE_ABILITY_ENABLED_STATE
336
337**系统能力:**
338
339SystemCapability.BundleManager.BundleFramework
340
341**系统接口:**
342
343此接口为系统接口。
344
345**参数:**
346
347| 参数名   | 类型                                         | 必填 | 说明                                            |
348| -------- | -------------------------------------------- | ---- | ----------------------------------------------- |
349| info     | [AbilityInfo](js-apis-bundle-AbilityInfo.md) | 是   | Ability信息,指示需要设置启用状态的Ability。                                   |
350| isEnable | boolean                                      | 是   | 指定是否启用应用程序。true表示启用,false禁用。 |
351
352**返回值:**
353
354| 类型          | 说明                                 |
355| ------------- | ------------------------------------ |
356| Promise\<void> | Promise对象,无返回结果的Promise对象。 |
357
358**示例:**
359
360```ts
361import bundle from '@ohos.bundle';
362import { BusinessError } from '@ohos.base';
363
364let bundleName: string = "com.example.myapplication";
365let abilityName: string = "EntryAbility";
366
367bundle.getAbilityInfo(bundleName, abilityName).then((abilityInfo) => {
368  console.info('getAbilityInfo successfully. Data: ' + JSON.stringify(abilityInfo));
369
370  bundle.setAbilityEnabled(abilityInfo, false).then(data => {
371    console.info('setAbilityEnabled successfully.');
372  }).catch((error: BusinessError) => {
373    console.error('setAbilityEnabled failed:' + JSON.stringify(error));
374  })
375}).catch((error: BusinessError) => {
376  console.error('getAbilityInfo failed. Cause: ' + JSON.stringify(error));
377});
378```
379## bundle.getPermissionDef<sup>8+</sup> <sup>deprecated<sup>
380
381> 从API version 9开始不再维护,建议使用[bundleManager.getPermissionDef](js-apis-bundleManager-sys.md#bundlemanagergetpermissiondef)替代。
382
383getPermissionDef(permissionName: string, callback: AsyncCallback&lt;PermissionDef&gt;): void
384
385按权限名称获取权限的详细信息,使用callback异步回调。
386
387**需要权限:**
388
389ohos.permission.GET_BUNDLE_INFO_PRIVILEGED
390
391**系统能力:**
392
393SystemCapability.BundleManager.BundleFramework
394
395**系统接口:**
396
397此接口为系统接口。
398
399**参数:**
400
401| 参数名         | 类型                                                         | 必填 | 说明                                             |
402| -------------- | ------------------------------------------------------------ | ---- | ------------------------------------------------ |
403| permissionName | string                                                       | 是   | 需要查询的权限的名称。                                 |
404| callback       | AsyncCallback<[PermissionDef](js-apis-bundle-PermissionDef-sys.md)> | 是   | 程序启动作为入参的回调函数,返回定义的权限信息。 |
405
406**示例:**
407
408```ts
409import bundle from '@ohos.bundle';
410
411let permission: string = "ohos.permission.GET_BUNDLE_INFO";
412bundle.getPermissionDef(permission, (err, data) => {
413  if (err) {
414    console.error('getPermissionDef failed:' + err.message);
415  } else {
416    console.info('getPermissionDef successfully:' + JSON.stringify(data));
417  }
418});
419```
420
421## bundle.getPermissionDef<sup>8+</sup> <sup>deprecated<sup>
422
423> 从API version 9开始不再维护,建议使用[bundleManager.getPermissionDef](js-apis-bundleManager-sys.md#bundlemanagergetpermissiondef)替代。
424
425getPermissionDef(permissionName: string): Promise&lt;PermissionDef&gt;
426
427按权限名称获取权限的详细信息,使用promise异步回调。
428
429**需要权限:**
430
431ohos.permission.GET_BUNDLE_INFO_PRIVILEGED
432
433**系统能力:**
434
435SystemCapability.BundleManager.BundleFramework
436
437**系统接口:**
438
439此接口为系统接口。
440
441**参数:**
442
443| 参数名         | 类型   | 必填 | 说明             |
444| -------------- | ------ | ---- | ---------------- |
445| permissionName | string | 是   | 需要查询的权限的名称。 |
446
447**返回值:**
448
449| 类型                                                   | 说明                                                   |
450| ------------------------------------------------------ | ------------------------------------------------------ |
451| Promise<[PermissionDef](js-apis-bundle-PermissionDef-sys.md)> | Promise对象,获取成功时返回权限详细信息。 |
452
453**示例:**
454
455```ts
456import bundle from '@ohos.bundle';
457import { BusinessError } from '@ohos.base';
458
459let permissionName: string = "ohos.permission.GET_BUNDLE_INFO";
460bundle.getPermissionDef(permissionName).then((data) => {
461  console.info('getPermissionDef successfully. Data: ' + JSON.stringify(data));
462}).catch((error: BusinessError) => {
463  console.error('getPermissionDef failed. Cause: ' + error.message);
464});
465```