1# @ohos.bundle.bundleMonitor (bundleMonitor模块)(系统接口) 2<!--Kit: Ability Kit--> 3<!--Subsystem: BundleManager--> 4<!--Owner: @wanghang904--> 5<!--Designer: @hanfeng6--> 6<!--Tester: @kongjing2--> 7<!--Adviser: @Brilliantry_Rui--> 8 9本模块提供监听应用安装,卸载,更新的能力。 10 11> **说明:** 12> 13> 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 14> 15> 本模块为系统接口。 16 17## 导入模块 18 19```ts 20import bundleMonitor from '@ohos.bundle.bundleMonitor'; 21``` 22 23## BundleChangedInfo 24 25**系统能力:** SystemCapability.BundleManager.BundleFramework.Core 26 27**系统接口:** 此接口为系统接口。 28 29| 名称 | 类型 | 只读 | 可选 | 说明 | 30| ---------- | ------ | ---- | ---- | -------------------------- | 31| bundleName | string | 是 | 否 | 应用状态发生变化的应用Bundle名称。 | 32| userId | number | 是 | 否 | 应用状态发生变化的用户ID,可以通过[getOsAccountLocalId接口](../apis-basic-services-kit/js-apis-osAccount.md#getosaccountlocalid9)获取。 | 33| appIndex<sup>12+</sup> | number | 是 | 否 | 应用状态发生变化的应用分身索引。 | 34 35## BundleChangedEvent 36 37监听的事件类型。 38 39**系统能力:** SystemCapability.BundleManager.BundleFramework.Core 40 41**系统接口:** 此接口为系统接口。 42 43| 名称 | 说明 | 44| ---------- | --------------- | 45| add | 监听应用安装事件。 | 46| update | 监听应用更新事件。 | 47| remove | 监听应用卸载事件。 | 48 49## bundleMonitor.on 50 51on(type: BundleChangedEvent, callback: Callback\<BundleChangedInfo>): void 52 53注册监听应用的安装、卸载、更新。 54>**说明:** 55> 56>该方法需要与[bundleMonitor.off](#bundlemonitoroff)配合使用,在组件、页面、应用的生命周期结束时,使用[bundleMonitor.off](#bundlemonitoroff)注销对应用的安装、卸载、更新等事件的监听。 57 58**需要权限:** ohos.permission.LISTEN_BUNDLE_CHANGE 59 60**系统接口:** 此接口为系统接口。 61 62**系统能力:** SystemCapability.BundleManager.BundleFramework.Core 63 64**参数:** 65 66| 参数名 | 类型 | 必填 | 说明 | 67| ---------------------------- | -------- | ---- | ------------------ | 68| type| [BundleChangedEvent](js-apis-bundleMonitor-sys.md#bundlechangedevent)| 是 | 注册监听的事件类型。 | 69| callback | callback\<BundleChangedInfo>| 是 | 注册监听的[回调函数](../apis-basic-services-kit/js-apis-base.md#asynccallback)。 | 70 71**错误码:** 72 73以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 74 75| 错误码ID | 错误信息 | 76| -------- | --------------------------------------| 77| 201 | Verify permission denied. | 78| 202 | Permission denied, non-system app called system api. | 79| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.| 80 81**示例:** 82 83```ts 84import bundleMonitor from '@ohos.bundle.bundleMonitor'; 85import { BusinessError } from '@ohos.base'; 86let callbackFun = (bundleChangeInfo: bundleMonitor.BundleChangedInfo) => { 87 console.info(`bundleName : ${bundleChangeInfo.bundleName} userId : ${bundleChangeInfo.userId}`); 88}; 89try { 90 bundleMonitor.on('add', callbackFun); 91} catch (errData) { 92 let message = (errData as BusinessError).message; 93 let errCode = (errData as BusinessError).code; 94 console.error(`errData is errCode:${errCode} message:${message}`); 95} 96``` 97 98## bundleMonitor.off 99 100off(type: BundleChangedEvent, callback?: Callback\<BundleChangedInfo>): void 101 102注销监听应用的安装,卸载,更新。 103 104**需要权限:** ohos.permission.LISTEN_BUNDLE_CHANGE 105 106**系统接口:** 此接口为系统接口。 107 108**系统能力:** SystemCapability.BundleManager.BundleFramework.Core 109 110**参数:** 111 112| 参数名 | 类型 | 必填 | 说明 | 113| ---------------------------- | -------- | ---- | ---------------------------------------------------------- | 114| type| [BundleChangedEvent](js-apis-bundleMonitor-sys.md#bundlechangedevent)| 是 | 注销监听的事件类型。 | 115| callback | callback\<BundleChangedInfo>| 否 | 注销监听的[回调函数](../apis-basic-services-kit/js-apis-base.md#asynccallback),参数不传时,默认值:注销当前事件的所有callback。 | 116 117**错误码:** 118 119以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。 120 121| 错误码ID | 错误信息 | 122| -------- | --------------------------------------| 123| 201 | Verify permission denied. | 124| 202 | Permission denied, non-system app called system api. | 125| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types.| 126 127**示例:** 128 129```ts 130import bundleMonitor from '@ohos.bundle.bundleMonitor'; 131import { BusinessError } from '@ohos.base'; 132 133// 该方法变量需要和bundleMonitor.on方法是同一个,才能移除对应监听的方法,否则注销监听无效 134let callbackFun = (bundleChangeInfo: bundleMonitor.BundleChangedInfo) => { 135 console.info(`bundleName : ${bundleChangeInfo.bundleName} userId : ${bundleChangeInfo.userId}`); 136}; 137 138try { 139 bundleMonitor.off('add', callbackFun); 140} catch (errData) { 141 let message = (errData as BusinessError).message; 142 let errCode = (errData as BusinessError).code; 143 console.error(`errData is errCode:${errCode} message:${message}`); 144} 145```