harmony 鸿蒙@ohos.enterprise.deviceSettings (设备设置管理)

2023-10-30 浏览 (575)

@ohos.enterprise.deviceSettings (设备设置管理)

本模块提供企业设备设置能力,包括获取设备息屏时间等。

说明:

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

本模块接口仅可在Stage模型下使用。

本模块接口仅对设备管理应用开放,需将设备管理应用激活后调用,实现相应功能。

导入模块

import deviceSettings from '@ohos.enterprise.deviceSettings';

deviceSettings.setScreenOffTime11+

setScreenOffTime(admin: Want, time: number): void

以同步方法指定设备管理应用设置设备息屏时间。成功返回null,失败抛出对应异常。

需要权限: ohos.permission.ENTERPRISE_SET_SCREENOFF_TIME

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

系统API: 此接口为系统接口。

参数:

参数名类型必填说明
adminWant设备管理应用。
timenumber设备息屏时间(单位:毫秒,建议参数与设备可选息屏时间保持一致)

错误码

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

错误码ID错误信息
9200001the application is not an administrator of the device.
9200002the administrator application does not have permission to manage the device.

示例:

import Want from '@ohos.app.ability.Want';
let wantTemp: Want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility',
};
try {
  deviceSettings.setScreenOffTime(wantTemp, 30000);
  console.info(`Succeeded in setting screen off time`);
} catch(err) {
  console.error(`Failed to set screen off time. Code: ${err.code}, message: ${err.message}`);
}

deviceSettings.getScreenOffTime

getScreenOffTime(admin: Want, callback: AsyncCallback<number>): void

指定设备管理应用获取设备息屏时间,使用callback异步回调。

需要权限: ohos.permission.ENTERPRISE_GET_SETTINGS

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

系统API: 此接口为系统接口。

参数:

参数名类型必填说明
adminWant设备管理应用。
callbackAsyncCallback<number>回调函数。当接口调用成功,err为null,data为设备息屏时间,否则err为错误对象。

错误码

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

错误码ID错误信息
9200001the application is not an administrator of the device.
9200002the administrator application does not have permission to manage the device.

示例:

import Want from '@ohos.app.ability.Want';
let wantTemp: Want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility',
};

deviceSettings.getScreenOffTime(wantTemp, (err, result) => {
  if (err) {
    console.error(`Failed to get screen off time. Code: ${err.code}, message: ${err.message}`);
    return;
  }
  console.info(`Succeeded in getting screen off time, result : ${result}`);
});

deviceSettings.getScreenOffTime

getScreenOffTime(admin: Want): Promise<number>

指定设备管理应用获取设备息屏时间,使用Promise异步回调。

需要权限: ohos.permission.ENTERPRISE_GET_SETTINGS

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

系统API: 此接口为系统接口。

参数:

参数名类型必填说明
adminWant设备管理应用。

返回值:

类型说明
Promise<number>Promise对象,返回设备息屏时间。

错误码

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

错误码ID错误信息
9200001the application is not an administrator of the device.
9200002the administrator application does not have permission to manage the device.

示例:

import Want from '@ohos.app.ability.Want';
import { BusinessError } from '@ohos.base';
let wantTemp: Want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility',
};

deviceSettings.getScreenOffTime(wantTemp).then((result) => {
  console.info(`Succeeded in getting screen off time, result : ${result}`);
}).catch((err: BusinessError) => {
  console.error(`Failed to get screen off time. Code: ${err.code}, message: ${err.message}`);
});

deviceSettings.installUserCertificate

installUserCertificate(admin: Want, certificate: CertBlob, callback: AsyncCallback<string>): void

指定设备管理应用安装用户证书,使用callback异步回调。

需要权限: ohos.permission.ENTERPRISE_MANAGE_CERTIFICATE

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

系统API: 此接口为系统接口。

参数:

参数名类型必填说明
adminWant设备管理应用。
certificateCertBlob证书信息。
callbackAsyncCallback<string>回调函数,当接口调用成功,err为null,否则为错误对象。

错误码

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

错误码ID错误信息
9200001the application is not an administrator of the device.
9200002the administrator application does not have permission to manage the device.
9201001manage certificate failed

示例:

import Want from '@ohos.app.ability.Want';
import { BusinessError } from '@ohos.base';
let wantTemp: Want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility',
};
let certFileArray: Uint8Array = new Uint8Array();
// The variable context needs to be initialized in MainAbility's onCreate callback function
// test.cer needs to be placed in the rawfile directory
getContext().resourceManager.getRawFileContent("test.cer").then((value) => {
  certFileArray = value;
  deviceSettings.installUserCertificate(wantTemp, { inData: certFileArray, alias: "cert_alias_xts" }, (err, result) => {
    if (err) {
      console.error(`Failed to install user certificate. Code: ${err.code}, message: ${err.message}`);
    } else {
      console.info(`Succeeded in installing user certificate, result : ${JSON.stringify(result)}`);
    }
  });
}).catch((error: BusinessError) => {
  console.error(`Failed to get row file content. message: ${error.message}`);
  return
});

deviceSettings.installUserCertificate

installUserCertificate(admin: Want, certificate: CertBlob): Promise<string>

指定设备管理应用安装用户证书,使用Promise异步回调。

需要权限: ohos.permission.ENTERPRISE_MANAGE_CERTIFICATE

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

系统API: 此接口为系统接口。

参数:

参数名类型必填说明
adminWant设备管理应用。
certificateCertBlob证书信息。

返回值:

类型说明
Promise<string>Promise对象,返回当前证书安装后的uri,用于卸载证书。

错误码

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

错误码ID错误信息
9200001the application is not an administrator of the device.
9200002the administrator application does not have permission to manage the device.
9201001manage certificate failed

示例:

import Want from '@ohos.app.ability.Want';
import { BusinessError } from '@ohos.base';
let wantTemp: Want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility',
};
let certFileArray: Uint8Array = new Uint8Array();
// The variable context needs to be initialized in MainAbility's onCreate callback function
// test.cer needs to be placed in the rawfile directory
getContext().resourceManager.getRawFileContent("test.cer").then((value) => {
  certFileArray = value
  deviceSettings.installUserCertificate(wantTemp, { inData: certFileArray, alias: "cert_alias_xts" })
    .then((result) => {
      console.info(`Succeeded in installing user certificate, result : ${JSON.stringify(result)}`);
    }).catch((err: BusinessError) => {
    console.error(`Failed to install user certificate. Code: ${err.code}, message: ${err.message}`);
  })
}).catch((error: BusinessError) => {
  console.error(`Failed to get row file content. message: ${error.message}`);
  return
});

CertBlob

证书信息。

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

系统API: 此接口为系统接口。

名称类型必填说明
inDataUint8Array证书的二进制内容。
aliasstring证书别名。

deviceSettings.uninstallUserCertificate

uninstallUserCertificate(admin: Want, certUri: string, callback: AsyncCallback<void>): void

指定设备管理应用卸载用户证书,使用callback异步回调。

需要权限: ohos.permission.ENTERPRISE_MANAGE_CERTIFICATE

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

系统API: 此接口为系统接口。

参数:

参数名类型必填说明
adminWant设备管理应用。
certUristring证书uri,由安装用户证书接口返回。
callbackAsyncCallback<void>回调函数,当接口调用成功,err为null,否则为错误对象。

错误码

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

错误码ID错误信息
9200001the application is not an administrator of the device.
9200002the administrator application does not have permission to manage the device.
9201001manage certificate failed

示例:

import Want from '@ohos.app.ability.Want';
let wantTemp: Want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility',
};
let aliasStr = "certName"
deviceSettings.uninstallUserCertificate(wantTemp, aliasStr, (err) => {
  if (err) {
    console.error(`Failed to uninstall user certificate. Code: ${err.code}, message: ${err.message}`);
    return;
  }
  console.info(`Succeeded in uninstalling user certificate`);
});

deviceSettings.uninstallUserCertificate

uninstallUserCertificate(admin: Want, certUri: string): Promise<void>

指定设备管理应用卸载用户证书,使用Promise异步回调。

需要权限: ohos.permission.ENTERPRISE_MANAGE_CERTIFICATE

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

系统API: 此接口为系统接口。

参数:

参数名类型必填说明
adminWant设备管理应用。
certUristring证书uri,由安装用户证书接口返回。

返回值:

类型说明
Promise<void>无返回结果的Promise对象。当指定设备管理应用卸载用户证书失败时会抛出错误对象。

错误码

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

错误码ID错误信息
9200001the application is not an administrator of the device.
9200002the administrator application does not have permission to manage the device.
9201001manage certificate failed

示例:

import Want from '@ohos.app.ability.Want';
import { BusinessError } from '@ohos.base';
let wantTemp: Want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility',
};
let aliasStr = "certName"
deviceSettings.uninstallUserCertificate(wantTemp, aliasStr).then(() => {
  console.info(`Succeeded in uninstalling user certificate`);
}).catch((err: BusinessError) => {
  console.error(`Failed to uninstall user certificate. Code is ${err.code}, message is ${err.message}`);
});

deviceSettings.setPowerPolicy11+

setPowerPolicy(admin: Want, powerScene: PowerScene, powerPolicy: PowerPolicy): void

以同步方法指定设备管理应用设置电源策略。成功返回null,失败抛出对应异常。

需要权限: ohos.permission.ENTERPRISE_MANAGE_SETTINGS

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

系统API: 此接口为系统接口。

参数:

参数名类型必填说明
adminWant设备管理应用。
powerScenePowerScene电源策略场景,当前只支持超时场景。
powerPolicyPowerPolicy电源策略。

错误码

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

错误码ID错误信息
9200001the application is not an administrator of the device.
9200002the administrator application does not have permission to manage the device.

示例:

import Want from '@ohos.app.ability.Want';
let wantTemp: Want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility',
};
try {
  let delayTime = 0;
  let powerScene: deviceSettings.PowerScene = deviceSettings.PowerScene.TIME_OUT;
  let powerPolicyAction: deviceSettings.PowerPolicyAction = deviceSettings.PowerPolicyAction.AUTO_SUSPEND;
  let powerPolicy: deviceSettings.PowerPolicy = {powerPolicyAction, delayTime};
  deviceSettings.setPowerPolicy(wantTemp, powerScene, powerPolicy);
  console.info(`Succeeded in setting power polilcy`);
} catch (error) {
  console.error(`Failed to set power policy. Code: ${err.code}, message: ${err.message}`);
}

deviceSettings.getPowerPolicy11+

getPowerPolicy(admin: Want, powerScene: PowerScene): PowerPolicy

以同步方法指定设备管理应用获取电源策略。成功返回电源策略,失败抛出对应异常。

需要权限: ohos.permission.ENTERPRISE_MANAGE_SETTINGS

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

系统API: 此接口为系统接口。

参数:

参数名类型必填说明
adminWant设备管理应用。
powerScenePowerScene电源策略场景,当前只支持超时场景。

返回值:

类型说明说明
PowerPolicyPowerPolicy电源策略。

错误码

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

错误码ID错误信息
9200001the application is not an administrator of the device.
9200002the administrator application does not have permission to manage the device.

示例:

import Want from '@ohos.app.ability.Want';
let wantTemp: Want = {
  bundleName: 'com.example.myapplication',
  abilityName: 'EntryAbility',
};
try {
  let powerScene: deviceSettings.PowerScene = deviceSettings.PowerScene.TIME_OUT;
  let powerPolicy: deviceSettings.PowerPolicy = deviceSettings.getPowerPolicy(wantTemp, powerScene);
  console.info(`Succeeded in getting power polilcy ${JSON.stringify(powerPolicy)}`);
} catch (error) {
  console.error(`Failed to get power policy. Code: ${err.code}, message: ${err.message}`);
}

PowerPolicy11+

电源策略。

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

系统API: 此接口为系统接口。

名称类型必填说明
powerPolicyActionPowerPolicyAction执行电源策略的动作。
delayTimenumber延迟时间。

PowerScene11+

执行电源策略的场景。

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

系统API: 此接口为系统接口。

名称说明
TIME_OUT0超时场景。

PowerPolicyAction11+

执行电源策略的动作。

系统能力: SystemCapability.Customization.EnterpriseDeviceManager

系统API: 此接口为系统接口。

名称说明
NONE0不执行动作。
AUTO_SUSPEND1自动进入睡眠。
FORCE_SUSPEND2强制进入睡眠。
HIBERNATE3进入休眠。(当前电源子系统暂不支持)
SHUTDOWN4关机。

你可能感兴趣的鸿蒙文章

harmony 鸿蒙接口

harmony 鸿蒙系统公共事件定义(待停用)

harmony 鸿蒙系统公共事件定义

harmony 鸿蒙开发说明

harmony 鸿蒙企业设备管理概述(仅对系统应用开放)

harmony 鸿蒙BundleStatusCallback

harmony 鸿蒙@ohos.bundle.innerBundleManager (innerBundleManager模块)

harmony 鸿蒙@ohos.distributedBundle (分布式包管理)

harmony 鸿蒙@ohos.bundle (Bundle模块)

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

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