• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.bundle.bundleMonitor (bundleMonitor模块)
2
3本模块提供监听应用安装,卸载,更新的能力。
4
5> **说明:**
6>
7> 本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8
9## 导入模块
10
11```ts
12import bundleMonitor from '@ohos.bundle.bundleMonitor';
13```
14
15## 权限列表
16
17| 权限                                 | 权限等级    | 描述                           |
18| ------------------------------------ | ----------- | ------------------------------ |
19| ohos.permission.LISTEN_BUNDLE_CHANGE | system_basic | 可监听应用的安装,卸载,更新。 |
20
21权限等级参考[权限等级说明](../../security/accesstoken-overview.md)
22
23## BundleChangeInfo
24
25**系统能力:** SystemCapability.BundleManager.BundleFramework.Core
26
27**系统API:**  此接口为系统接口。
28
29| 名称       | 类型   | 可读 | 可写 | 说明                       |
30| ---------- | ------ | ---- | ---- | -------------------------- |
31| bundleName | string | 是   | 否   | 应用状态发生变化的应用Bundle名称。 |
32| userId     | number | 是   | 否   | 应用状态发生变化的用户id。   |
33
34## BundleChangedEvent
35
36监听的事件类型。
37
38**系统能力:** SystemCapability.BundleManager.BundleFramework.Core
39
40**系统API:**  此接口为系统接口。
41
42| 名称       | 说明             |
43| ---------- | --------------- |
44| add        | 监听应用事件。   |
45| update     | 监听更新事件。   |
46| remove     | 监听删除事件。   |
47
48## bundleMonitor.on
49
50on(type: BundleChangedEvent, callback: Callback\<BundleChangedInfo>): void;
51
52注册监听应用的安装,卸载,更新。
53
54**需要权限:** ohos.permission.LISTEN_BUNDLE_CHANGE
55
56**系统API:**  此接口为系统接口。
57
58**系统能力:** SystemCapability.BundleManager.BundleFramework.Core
59
60**参数:**
61
62| 参数名                       | 类型     | 必填 | 说明               |
63| ---------------------------- | -------- | ---- | ------------------ |
64| type| [BundleChangedEvent](js-apis-bundleMonitor.md#bundlechangedevent)| 是   | 注册监听的事件类型。 |
65| callback | callback\<BundleChangedInfo>| 是   | 注册监听的回调函数。 |
66
67**示例:**
68
69```ts
70import bundleMonitor from '@ohos.bundle.bundleMonitor';
71import { BusinessError } from '@ohos.base';
72
73try {
74    bundleMonitor.on('add', (bundleChangeInfo) => {
75        console.info(`bundleName : ${bundleChangeInfo.bundleName} userId : ${bundleChangeInfo.userId}`);
76	})
77} catch (errData) {
78    let message = (errData as BusinessError).message;
79    let errCode = (errData as BusinessError).code;
80    console.log(`errData is errCode:${errCode}  message:${message}`);
81}
82```
83
84## bundleMonitor.off
85
86off(type: BundleChangedEvent, callback?: Callback\<BundleChangedInfo>): void;
87
88注销监听应用的安装,卸载,更新。
89
90**需要权限:** ohos.permission.LISTEN_BUNDLE_CHANGE
91
92**系统API:**  此接口为系统接口。
93
94**系统能力:** SystemCapability.BundleManager.BundleFramework.Core
95
96**参数:**
97
98| 参数名                       | 类型     | 必填 | 说明                                                       |
99| ---------------------------- | -------- | ---- | ---------------------------------------------------------- |
100| type| [BundleChangedEvent](js-apis-bundleMonitor.md#bundlechangedevent)| 是   | 注销监听的事件类型。                                         |
101| callback | callback\<BundleChangedInfo>| 否   | 注销监听的回调函数,默认值:注销当前事件的所有callback。 |
102
103**示例:**
104
105```ts
106import bundleMonitor from '@ohos.bundle.bundleMonitor';
107import { BusinessError } from '@ohos.base';
108
109try {
110    bundleMonitor.off('add');
111} catch (errData) {
112    let message = (errData as BusinessError).message;
113    let errCode = (errData as BusinessError).code;
114    console.log(`errData is errCode:${errCode}  message:${message}`);
115}
116```