1# @ohos.bundle.bundleMonitor (bundleMonitor模块)(系统接口) 2 3本模块提供监听应用安装,卸载,更新的能力。 4 5> **说明:** 6> 7> 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8> 9> 本模块为系统接口。 10 11## 导入模块 12 13```ts 14import bundleMonitor from '@ohos.bundle.bundleMonitor'; 15``` 16 17## 权限列表 18 19| 权限 | 权限等级 | 描述 | 20| ------------------------------------ | ----------- | ------------------------------ | 21| ohos.permission.LISTEN_BUNDLE_CHANGE | system_basic | 可监听应用的安装,卸载,更新。 | 22 23权限等级参考[权限APL等级说明](../../security/AccessToken/app-permission-mgmt-overview.md#权限机制中的基本概念)。 24 25## BundleChangedInfo 26 27**系统能力:** SystemCapability.BundleManager.BundleFramework.Core 28 29**系统接口:** 此接口为系统接口。 30 31| 名称 | 类型 | 只读 | 可选 | 说明 | 32| ---------- | ------ | ---- | ---- | -------------------------- | 33| bundleName | string | 是 | 否 | 应用状态发生变化的应用Bundle名称。 | 34| userId | number | 是 | 否 | 应用状态发生变化的用户id。 | 35 36## BundleChangedEvent 37 38监听的事件类型。 39 40**系统能力:** SystemCapability.BundleManager.BundleFramework.Core 41 42**系统接口:** 此接口为系统接口。 43 44| 名称 | 说明 | 45| ---------- | --------------- | 46| add | 监听应用事件。 | 47| update | 监听更新事件。 | 48| remove | 监听删除事件。 | 49 50## bundleMonitor.on 51 52on(type: BundleChangedEvent, callback: Callback\<BundleChangedInfo>): void 53 54注册监听应用的安装、卸载、更新。 55>**说明:** 56> 57>该方法需要与[bundleMonitor.off](#bundlemonitoroff)配合使用,在组件、页面、应用的生命周期结束时,使用[bundleMonitor.off](#bundlemonitoroff)注销对应用的安装、卸载、更新等事件的监听。 58 59**需要权限:** ohos.permission.LISTEN_BUNDLE_CHANGE 60 61**系统接口:** 此接口为系统接口。 62 63**系统能力:** SystemCapability.BundleManager.BundleFramework.Core 64 65**参数:** 66 67| 参数名 | 类型 | 必填 | 说明 | 68| ---------------------------- | -------- | ---- | ------------------ | 69| type| [BundleChangedEvent](js-apis-bundleMonitor-sys.md#bundlechangedevent)| 是 | 注册监听的事件类型。 | 70| callback | callback\<BundleChangedInfo>| 是 | 注册监听的回调函数。 | 71 72**错误码:** 73 74以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 75 76| 错误码ID | 错误信息 | 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**示例:** 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 103注销监听应用的安装,卸载,更新。 104 105**需要权限:** ohos.permission.LISTEN_BUNDLE_CHANGE 106 107**系统接口:** 此接口为系统接口。 108 109**系统能力:** SystemCapability.BundleManager.BundleFramework.Core 110 111**参数:** 112 113| 参数名 | 类型 | 必填 | 说明 | 114| ---------------------------- | -------- | ---- | ---------------------------------------------------------- | 115| type| [BundleChangedEvent](js-apis-bundleMonitor-sys.md#bundlechangedevent)| 是 | 注销监听的事件类型。 | 116| callback | callback\<BundleChangedInfo>| 否 | 注销监听的回调函数,默认值:注销当前事件的所有callback。 | 117 118**错误码:** 119 120以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 121 122| 错误码ID | 错误信息 | 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**示例:** 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```