openharmony 鸿蒙 js-apis-enterprise-adminManager

2025-06-16 浏览 (1)

@ohos.enterprise.adminManager (admin权限管理)

本模块为企业MDM应用提供admin权限管理能力,包括激活/解除激活admin权限、事件订阅、委托授权等。

说明:

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

本模块接口仅对设备管理应用开放,具体请参考MDM Kit开发指南

导入模块

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

adminManager.disableAdmin

disableAdmin(admin: Want, userId?: number): Promise<void>

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

需要权限: ohos.permission.MANAGE_ENTERPRISE_DEVICE_ADMIN

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

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

参数

参数名类型必填说明
adminWant企业设备管理扩展组件。
userIdnumber用户ID,取值范围:大于等于0。
- 调用接口时,若传入userId,表示指定用户。
- 调用接口时,若未传入userId,表示当前用户。

返回值:

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

错误码:

以下错误码的详细介绍请参见企业设备管理错误码通用错误码

错误码ID错误信息
9200005Failed to deactivate the administrator application of the device.
201Permission verification failed. The application does not have the permission required to call the API.
401Parameter 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<ManagedEvent>): void

订阅系统管理事件。

需要权限: ohos.permission.ENTERPRISE_SUBSCRIBE_MANAGED_EVENT

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

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

参数:

参数名类型必填说明
adminWant企业设备管理扩展组件。
managedEventsArray<ManagedEvent>订阅事件数组。

错误码

以下错误码的详细介绍请参见企业设备管理错误码通用错误码

错误码ID错误信息
9200001The application is not an administrator application of the device.
9200008The specified system event is invalid.
201Permission verification failed. The application does not have the permission required to call the API.
401Parameter 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> = [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<ManagedEvent>): void

取消订阅系统管理事件。

需要权限: ohos.permission.ENTERPRISE_SUBSCRIBE_MANAGED_EVENT

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

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

参数:

参数名类型必填说明
adminWant企业设备管理扩展组件。
managedEventsArray<ManagedEvent>取消订阅事件数组。

错误码

以下错误码的详细介绍请参见企业设备管理错误码通用错误码

错误码ID错误信息
9200001The application is not an administrator application of the device.
9200008The specified system event is invalid.
201Permission verification failed. The application does not have the permission required to call the API.
401Parameter 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> = [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}`);
}

adminManager.setDelegatedPolicies14+

setDelegatedPolicies(admin: Want, bundleName: string, policies: Array<string>): void

委托其他应用来设置设备的管控策略。被委托的其他应用需申请委托策略对应接口所需权限。

需要权限: ohos.permission.ENTERPRISE_MANAGE_DELEGATED_POLICY

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

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

参数:

参数名类型必填说明
adminWant企业设备管理扩展组件。
bundleNamestring被委托应用包名。被委托应用仅支持分发类型为enterprise_normal(企业普通应用)和enterprise_mdm(企业MDM应用)两种类型,开发者可在Profile文件中查询分发类型字段app-distribution-type。
policiesArray<string>委托策略列表

错误码

以下错误码的详细介绍请参见企业设备管理错误码通用错误码

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

示例:

import { Want } from '@kit.AbilityKit';

let admin: Want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility',
};
let policies: Array<string> = ["disabled_hdc"];

try {
  adminManager.setDelegatedPolicies(admin, "com.example.enterprise.xxx", policies);
  console.info('Succeeded in setting delegated policies.');
} catch (err) {
  console.error(`Failed to set delegated policies. Code: ${err.code}, message: ${err.message}`);
}

adminManager.getDelegatedPolicies14+

getDelegatedPolicies(admin: Want, bundleName: string): Array<string>

查询被委托应用可访问的策略列表。

需要权限: ohos.permission.ENTERPRISE_MANAGE_DELEGATED_POLICY

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

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

参数:

参数名类型必填说明
adminWant企业设备管理扩展组件。
bundleNamestring被委托应用包名。被委托应用仅支持分发类型为enterprise_normal(企业普通应用)和enterprise_mdm(企业MDM应用)两种类型,开发者可在Profile文件中查询分发类型字段app-distribution-type。

返回值:

类型说明
Array<string>委托策略列表。

错误码

以下错误码的详细介绍请参见企业设备管理错误码通用错误码

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

示例:

import { Want } from '@kit.AbilityKit';

let admin: Want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility',
};

try {
  let policies: Array<string> = adminManager.getDelegatedPolicies(admin, "com.example.enterprise.xxx");
  console.info(`Succeeded in getting delegated policies.${JSON.stringify(policies)}`);
} catch (err) {
  console.error(`Failed to get delegated policies. Code: ${err.code}, message: ${err.message}`);
}

adminManager.getDelegatedBundleNames14+

getDelegatedBundleNames(admin: Want, policy: string): Array<string>

查询可以访问某个委托策略的被委托应用,输出被委托应用列表。

需要权限: ohos.permission.ENTERPRISE_MANAGE_DELEGATED_POLICY

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

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

参数:

参数名类型必填说明
adminWant企业设备管理扩展组件。
policystring委托策略。

返回值:

类型说明
Array<string>被委托应用列表。

错误码

以下错误码的详细介绍请参见企业设备管理错误码通用错误码

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

示例:

import { Want } from '@kit.AbilityKit';

let admin: Want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility',
};

try {
  let bundleNames: Array<string> = adminManager.getDelegatedBundleNames(admin, "disabled_hdc");
  console.info(`Succeeded in getting delegated bundles.${JSON.stringify(bundleNames)}`);
} catch (err) {
  console.error(`Failed to get delegated bundles. Code: ${err.code}, message: ${err.message}`);
}

adminManager.startAdminProvision15+

startAdminProvision(admin: Want, type: AdminType, context: common.Context, parameters: Record<string, string>): void

设备管理应用拉起BYOD管理员激活页面进行激活。

需要权限: ohos.permission.START_PROVISIONING_MESSAGE

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

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

参数:

参数名类型必填说明
adminWant企业设备管理扩展组件。
typeAdminType激活的设备管理应用类型,仅支持ADMIN_TYPE_BYOD类型。
contextcommon.Context管理应用的上下文信息。
parametersRecord<string, string>自定义参数信息,其中Key值必须包含:"activateId"。

错误码

以下的错误码的详细介绍请参见通用错误码

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

示例:

import { common, Want } from '@kit.AbilityKit';

let wantTemp: Want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility',
};
let recordParameters: Record<string, string> = {
  "activateId": "activateId testValue",
  "customizedInfo": "customizedInfo testValue"
};
const context = this.getUIContext().getHostContext() as common.UIAbilityContext;
try {
  console.info('context:' + JSON.stringify(context));
  adminManager.startAdminProvision(wantTemp, adminManager.AdminType.ADMIN_TYPE_BYOD, context, recordParameters);
  console.info('startAdminProvision::success');
} catch (error) {
  console.error('startAdminProvision::errorCode: ' + error.code + ' errorMessage: ' + error.message);
}

ManagedEvent

可订阅的系统管理事件。

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

名称说明
MANAGED_EVENT_BUNDLE_ADDED0应用安装事件。
MANAGED_EVENT_BUNDLE_REMOVED1应用卸载事件。
MANAGED_EVENT_APP_START2应用启动事件。
MANAGED_EVENT_APP_STOP3应用停止事件。
MANAGED_EVENT_SYSTEM_UPDATE4系统更新事件。
MANAGED_EVENT_ACCOUNT_ADDED18+5账号新增事件。
MANAGED_EVENT_ACCOUNT_SWITCHED18+6账号切换事件。
MANAGED_EVENT_ACCOUNT_REMOVED18+7账号删除事件。

AdminType15+

设备管理应用的类型。

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

名称说明
ADMIN_TYPE_BYOD0x02BYOD设备管理应用。

附录

可委托策略列表

策略名称对应接口说明
disallow_add_local_accountaccountManager.disallowOsAccountAddition
accountManager.isOsAccountAdditionDisallowed
不传accountId参数,禁止设备创建本地用户。
不传accountId参数,查询是否禁止设备创建本地用户。
disallow_add_os_account_by_useraccountManager.disallowOsAccountAddition
accountManager.isOsAccountAdditionDisallowed
需传入accountId参数,禁止指定用户添加账号。
需传入accountId参数,查询是否禁止指定用户添加账号。
disallow_running_bundlesapplicationManager.addDisallowedRunningBundlesSync
applicationManager.removeDisallowedRunningBundlesSync
applicationManager.getDisallowedRunningBundlesSync
添加应用至应用运行黑名单,添加至黑名单的应用不允许在当前/指定用户下运行。
从应用运行黑名单中移除应用。
获取当前/指定用户下的应用运行黑名单。
manage_auto_start_appsapplicationManager.addAutoStartApps
applicationManager.removeAutoStartApps
applicationManager.getAutoStartApps
添加开机自启动应用名单。该能力当前仅支持2in1设备。
从开机自启动应用名单中移除应用。该能力当前仅支持2in1设备。
查询开机自启动应用名单。该能力当前仅支持2in1设备。
allowed_bluetooth_devicesbluetoothManager.addAllowedBluetoothDevices
bluetoothManager.removeAllowedBluetoothDevices
bluetoothManager.getAllowedBluetoothDevices
添加蓝牙设备可用白名单。
从蓝牙设备可用白名单中移除。
查询蓝牙设备可用白名单。
set_browser_policiesbrowser.setPolicySync
browser.getPoliciesSync
为指定的浏览器设置浏览器子策略。
获取指定浏览器的策略。
allowed_install_bundlesbundleManager.addAllowedInstallBundlesSync
bundleManager.removeAllowedInstallBundlesSync
bundleManager.getAllowedInstallBundlesSync
添加应用至应用程序包安装白名单,添加至白名单的应用允许在当前/指定用户下安装,否则不允许安装。
从应用程序包安装白名单中移除应用。
获取当前/指定用户下的应用程序包安装白名单。
disallowed_install_bundlesbundleManager.addDisallowedInstallBundlesSync
bundleManager.removeDisallowedInstallBundlesSync
bundleManager.getDisallowedInstallBundlesSync
添加应用至应用程序包安装黑名单,添加至黑名单的应用不允许在当前/指定用户下安装。
从应用程序包安装黑名单中移除应用。
获取当前/指定用户下的应用程序包安装黑名单。
disallowed_uninstall_bundlesbundleManager.addDisallowedUninstallBundlesSync
bundleManager.removeDisallowedUninstallBundlesSync
bundleManager.getDisallowedUninstallBundlesSync
添加应用至应用程序包卸载黑名单,添加至黑名单的应用不允许在当前/指定用户下卸载。
从应用程序包卸载黑名单中移除应用。
获取当前/指定用户下的应用包程序卸载黑名单。
get_device_infodeviceInfo.getDeviceInfo获取设备信息。
location_policylocationManager.setLocationPolicy
locationManager.getLocationPolicy
设置位置服务管理策略。
查询位置服务策略。
disabled_network_interfacenetworkManager.setNetworkInterfaceDisabledSync
networkManager.isNetworkInterfaceDisabledSync
禁止设备使用指定网络。
查询指定网络接口是否被禁用。
global_proxynetworkManager.setGlobalProxySync
networkManager.getGlobalProxySync
设置网络全局代理。
获取网络全局代理。
disabled_bluetoothrestrictions.setDisallowedPolicy
restrictions.getDisallowedPolicy
feature传入bluetooth,禁用/启用蓝牙能力。
feature传入bluetooth,查询是否禁用蓝牙能力。
disallow_modify_datetimerestrictions.setDisallowedPolicy
restrictions.getDisallowedPolicy
feature传入modifyDateTime,禁用/启用设置系统时间能力。
feature传入modifyDateTime,查询是否禁用修改系统时间能力。
disabled_printerrestrictions.setDisallowedPolicy
restrictions.getDisallowedPolicy
feature传入printer,禁用/启用打印能力。
feature传入printer,查询是否禁用打印能力。
disabled_hdcrestrictions.setDisallowedPolicy
restrictions.getDisallowedPolicy
feature传入hdc,禁用/启用HDC能力。
feature传入hdc,查询是否禁用HDC能力。
disable_microphonerestrictions.setDisallowedPolicy
restrictions.getDisallowedPolicy
feature传入microphone,禁用/启用麦克风能力。
feature传入microphone,查询是否禁用麦克风能力。
fingerprint_authrestrictions.setDisallowedPolicy
restrictions.getDisallowedPolicy
restrictions.setDisallowedPolicyForAccount
restrictions.getDisallowedPolicyForAccount
feature传入fingerprint,禁用/启用指纹认证能力。
feature传入fingerprint,查询是否禁用指纹认证能力。
feature传入fingerprint,禁用/启用指定用户的指纹认证能力。
feature传入fingerprint,查询是否禁用指定用户的指纹认证能力。
disable_usbrestrictions.setDisallowedPolicy
restrictions.getDisallowedPolicy
feature传入usb,禁用/启用USB能力。
feature传入usb,查询是否禁用USB能力。
disable_wifirestrictions.setDisallowedPolicy
restrictions.getDisallowedPolicy
feature传入wifi,禁用/启用WIFI能力。
feature传入wifi,查询是否禁用WIFI能力。
disallowed_tetheringrestrictions.setDisallowedPolicy
restrictions.getDisallowedPolicy
feature传入tethering,禁用/启用网络共享能力。
feature传入tethering,查询是否禁用网络共享能力。
inactive_user_freezerestrictions.setDisallowedPolicy
restrictions.getDisallowedPolicy
feature传入inactiveUserFreeze,禁用/启用非活跃用户运行能力。
feature传入inactiveUserFreeze,查询是否禁用非活跃用户运行能力。
snapshot_skiprestrictions.addDisallowedListForAccount
restrictions.removeDisallowedListForAccount
restrictions.getDisallowedListForAccount
feature传入snapshotSkip,禁用屏幕快照能力的应用名单。
feature传入snapshotSkip,从禁用屏幕快照能力的应用名单中移除。
feature传入snapshotSkip,查询禁用屏幕快照能力的应用名单。
password_policysecurityManager.setPasswordPolicy
securityManager.getPasswordPolicy
设置设备口令策略。
获取设备口令策略。
clipboard_policysecurityManager.setAppClipboardPolicy
securityManager.getAppClipboardPolicy
设置设备剪贴板策略。
获取设备剪贴板策略。
watermark_image_policysecurityManager.setWatermarkImage
securityManager.cancelWatermarkImage
设置水印策略,当前仅支持2in1使用。
取消水印策略,当前仅支持2in1使用。
ntp_serversystemManager.setNTPServer
systemManager.getNTPServer
设置NTP服务器的策略。
获取NTP服务器信息。
set_update_policysystemManager.setOtaUpdatePolicy
systemManager.getOtaUpdatePolicy
设置升级策略。
查询升级策略。
notify_upgrade_packagessystemManager.notifyUpdatePackages
systemManager.getUpdateResult
通知系统更新包信息。
获取系统更新结果。
allowed_usb_devicesusbManager.addAllowedUsbDevices
usbManager.removeAllowedUsbDevices
usbManager.getAllowedUsbDevices
添加USB设备可用白名单。
移除USB设备可用白名单。
获取USB设备可用白名单。
usb_read_onlyusbManager.setUsbStorageDeviceAccessPolicy
usbManager.getUsbStorageDeviceAccessPolicy
设置USB存储设备访问策略。
获取USB存储设备访问策略。
disallowed_usb_devicesusbManager.addDisallowedUsbDevices
usbManager.removeDisallowedUsbDevices
usbManager.getDisallowedUsbDevices
添加禁止使用的USB设备类型。
移除禁止使用的USB设备类型。
获取禁止使用的USB设备类型。

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Mdm Kit(企业设备管理服务)

harmony 鸿蒙企业设备管理错误码

harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility(企业设备管理扩展能力)

harmony 鸿蒙@ohos.enterprise.accountManager(账户管理)(系统接口)

harmony 鸿蒙@ohos.enterprise.accountManager(账户管理)

harmony 鸿蒙@ohos.enterprise.adminManager(企业设备管理)(系统接口)

harmony 鸿蒙@ohos.enterprise.applicationManager(应用管理)(系统接口)

harmony 鸿蒙@ohos.enterprise.applicationManager(应用管理)

harmony 鸿蒙@ohos.enterprise.bluetoothManager(蓝牙管理)(系统接口)

harmony 鸿蒙@ohos.enterprise.bluetoothManager(蓝牙管理)

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/1GNEuN