• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.bundle.bundleMonitor (bundleMonitor)
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## Modules to Import
10
11```ts
12import bundleMonitor from '@ohos.bundle.bundleMonitor';
13```
14
15## Required Permissions
16
17| Permission                                | Permission Level   | Description                          |
18| ------------------------------------ | ----------- | ------------------------------ |
19| ohos.permission.LISTEN_BUNDLE_CHANGE | system_basic | Permission to listen for bundle installation, uninstall, and updates.|
20
21For details, see [Permission Levels](../../security/accesstoken-overview.md).
22
23## BundleChangeInfo
24
25**System capability**: SystemCapability.BundleManager.BundleFramework.Core
26
27**System API**: This is a system API.
28
29| Name      | Template  | Readable| Writable| Description                      |
30| ---------- | ------ | ---- | ---- | -------------------------- |
31| bundleName | string | Yes  | No  | Name of the bundle whose status changes.|
32| userId     | number | Yes  | No  | ID of the user whose bundle status changes.  |
33
34## BundleChangedEvent
35
36Enumerates the types of events to listen for.
37
38**System capability**: SystemCapability.BundleManager.BundleFramework.Core
39
40**System API**: This is a system API.
41
42| Name      | Description            |
43| ---------- | --------------- |
44| add        | Bundle addition events.  |
45| update     | Bundle update events.  |
46| remove     | Bundle removal events.  |
47
48## bundleMonitor.on
49
50on(type: BundleChangedEvent, callback: Callback\<BundleChangedInfo>): void;
51
52Subscribes to bundle installation, uninstall, and update events.
53
54**Required permissions**: ohos.permission.LISTEN_BUNDLE_CHANGE
55
56**System API**: This is a system API.
57
58**System capability**: SystemCapability.BundleManager.BundleFramework.Core
59
60**Parameters**
61
62| Name                      | Type    | Mandatory| Description              |
63| ---------------------------- | -------- | ---- | ------------------ |
64| type| [BundleChangedEvent](js-apis-bundleMonitor.md#bundlechangedevent)| Yes  | Type of the event to subscribe to.|
65| callback | callback\<BundleChangedInfo>| Yes  | Callback used for the subscription.|
66
67**Example**
68
69```ts
70import bundleMonitor from '@ohos.bundle.bundleMonitor';
71import { BusinessError } from '@ohos.base';
72
73try {
74    bundleMonitor.on('add', (bundleChangeInfo) => {
75        console.info(`bundleName : ${bundleChangeInfo.bundleName} userId : ${bundleChangeInfo.userId}`);
76	})
77} catch (errData) {
78    let message = (errData as BusinessError).message;
79    let errCode = (errData as BusinessError).code;
80    console.log(`errData is errCode:${errCode}  message:${message}`);
81}
82```
83
84## bundleMonitor.off
85
86off(type: BundleChangedEvent, callback?: Callback\<BundleChangedInfo>): void;
87
88Unsubscribes from bundle installation, uninstall, and update events.
89
90**Required permissions**: ohos.permission.LISTEN_BUNDLE_CHANGE
91
92**System API**: This is a system API.
93
94**System capability**: SystemCapability.BundleManager.BundleFramework.Core
95
96**Parameters**
97
98| Name                      | Type    | Mandatory| Description                                                      |
99| ---------------------------- | -------- | ---- | ---------------------------------------------------------- |
100| type| [BundleChangedEvent](js-apis-bundleMonitor.md#bundlechangedevent)| Yes  | Type of the event to unsubscribe from.                                        |
101| 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.|
102
103**Example**
104
105```ts
106import bundleMonitor from '@ohos.bundle.bundleMonitor';
107import { BusinessError } from '@ohos.base';
108
109try {
110    bundleMonitor.off('add');
111} catch (errData) {
112    let message = (errData as BusinessError).message;
113    let errCode = (errData as BusinessError).code;
114    console.log(`errData is errCode:${errCode}  message:${message}`);
115}
116```
117