> 技术文档 > 【鸿蒙next教程】企业设备管理:@ohos.enterprise.adminManager (企业设备管理)

【鸿蒙next教程】企业设备管理:@ohos.enterprise.adminManager (企业设备管理)


 往期鸿蒙5.0全套实战文章必看:(文中附带鸿蒙5.0全栈学习资料)

  • 鸿蒙开发核心知识点,看这篇文章就够了

  • 最新版!鸿蒙HarmonyOS Next应用开发实战学习路线

  • 鸿蒙HarmonyOS NEXT开发技术最全学习路线指南

  • 鸿蒙应用开发实战项目,看这一篇文章就够了(部分项目附源码)


@ohos.enterprise.adminManager (企业设备管理)

本模块提供企业设备管理能力,使设备具备企业场景下所需的定制能力。

说明

本模块首批接口从API version 12开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

本模块接口仅对设备管理应用开放,实现相应功能。

导入模块

import { adminManager } from \'@kit.MDMKit\';

adminManager.disableAdmin

disableAdmin(admin: Want, userId?: number): Promise

将当前设备指定用户的设备管理应用去激活。使用promise异步回调。

需要权限: ohos.permission.MANAGE_ENTERPRISE_DEVICE_ADMIN

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

模型约束: 此接口仅可在Stage模型下使用。

参数

参数名 类型 必填 说明 admin Want 是 企业设备管理扩展组件。 userId number 否

用户ID, 取值范围:大于等于0。

- 调用接口时,若传入userId,表示指定用户。

- 调用接口时,若未传入userId,表示当前用户。

返回值:

类型 说明 Promise 无返回结果的Promise对象。当去激活设备管理应用失败时,会抛出错误对象。

错误码:

以下错误码的详细介绍。

错误码ID 错误信息 9200005 Failed to deactivate the administrator application of the device. 201 Permission verification failed. The application does not have the permission required to call the API. 401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.

示例

import { Want } from \'@kit.AbilityKit\';import { BusinessError } from \'@kit.BasicServicesKit\';let wantTemp: Want = { bundleName: \'com.example.myapplication\', abilityName: \'EntryAbility\',};adminManager.disableAdmin(wantTemp, 100).catch((err: BusinessError) => { console.error(`Failed to disable admin. Code: ${err.code}, message: ${err.message}`);});

adminManager.subscribeManagedEventSync

subscribeManagedEventSync(admin: Want, managedEvents: Array): void

订阅系统管理事件

需要权限: ohos.permission.ENTERPRISE_SUBSCRIBE_MANAGED_EVENT

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

模型约束: 此接口仅可在Stage模型下使用。

参数:

参数名 类型 必填 说明 admin Want 是 企业设备管理扩展组件。 managedEvents Array 是 订阅事件数组。

错误码

以下错误码的详细介绍。

错误码ID 错误信息 9200001 The application is not an administrator application of the device. 9200008 The specified system event is invalid. 201 Permission verification failed. The application does not have the permission required to call the API. 401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.

示例:

import { Want } from \'@kit.AbilityKit\';let wantTemp: Want = { bundleName: \'com.example.myapplication\', abilityName: \'EntryAbility\',};let events: Array = [adminManager.ManagedEvent.MANAGED_EVENT_BUNDLE_ADDED, adminManager.ManagedEvent.MANAGED_EVENT_BUNDLE_REMOVED];try { adminManager.subscribeManagedEventSync(wantTemp, events); console.info(\'Succeeded in subscribing managed event.\');} catch (err) { console.error(`Failed to subscribe managed event. Code: ${err.code}, message: ${err.message}`);}

adminManager.unsubscribeManagedEventSync

unsubscribeManagedEventSync(admin: Want, managedEvents: Array): void

取消订阅系统管理事件。

需要权限: ohos.permission.ENTERPRISE_SUBSCRIBE_MANAGED_EVENT

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

模型约束: 此接口仅可在Stage模型下使用。

参数:

参数名 类型 必填 说明 admin Want 是 企业设备管理扩展组件。 managedEvents Array 是 取消订阅事件数组。

错误码

以下错误码的详细介绍。

错误码ID 错误信息 9200001 The application is not an administrator application of the device. 9200008 The specified system event is invalid. 201 Permission verification failed. The application does not have the permission required to call the API. 401 Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed.

示例:

import { Want } from \'@kit.AbilityKit\';let wantTemp: Want = { bundleName: \'com.example.myapplication\', abilityName: \'EntryAbility\',};let events: Array = [adminManager.ManagedEvent.MANAGED_EVENT_BUNDLE_ADDED, adminManager.ManagedEvent.MANAGED_EVENT_BUNDLE_REMOVED];try { adminManager.unsubscribeManagedEventSync(wantTemp, events); console.info(\'Succeeded in unsubscribing managed event.\');} catch (err) { console.error(`Failed to unsubscribe managed event. Code: ${err.code}, message: ${err.message}`);}

ManagedEvent

可订阅的系统管理事件。

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

名称 值 说明 MANAGED_EVENT_BUNDLE_ADDED 0 应用安装事件。 MANAGED_EVENT_BUNDLE_REMOVED 1 应用卸载事件。 MANAGED_EVENT_APP_START 2 应用启动事件。 MANAGED_EVENT_APP_STOP 3 应用停止事件。 MANAGED_EVENT_SYSTEM_UPDATE 4 系统更新事件。