1e41f4b71Sopenharmony_ci# @ohos.bundle.bundleMonitor (bundleMonitor) (System API) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciThe **Bundle.bundleMonitor** module provides APIs for listens for bundle installation, uninstall, and updates. 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci> **NOTE** 6e41f4b71Sopenharmony_ci> 7e41f4b71Sopenharmony_ci> 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. 8e41f4b71Sopenharmony_ci> 9e41f4b71Sopenharmony_ci> The APIs provided by this module are system APIs. 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci## Modules to Import 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci```ts 14e41f4b71Sopenharmony_ciimport bundleMonitor from '@ohos.bundle.bundleMonitor'; 15e41f4b71Sopenharmony_ci``` 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci## Required Permissions 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci| Permission | APL | Description | 20e41f4b71Sopenharmony_ci| ------------------------------------ | ----------- | ------------------------------ | 21e41f4b71Sopenharmony_ci| ohos.permission.LISTEN_BUNDLE_CHANGE | system_basic | Permission to listen for bundle installation, uninstall, and updates.| 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ciFor details about the APL, see [Basic Concepts in the Permission Mechanism](../../security/AccessToken/app-permission-mgmt-overview.md#basic-concepts-in-the-permission-mechanism). 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci## BundleChangedInfo 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ci**System capability**: SystemCapability.BundleManager.BundleFramework.Core 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ci**System API**: This is a system API. 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci| Name | Template | Read-Only| Optional| Description | 32e41f4b71Sopenharmony_ci| ---------- | ------ | ---- | ---- | -------------------------- | 33e41f4b71Sopenharmony_ci| bundleName | string | Yes | No | Name of the bundle whose status changes.| 34e41f4b71Sopenharmony_ci| userId | number | Yes | No | ID of the user whose bundle status changes. | 35e41f4b71Sopenharmony_ci 36e41f4b71Sopenharmony_ci## BundleChangedEvent 37e41f4b71Sopenharmony_ci 38e41f4b71Sopenharmony_ciEnumerates the types of events to listen for. 39e41f4b71Sopenharmony_ci 40e41f4b71Sopenharmony_ci**System capability**: SystemCapability.BundleManager.BundleFramework.Core 41e41f4b71Sopenharmony_ci 42e41f4b71Sopenharmony_ci**System API**: This is a system API. 43e41f4b71Sopenharmony_ci 44e41f4b71Sopenharmony_ci| Name | Description | 45e41f4b71Sopenharmony_ci| ---------- | --------------- | 46e41f4b71Sopenharmony_ci| add | Bundle addition events. | 47e41f4b71Sopenharmony_ci| update | Bundle update events. | 48e41f4b71Sopenharmony_ci| remove | Bundle removal events. | 49e41f4b71Sopenharmony_ci 50e41f4b71Sopenharmony_ci## bundleMonitor.on 51e41f4b71Sopenharmony_ci 52e41f4b71Sopenharmony_cion(type: BundleChangedEvent, callback: Callback\<BundleChangedInfo>): void 53e41f4b71Sopenharmony_ci 54e41f4b71Sopenharmony_ciSubscribes to bundle installation, uninstall, and update events. 55e41f4b71Sopenharmony_ci>**NOTE** 56e41f4b71Sopenharmony_ci> 57e41f4b71Sopenharmony_ci>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. 58e41f4b71Sopenharmony_ci 59e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.LISTEN_BUNDLE_CHANGE 60e41f4b71Sopenharmony_ci 61e41f4b71Sopenharmony_ci**System API**: This is a system API. 62e41f4b71Sopenharmony_ci 63e41f4b71Sopenharmony_ci**System capability**: SystemCapability.BundleManager.BundleFramework.Core 64e41f4b71Sopenharmony_ci 65e41f4b71Sopenharmony_ci**Parameters** 66e41f4b71Sopenharmony_ci 67e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 68e41f4b71Sopenharmony_ci| ---------------------------- | -------- | ---- | ------------------ | 69e41f4b71Sopenharmony_ci| type| [BundleChangedEvent](js-apis-bundleMonitor-sys.md#bundlechangedevent)| Yes | Type of the event to subscribe to.| 70e41f4b71Sopenharmony_ci| callback | callback\<BundleChangedInfo>| Yes | Callback used for the subscription.| 71e41f4b71Sopenharmony_ci 72e41f4b71Sopenharmony_ci**Error codes** 73e41f4b71Sopenharmony_ci 74e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 75e41f4b71Sopenharmony_ci 76e41f4b71Sopenharmony_ci| ID| Error Message | 77e41f4b71Sopenharmony_ci| -------- | --------------------------------------| 78e41f4b71Sopenharmony_ci| 201 | Permission denied. | 79e41f4b71Sopenharmony_ci| 202 | Permission denied, non-system app called system api. | 80e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.| 81e41f4b71Sopenharmony_ci 82e41f4b71Sopenharmony_ci**Example** 83e41f4b71Sopenharmony_ci 84e41f4b71Sopenharmony_ci```ts 85e41f4b71Sopenharmony_ciimport bundleMonitor from '@ohos.bundle.bundleMonitor'; 86e41f4b71Sopenharmony_ciimport { BusinessError } from '@ohos.base'; 87e41f4b71Sopenharmony_ci 88e41f4b71Sopenharmony_citry { 89e41f4b71Sopenharmony_ci bundleMonitor.on('add', (bundleChangeInfo) => { 90e41f4b71Sopenharmony_ci console.info(`bundleName : ${bundleChangeInfo.bundleName} userId : ${bundleChangeInfo.userId}`); 91e41f4b71Sopenharmony_ci }) 92e41f4b71Sopenharmony_ci} catch (errData) { 93e41f4b71Sopenharmony_ci let message = (errData as BusinessError).message; 94e41f4b71Sopenharmony_ci let errCode = (errData as BusinessError).code; 95e41f4b71Sopenharmony_ci console.log(`errData is errCode:${errCode} message:${message}`); 96e41f4b71Sopenharmony_ci} 97e41f4b71Sopenharmony_ci``` 98e41f4b71Sopenharmony_ci 99e41f4b71Sopenharmony_ci## bundleMonitor.off 100e41f4b71Sopenharmony_ci 101e41f4b71Sopenharmony_cioff(type: BundleChangedEvent, callback?: Callback\<BundleChangedInfo>): void 102e41f4b71Sopenharmony_ci 103e41f4b71Sopenharmony_ciUnsubscribes from bundle installation, uninstall, and update events. 104e41f4b71Sopenharmony_ci 105e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.LISTEN_BUNDLE_CHANGE 106e41f4b71Sopenharmony_ci 107e41f4b71Sopenharmony_ci**System API**: This is a system API. 108e41f4b71Sopenharmony_ci 109e41f4b71Sopenharmony_ci**System capability**: SystemCapability.BundleManager.BundleFramework.Core 110e41f4b71Sopenharmony_ci 111e41f4b71Sopenharmony_ci**Parameters** 112e41f4b71Sopenharmony_ci 113e41f4b71Sopenharmony_ci| Name | Type | Mandatory| Description | 114e41f4b71Sopenharmony_ci| ---------------------------- | -------- | ---- | ---------------------------------------------------------- | 115e41f4b71Sopenharmony_ci| type| [BundleChangedEvent](js-apis-bundleMonitor-sys.md#bundlechangedevent)| Yes | Type of the event to unsubscribe from. | 116e41f4b71Sopenharmony_ci| 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.| 117e41f4b71Sopenharmony_ci 118e41f4b71Sopenharmony_ci**Error codes** 119e41f4b71Sopenharmony_ci 120e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 121e41f4b71Sopenharmony_ci 122e41f4b71Sopenharmony_ci| ID| Error Message | 123e41f4b71Sopenharmony_ci| -------- | --------------------------------------| 124e41f4b71Sopenharmony_ci| 201 | Permission denied. | 125e41f4b71Sopenharmony_ci| 202 | Permission denied, non-system app called system api. | 126e41f4b71Sopenharmony_ci| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.| 127e41f4b71Sopenharmony_ci 128e41f4b71Sopenharmony_ci**Example** 129e41f4b71Sopenharmony_ci 130e41f4b71Sopenharmony_ci```ts 131e41f4b71Sopenharmony_ciimport bundleMonitor from '@ohos.bundle.bundleMonitor'; 132e41f4b71Sopenharmony_ciimport { BusinessError } from '@ohos.base'; 133e41f4b71Sopenharmony_ci 134e41f4b71Sopenharmony_citry { 135e41f4b71Sopenharmony_ci bundleMonitor.off('add'); 136e41f4b71Sopenharmony_ci} catch (errData) { 137e41f4b71Sopenharmony_ci let message = (errData as BusinessError).message; 138e41f4b71Sopenharmony_ci let errCode = (errData as BusinessError).code; 139e41f4b71Sopenharmony_ci console.log(`errData is errCode:${errCode} message:${message}`); 140e41f4b71Sopenharmony_ci} 141e41f4b71Sopenharmony_ci``` 142