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 about the APL, see [Basic Concepts in the Permission Mechanism](../../security/AccessToken/app-permission-mgmt-overview.md#basic-concepts-in-the-permission-mechanism). 24 25## BundleChangedInfo 26 27**System capability**: SystemCapability.BundleManager.BundleFramework.Core 28 29**System API**: This is a system API. 30 31| Name | Template | Read-Only| Optional| 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**Error codes** 73 74For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 75 76| ID| Error Message | 77| -------- | --------------------------------------| 78| 201 | Permission denied. | 79| 202 | Permission denied, non-system app called system api. | 80| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.| 81 82**Example** 83 84```ts 85import bundleMonitor from '@ohos.bundle.bundleMonitor'; 86import { BusinessError } from '@ohos.base'; 87 88try { 89 bundleMonitor.on('add', (bundleChangeInfo) => { 90 console.info(`bundleName : ${bundleChangeInfo.bundleName} userId : ${bundleChangeInfo.userId}`); 91 }) 92} catch (errData) { 93 let message = (errData as BusinessError).message; 94 let errCode = (errData as BusinessError).code; 95 console.log(`errData is errCode:${errCode} message:${message}`); 96} 97``` 98 99## bundleMonitor.off 100 101off(type: BundleChangedEvent, callback?: Callback\<BundleChangedInfo>): void 102 103Unsubscribes from bundle installation, uninstall, and update events. 104 105**Required permissions**: ohos.permission.LISTEN_BUNDLE_CHANGE 106 107**System API**: This is a system API. 108 109**System capability**: SystemCapability.BundleManager.BundleFramework.Core 110 111**Parameters** 112 113| Name | Type | Mandatory| Description | 114| ---------------------------- | -------- | ---- | ---------------------------------------------------------- | 115| type| [BundleChangedEvent](js-apis-bundleMonitor-sys.md#bundlechangedevent)| Yes | Type of the event to unsubscribe from. | 116| 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.| 117 118**Error codes** 119 120For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 121 122| ID| Error Message | 123| -------- | --------------------------------------| 124| 201 | Permission denied. | 125| 202 | Permission denied, non-system app called system api. | 126| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.| 127 128**Example** 129 130```ts 131import bundleMonitor from '@ohos.bundle.bundleMonitor'; 132import { BusinessError } from '@ohos.base'; 133 134try { 135 bundleMonitor.off('add'); 136} catch (errData) { 137 let message = (errData as BusinessError).message; 138 let errCode = (errData as BusinessError).code; 139 console.log(`errData is errCode:${errCode} message:${message}`); 140} 141``` 142