• 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#permission-levels).
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>**NOTE**
54>
55>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.
56
57**Required permissions**: ohos.permission.LISTEN_BUNDLE_CHANGE
58
59**System API**: This is a system API.
60
61**System capability**: SystemCapability.BundleManager.BundleFramework.Core
62
63**Parameters**
64
65| Name                      | Type    | Mandatory| Description              |
66| ---------------------------- | -------- | ---- | ------------------ |
67| type| [BundleChangedEvent](js-apis-bundleMonitor.md#bundlechangedevent)| Yes  | Type of the event to subscribe to.|
68| callback | callback\<BundleChangedInfo>| Yes  | Callback used for the subscription.|
69
70**Example**
71
72```ts
73import bundleMonitor from '@ohos.bundle.bundleMonitor';
74import { BusinessError } from '@ohos.base';
75
76try {
77    bundleMonitor.on('add', (bundleChangeInfo) => {
78        console.info(`bundleName : ${bundleChangeInfo.bundleName} userId : ${bundleChangeInfo.userId}`);
79	})
80} catch (errData) {
81    let message = (errData as BusinessError).message;
82    let errCode = (errData as BusinessError).code;
83    console.log(`errData is errCode:${errCode}  message:${message}`);
84}
85```
86
87## bundleMonitor.off
88
89off(type: BundleChangedEvent, callback?: Callback\<BundleChangedInfo>): void
90
91Unsubscribes from bundle installation, uninstall, and update events.
92
93**Required permissions**: ohos.permission.LISTEN_BUNDLE_CHANGE
94
95**System API**: This is a system API.
96
97**System capability**: SystemCapability.BundleManager.BundleFramework.Core
98
99**Parameters**
100
101| Name                      | Type    | Mandatory| Description                                                      |
102| ---------------------------- | -------- | ---- | ---------------------------------------------------------- |
103| type| [BundleChangedEvent](js-apis-bundleMonitor.md#bundlechangedevent)| Yes  | Type of the event to unsubscribe from.                                        |
104| 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.|
105
106**Example**
107
108```ts
109import bundleMonitor from '@ohos.bundle.bundleMonitor';
110import { BusinessError } from '@ohos.base';
111
112try {
113    bundleMonitor.off('add');
114} catch (errData) {
115    let message = (errData as BusinessError).message;
116    let errCode = (errData as BusinessError).code;
117    console.log(`errData is errCode:${errCode}  message:${message}`);
118}
119```
120