• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.bundle.bundleMonitor (bundleMonitor) (System API)
2
3The **Bundle.bundleMonitor** module provides APIs for listens for bundle installation, uninstall, and updates.
4
5> **NOTE**
6>
7> The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.
8>
9> The APIs provided by this module are system APIs.
10
11## Modules to Import
12
13```ts
14import bundleMonitor from '@ohos.bundle.bundleMonitor';
15```
16
17## Required Permissions
18
19| Permission                                | APL   | Description                          |
20| ------------------------------------ | ----------- | ------------------------------ |
21| ohos.permission.LISTEN_BUNDLE_CHANGE | system_basic | Permission to listen for bundle installation, uninstall, and updates.|
22
23For details, see [Permission APL](../../security/AccessToken/app-permission-mgmt-overview.md#permission-apl).
24
25## BundleChangeInfo
26
27**System capability**: SystemCapability.BundleManager.BundleFramework.Core
28
29**System API**: This is a system API.
30
31| Name      | Template  | Readable| Writable| Description                      |
32| ---------- | ------ | ---- | ---- | -------------------------- |
33| bundleName | string | Yes  | No  | Name of the bundle whose status changes.|
34| userId     | number | Yes  | No  | ID of the user whose bundle status changes.  |
35
36## BundleChangedEvent
37
38Enumerates the types of events to listen for.
39
40**System capability**: SystemCapability.BundleManager.BundleFramework.Core
41
42**System API**: This is a system API.
43
44| Name      | Description            |
45| ---------- | --------------- |
46| add        | Bundle addition events.  |
47| update     | Bundle update events.  |
48| remove     | Bundle removal events.  |
49
50## bundleMonitor.on
51
52on(type: BundleChangedEvent, callback: Callback\<BundleChangedInfo>): void
53
54Subscribes to bundle installation, uninstall, and update events.
55>**NOTE**
56>
57>This API must be used together with [bundleMonitor.off](#bundlemonitoroff). When the lifecycle of a component, page, or application ends, use [bundleMonitor.off](#bundlemonitoroff) to unsubscribe from the bundle installation, uninstall, and update events.
58
59**Required permissions**: ohos.permission.LISTEN_BUNDLE_CHANGE
60
61**System API**: This is a system API.
62
63**System capability**: SystemCapability.BundleManager.BundleFramework.Core
64
65**Parameters**
66
67| Name                      | Type    | Mandatory| Description              |
68| ---------------------------- | -------- | ---- | ------------------ |
69| type| [BundleChangedEvent](js-apis-bundleMonitor-sys.md#bundlechangedevent)| Yes  | Type of the event to subscribe to.|
70| callback | callback\<BundleChangedInfo>| Yes  | Callback used for the subscription.|
71
72**Example**
73
74```ts
75import bundleMonitor from '@ohos.bundle.bundleMonitor';
76import { BusinessError } from '@ohos.base';
77
78try {
79    bundleMonitor.on('add', (bundleChangeInfo) => {
80        console.info(`bundleName : ${bundleChangeInfo.bundleName} userId : ${bundleChangeInfo.userId}`);
81	})
82} catch (errData) {
83    let message = (errData as BusinessError).message;
84    let errCode = (errData as BusinessError).code;
85    console.log(`errData is errCode:${errCode}  message:${message}`);
86}
87```
88
89## bundleMonitor.off
90
91off(type: BundleChangedEvent, callback?: Callback\<BundleChangedInfo>): void
92
93Unsubscribes from bundle installation, uninstall, and update events.
94
95**Required permissions**: ohos.permission.LISTEN_BUNDLE_CHANGE
96
97**System API**: This is a system API.
98
99**System capability**: SystemCapability.BundleManager.BundleFramework.Core
100
101**Parameters**
102
103| Name                      | Type    | Mandatory| Description                                                      |
104| ---------------------------- | -------- | ---- | ---------------------------------------------------------- |
105| type| [BundleChangedEvent](js-apis-bundleMonitor-sys.md#bundlechangedevent)| Yes  | Type of the event to unsubscribe from.                                        |
106| callback | callback\<BundleChangedInfo>| No  | Callback used for the unsubscription. By default, no value is passed, and all callbacks of the current event are unsubscribed from.|
107
108**Example**
109
110```ts
111import bundleMonitor from '@ohos.bundle.bundleMonitor';
112import { BusinessError } from '@ohos.base';
113
114try {
115    bundleMonitor.off('add');
116} catch (errData) {
117    let message = (errData as BusinessError).message;
118    let errCode = (errData as BusinessError).code;
119    console.log(`errData is errCode:${errCode}  message:${message}`);
120}
121```
122