1# @ohos.enterprise.adminManager (企业设备管理)
2
3本模块提供企业设备管理能力,使设备具备企业场景下所需的定制能力。
4
5> **说明:**
6>
7> 本模块首批接口从API version 12开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8>
9> 本模块接口仅对[设备管理应用](../../mdm/mdm-kit-guide.md#功能介绍)开放,实现相应功能。
10
11## 导入模块
12
13```ts
14import { adminManager } from '@kit.MDMKit';
15```
16
17## adminManager.disableAdmin
18
19disableAdmin(admin: Want, userId?: number): Promise\<void>
20
21将当前/指定用户下指定的普通管理应用去激活。使用promise异步回调。
22
23**需要权限:** ohos.permission.MANAGE_ENTERPRISE_DEVICE_ADMIN
24
25**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager
26
27
28
29**模型约束**: 此接口仅可在Stage模型下使用。
30
31**参数**:
32
33| 参数名 | 类型                                                    | 必填 | 说明                                                         |
34| ------ | ------------------------------------------------------- | ---- | ------------------------------------------------------------ |
35| admin  | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是   | 普通设备管理应用。                                           |
36| userId | number                                                  | 否   | 用户ID, 取值范围:大于等于0。<br> - 调用接口时,若传入userId,表示指定用户。<br> - 调用接口时,若未传入userId,表示当前用户。 |
37
38**返回值:**
39
40| 类型           | 说明                                                         |
41| -------------- | ------------------------------------------------------------ |
42| Promise\<void> | 无返回结果的Promise对象。当去激活普通管理应用失败时,会抛出错误对象。 |
43
44**错误码**:
45
46以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。
47
48| 错误码ID | 错误信息                                                     |
49| -------- | ------------------------------------------------------------ |
50| 9200005  | Failed to deactivate the administrator application of the device. |
51| 201      | Permission verification failed. The application does not have the permission required to call the API. |
52| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
53
54**示例**:
55
56```ts
57import { Want } from '@kit.AbilityKit';
58import { BusinessError } from '@kit.BasicServicesKit';
59let wantTemp: Want = {
60  bundleName: 'bundleName',
61  abilityName: 'abilityName',
62};
63
64adminManager.disableAdmin(wantTemp, 100).catch((err: BusinessError) => {
65  console.error(`Failed to disable admin. Code: ${err.code}, message: ${err.message}`);
66});
67```
68
69## adminManager.subscribeManagedEventSync
70
71subscribeManagedEventSync(admin: Want, managedEvents: Array\<ManagedEvent>): void
72
73指定的设备管理应用订阅系统管理事件。
74
75**需要权限:** ohos.permission.ENTERPRISE_SUBSCRIBE_MANAGED_EVENT
76
77**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager
78
79
80
81**模型约束**: 此接口仅可在Stage模型下使用。
82
83**参数:**
84
85| 参数名        | 类型                                                    | 必填 | 说明           |
86| ------------- | ------------------------------------------------------- | ---- | -------------- |
87| admin         | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是   | 设备管理应用。 |
88| managedEvents | Array\<[ManagedEvent](#managedevent)>                   | 是   | 订阅事件数组。 |
89
90**错误码**:
91
92以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。
93
94| 错误码ID | 错误信息                                                     |
95| -------- | ------------------------------------------------------------ |
96| 9200001  | The application is not an administrator application of the device. |
97| 9200008  | The specified system event is invalid.                       |
98| 201      | Permission verification failed. The application does not have the permission required to call the API. |
99| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
100
101**示例:**
102
103```ts
104import { Want } from '@kit.AbilityKit';
105let wantTemp: Want = {
106  bundleName: 'bundleName',
107  abilityName: 'abilityName',
108};
109let events: Array<adminManager.ManagedEvent> = [adminManager.ManagedEvent.MANAGED_EVENT_BUNDLE_ADDED, adminManager.ManagedEvent.MANAGED_EVENT_BUNDLE_REMOVED];
110
111try {
112  adminManager.subscribeManagedEventSync(wantTemp, events);
113  console.info('Succeeded in subscribe managed event.');
114} catch (err) {
115  console.error(`Failed to subscribe managed event. Code: ${err.code}, message: ${err.message}`);
116}
117```
118
119## adminManager.unsubscribeManagedEventSync
120
121unsubscribeManagedEventSync(admin: Want, managedEvents: Array\<ManagedEvent>): void
122
123指定的设备管理应用取消订阅系统管理事件。
124
125**需要权限:** ohos.permission.ENTERPRISE_SUBSCRIBE_MANAGED_EVENT
126
127**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager
128
129
130
131**模型约束**: 此接口仅可在Stage模型下使用。
132
133**参数:**
134
135| 参数名        | 类型                                                    | 必填 | 说明               |
136| ------------- | ------------------------------------------------------- | ---- | ------------------ |
137| admin         | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是   | 设备管理应用。     |
138| managedEvents | Array\<[ManagedEvent](#managedevent)>                   | 是   | 取消订阅事件数组。 |
139
140**错误码**:
141
142以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。
143
144| 错误码ID | 错误信息                                                     |
145| -------- | ------------------------------------------------------------ |
146| 9200001  | The application is not an administrator application of the device. |
147| 9200008  | The specified system event is invalid.                       |
148| 201      | Permission verification failed. The application does not have the permission required to call the API. |
149| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
150
151**示例:**
152
153```ts
154import { Want } from '@kit.AbilityKit';
155let wantTemp: Want = {
156  bundleName: 'bundleName',
157  abilityName: 'abilityName',
158};
159let events: Array<adminManager.ManagedEvent> = [adminManager.ManagedEvent.MANAGED_EVENT_BUNDLE_ADDED, adminManager.ManagedEvent.MANAGED_EVENT_BUNDLE_REMOVED];
160
161try {
162  adminManager.unsubscribeManagedEventSync(wantTemp, events);
163  console.info('Succeeded in subscribe managed event.');
164} catch (err) {
165  console.error(`Failed to subscribe managed event. Code: ${err.code}, message: ${err.message}`);
166}
167```
168
169## ManagedEvent
170
171可订阅的系统管理事件。
172
173**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager
174
175
176
177| 名称                         | 值   | 说明           |
178| ---------------------------- | ---- | -------------- |
179| MANAGED_EVENT_BUNDLE_ADDED   | 0    | 应用安装事件。 |
180| MANAGED_EVENT_BUNDLE_REMOVED | 1    | 应用卸载事件。 |
181| MANAGED_EVENT_APP_START      | 2    | 应用启动事件。 |
182| MANAGED_EVENT_APP_STOP       | 3    | 应用停止事件。 |
183| MANAGED_EVENT_SYSTEM_UPDATE  | 4    | 系统更新事件。 |
184