1# @ohos.enterprise.deviceSettings (设备设置管理) 2 3本模块提供企业设备设置能力,包括获取设备息屏时间等。 4 5> **说明:** 6> 7> 本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8> 9> 本模块接口仅可在Stage模型下使用。 10> 11> 本模块接口仅对[设备管理应用](enterpriseDeviceManagement-overview.md#基本概念)开放,需将[设备管理应用激活](js-apis-enterprise-adminManager.md#adminmanagerenableadmin)后调用,实现相应功能。 12 13## 导入模块 14 15```ts 16import deviceSettings from '@ohos.enterprise.deviceSettings'; 17``` 18 19## deviceSettings.getScreenOffTime 20 21getScreenOffTime(admin: Want, callback: AsyncCallback<number>): void 22 23指定设备管理应用获取设备息屏时间,使用callback异步回调。 24 25**需要权限:** ohos.permission.ENTERPRISE_GET_SETTINGS 26 27**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 28 29**系统API**: 此接口为系统接口。 30 31**参数:** 32 33| 参数名 | 类型 | 必填 | 说明 | 34| -------- | ---------------------------------------- | ---- | ------------------------------- | 35| admin | [Want](js-apis-app-ability-want.md) | 是 | 设备管理应用。 | 36| callback | AsyncCallback<number> | 是 | 回调函数。当接口调用成功,err为null,data为设备息屏时间,否则err为错误对象。 | 37 38**错误码**: 39 40以下错误码的详细介绍请参见[企业设备管理错误码](../errorcodes/errorcode-enterpriseDeviceManager.md) 41 42| 错误码ID | 错误信息 | 43| ------- | ---------------------------------------------------------------------------- | 44| 9200001 | the application is not an administrator of the device. | 45| 9200002 | the administrator application does not have permission to manage the device. | 46 47**示例:** 48 49```ts 50import Want from '@ohos.app.ability.Want'; 51let wantTemp: Want = { 52 bundleName: 'com.example.myapplication', 53 abilityName: 'EntryAbility', 54}; 55 56deviceSettings.getScreenOffTime(wantTemp, (err, result) => { 57 if (err) { 58 console.error(`Failed to get screen off time. Code: ${err.code}, message: ${err.message}`); 59 return; 60 } 61 console.info(`Succeeded in getting screen off time, result : ${result}`); 62}); 63``` 64 65## deviceSettings.getScreenOffTime 66 67getScreenOffTime(admin: Want): Promise<number> 68 69指定设备管理应用获取设备息屏时间,使用Promise异步回调。 70 71**需要权限:** ohos.permission.ENTERPRISE_GET_SETTINGS 72 73**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 74 75**系统API**: 此接口为系统接口。 76 77**参数:** 78 79| 参数名 | 类型 | 必填 | 说明 | 80| ----- | ----------------------------------- | ---- | ------- | 81| admin | [Want](js-apis-app-ability-want.md) | 是 | 设备管理应用。 | 82 83**返回值:** 84 85| 类型 | 说明 | 86| --------------------- | ------------------------- | 87| Promise<number> | Promise对象,返回设备息屏时间。 | 88 89**错误码**: 90 91以下错误码的详细介绍请参见[企业设备管理错误码](../errorcodes/errorcode-enterpriseDeviceManager.md) 92 93| 错误码ID | 错误信息 | 94| ------- | ---------------------------------------------------------------------------- | 95| 9200001 | the application is not an administrator of the device. | 96| 9200002 | the administrator application does not have permission to manage the device. | 97 98**示例:** 99 100```ts 101import Want from '@ohos.app.ability.Want'; 102import { BusinessError } from '@ohos.base'; 103let wantTemp: Want = { 104 bundleName: 'com.example.myapplication', 105 abilityName: 'EntryAbility', 106}; 107 108deviceSettings.getScreenOffTime(wantTemp).then((result) => { 109 console.info(`Succeeded in getting screen off time, result : ${result}`); 110}).catch((err: BusinessError) => { 111 console.error(`Failed to get screen off time. Code: ${err.code}, message: ${err.message}`); 112}); 113``` 114 115## deviceSettings.installUserCertificate 116 117installUserCertificate(admin: Want, certificate: CertBlob, callback: AsyncCallback<string>): void 118 119指定设备管理应用安装用户证书,使用callback异步回调。 120 121**需要权限:** ohos.permission.ENTERPRISE_MANAGE_CERTIFICATE 122 123**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 124 125**系统API**: 此接口为系统接口。 126 127**参数:** 128 129| 参数名 | 类型 | 必填 | 说明 | 130| -------- | ---------------------------------------- | ---- | ------------------------------- | 131| admin | [Want](js-apis-app-ability-want.md) | 是 | 设备管理应用。 | 132| certificate | [CertBlob](#certblob) | 是 | 证书信息。 | 133| callback | AsyncCallback<string> | 是 | 回调函数,当接口调用成功,err为null,否则为错误对象。 | 134 135**错误码**: 136 137以下错误码的详细介绍请参见[企业设备管理错误码](../errorcodes/errorcode-enterpriseDeviceManager.md) 138 139| 错误码ID | 错误信息 | 140| ------- | ---------------------------------------------------------------------------- | 141| 9200001 | the application is not an administrator of the device. | 142| 9200002 | the administrator application does not have permission to manage the device. | 143| 9201001 | manage certificate failed | 144 145**示例:** 146 147```ts 148import Want from '@ohos.app.ability.Want'; 149import { BusinessError } from '@ohos.base'; 150let wantTemp: Want = { 151 bundleName: 'com.example.myapplication', 152 abilityName: 'EntryAbility', 153}; 154let certFileArray: Uint8Array = new Uint8Array(); 155// The variable context needs to be initialized in MainAbility's onCreate callback function 156// test.cer needs to be placed in the rawfile directory 157getContext().resourceManager.getRawFileContent("test.cer").then((value) => { 158 certFileArray = value; 159 deviceSettings.installUserCertificate(wantTemp, { inData: certFileArray, alias: "cert_alias_xts" }, (err, result) => { 160 if (err) { 161 console.error(`Failed to install user certificate. Code: ${err.code}, message: ${err.message}`); 162 } else { 163 console.info(`Succeeded in installing user certificate, result : ${JSON.stringify(result)}`); 164 } 165 }); 166}).catch((error: BusinessError) => { 167 console.error(`Failed to get row file content. message: ${error.message}`); 168 return 169}); 170``` 171 172## deviceSettings.installUserCertificate 173 174installUserCertificate(admin: Want, certificate: CertBlob): Promise<string> 175 176指定设备管理应用安装用户证书,使用Promise异步回调。 177 178**需要权限:** ohos.permission.ENTERPRISE_MANAGE_CERTIFICATE 179 180**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 181 182**系统API**: 此接口为系统接口。 183 184**参数:** 185 186| 参数名 | 类型 | 必填 | 说明 | 187| ----- | ----------------------------------- | ---- | ------- | 188| admin | [Want](js-apis-app-ability-want.md) | 是 | 设备管理应用。 | 189| certificate | [CertBlob](#certblob) | 是 | 证书信息。 | 190 191**返回值:** 192 193| 类型 | 说明 | 194| --------------------- | ------------------------- | 195| Promise<string> | Promise对象,返回当前证书安装后的uri,用于卸载证书。 | 196 197**错误码**: 198 199以下错误码的详细介绍请参见[企业设备管理错误码](../errorcodes/errorcode-enterpriseDeviceManager.md) 200 201| 错误码ID | 错误信息 | 202| ------- | ---------------------------------------------------------------------------- | 203| 9200001 | the application is not an administrator of the device. | 204| 9200002 | the administrator application does not have permission to manage the device. | 205| 9201001 | manage certificate failed | 206 207**示例:** 208 209```ts 210import Want from '@ohos.app.ability.Want'; 211import { BusinessError } from '@ohos.base'; 212let wantTemp: Want = { 213 bundleName: 'com.example.myapplication', 214 abilityName: 'EntryAbility', 215}; 216let certFileArray: Uint8Array = new Uint8Array(); 217// The variable context needs to be initialized in MainAbility's onCreate callback function 218// test.cer needs to be placed in the rawfile directory 219getContext().resourceManager.getRawFileContent("test.cer").then((value) => { 220 certFileArray = value 221 deviceSettings.installUserCertificate(wantTemp, { inData: certFileArray, alias: "cert_alias_xts" }) 222 .then((result) => { 223 console.info(`Succeeded in installing user certificate, result : ${JSON.stringify(result)}`); 224 }).catch((err: BusinessError) => { 225 console.error(`Failed to install user certificate. Code: ${err.code}, message: ${err.message}`); 226 }) 227}).catch((error: BusinessError) => { 228 console.error(`Failed to get row file content. message: ${error.message}`); 229 return 230}); 231``` 232 233## CertBlob 234 235证书信息。 236 237**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 238 239**系统API**: 此接口为系统接口。 240 241| 名称 | 类型 | 必填 | 说明 | 242| ----------- | --------| ----- | ------------------------------- | 243| inData | Uint8Array | 是 | 证书的二进制内容。 | 244| alias | string | 是 | 证书别名。 | 245 246## deviceSettings.uninstallUserCertificate 247 248uninstallUserCertificate(admin: Want, certUri: string, callback: AsyncCallback<void>): void 249 250指定设备管理应用卸载用户证书,使用callback异步回调。 251 252**需要权限:** ohos.permission.ENTERPRISE_MANAGE_CERTIFICATE 253 254**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 255 256**系统API**: 此接口为系统接口。 257 258**参数:** 259 260| 参数名 | 类型 | 必填 | 说明 | 261| -------- | ---------------------------------------- | ---- | ------------------------------- | 262| admin | [Want](js-apis-app-ability-want.md) | 是 | 设备管理应用。 | 263| certUri | string | 是 | 证书uri,由安装用户证书接口返回。 | 264| callback | AsyncCallback<void> | 是 | 回调函数,当接口调用成功,err为null,否则为错误对象。 | 265 266**错误码**: 267 268以下错误码的详细介绍请参见[企业设备管理错误码](../errorcodes/errorcode-enterpriseDeviceManager.md) 269 270| 错误码ID | 错误信息 | 271| ------- | ---------------------------------------------------------------------------- | 272| 9200001 | the application is not an administrator of the device. | 273| 9200002 | the administrator application does not have permission to manage the device. | 274| 9201001 | manage certificate failed | 275 276**示例:** 277 278```ts 279import Want from '@ohos.app.ability.Want'; 280let wantTemp: Want = { 281 bundleName: 'com.example.myapplication', 282 abilityName: 'EntryAbility', 283}; 284let aliasStr = "certName" 285deviceSettings.uninstallUserCertificate(wantTemp, aliasStr, (err) => { 286 if (err) { 287 console.error(`Failed to uninstall user certificate. Code: ${err.code}, message: ${err.message}`); 288 return; 289 } 290 console.info(`Succeeded in uninstalling user certificate`); 291}); 292``` 293 294## deviceSettings.uninstallUserCertificate 295 296uninstallUserCertificate(admin: Want, certUri: string): Promise<void> 297 298指定设备管理应用卸载用户证书,使用Promise异步回调。 299 300**需要权限:** ohos.permission.ENTERPRISE_MANAGE_CERTIFICATE 301 302**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 303 304**系统API**: 此接口为系统接口。 305 306**参数:** 307 308| 参数名 | 类型 | 必填 | 说明 | 309| ----- | ----------------------------------- | ---- | ------- | 310| admin | [Want](js-apis-app-ability-want.md) | 是 | 设备管理应用。 | 311| certUri | string | 是 | 证书uri,由安装用户证书接口返回。 | 312 313**返回值:** 314 315| 类型 | 说明 | 316| --------------------- | ------------------------- | 317| Promise<void> | 无返回结果的Promise对象。当指定设备管理应用卸载用户证书失败时会抛出错误对象。 | 318 319**错误码**: 320 321以下错误码的详细介绍请参见[企业设备管理错误码](../errorcodes/errorcode-enterpriseDeviceManager.md) 322 323| 错误码ID | 错误信息 | 324| ------- | ---------------------------------------------------------------------------- | 325| 9200001 | the application is not an administrator of the device. | 326| 9200002 | the administrator application does not have permission to manage the device. | 327| 9201001 | manage certificate failed | 328 329**示例:** 330 331```ts 332import Want from '@ohos.app.ability.Want'; 333import { BusinessError } from '@ohos.base'; 334let wantTemp: Want = { 335 bundleName: 'com.example.myapplication', 336 abilityName: 'EntryAbility', 337}; 338let aliasStr = "certName" 339deviceSettings.uninstallUserCertificate(wantTemp, aliasStr).then(() => { 340 console.info(`Succeeded in uninstalling user certificate`); 341}).catch((err: BusinessError) => { 342 console.error(`Failed to uninstall user certificate. Code is ${err.code}, message is ${err.message}`); 343}); 344```