1# @ohos.enterprise.deviceControl(设备控制管理)(系统接口) 2 3本模块提供设备控制能力。 4 5> **说明**: 6> 7> 本模块首批接口从API version 10 开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8> 9> 本模块接口仅可在Stage模型下使用。 10> 11> 本模块接口仅对[设备管理应用](../../mdm/mdm-kit-guide.md#功能介绍)开放,需将[设备管理应用激活](js-apis-enterprise-adminManager-sys.md#adminmanagerenableadmin)后调用,实现相应功能。 12> 13> 当前页面仅包含本模块的系统接口,其他公开接口参见。其他公开接口参见[@ohos.enterprise.deviceControl](js-apis-enterprise-deviceControl.md)。 14 15## 导入模块 16 17```ts 18import { deviceControl } from '@kit.MDMKit'; 19``` 20 21## deviceControl.resetFactory 22 23resetFactory(admin: Want, callback: AsyncCallback\<void>): void 24 25指定设备管理应用使设备恢复出厂设置。使用callback异步回调。 26 27**需要权限:** ohos.permission.ENTERPRISE_RESET_DEVICE 28 29**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 30 31 32**参数:** 33 34| 参数名 | 类型 | 必填 | 说明 | 35| ----- | ----------------------------------- | ---- | ------- | 36| admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 设备管理应用。 | 37| callback | AsyncCallback\<void> | 是 | 回调函数。当接口调用成功,err为null,否则为错误对象。 | 38 39**错误码:** 40 41以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。 42 43| 错误码ID | 错误信息 | 44| ------- | ---------------------------------------------------------------------------- | 45| 9200001 | The application is not an administrator application of the device. | 46| 9200002 | The administrator application does not have permission to manage the device. | 47| 201 | Permission verification failed. The application does not have the permission required to call the API. | 48| 202 | Permission verification failed. A non-system application calls a system API. | 49| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 50 51**示例:** 52 53```ts 54import { deviceControl } from '@kit.MDMKit'; 55import { Want } from '@kit.AbilityKit'; 56 57let wantTemp: Want = { 58 bundleName: 'com.example.myapplication', 59 abilityName: 'EntryAbility', 60}; 61 62deviceControl.resetFactory(wantTemp, (err) => { 63 if (err) { 64 console.error(`Failed to reset factory. Code is ${err.code}, message is ${err.message}`); 65 return; 66 } 67 console.log('Succeeded in resetting factory'); 68}) 69``` 70 71## deviceControl.resetFactory 72 73resetFactory(admin: Want): Promise\<void> 74 75指定设备管理应用使设备恢复出厂设置。使用Promise异步回调。 76 77**需要权限:** ohos.permission.ENTERPRISE_RESET_DEVICE 78 79**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 80 81 82**参数:** 83 84| 参数名 | 类型 | 必填 | 说明 | 85| ----- | ----------------------------------- | ---- | ------- | 86| admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 设备管理应用。 | 87 88**返回值:** 89 90| 类型 | 说明 | 91| ----- | ----------------------------------- | 92| Promise\<void> | 无返回结果的Promise对象。当恢复出厂设置失败时抛出错误对象。| 93 94**错误码:** 95 96以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。 97 98| 错误码ID | 错误信息 | 99| ------- | ---------------------------------------------------------------------------- | 100| 9200001 | The application is not an administrator application of the device. | 101| 9200002 | The administrator application does not have permission to manage the device. | 102| 201 | Permission verification failed. The application does not have the permission required to call the API. | 103| 202 | Permission verification failed. A non-system application calls a system API. | 104| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 105 106**示例:** 107 108```ts 109import { deviceControl } from '@kit.MDMKit'; 110import { Want } from '@kit.AbilityKit'; 111import { BusinessError } from '@kit.BasicServicesKit'; 112 113let wantTemp: Want = { 114 bundleName: 'com.example.myapplication', 115 abilityName: 'EntryAbility', 116}; 117 118deviceControl.resetFactory(wantTemp).then(() => { 119}).catch((err: BusinessError) => { 120 console.error(`Failed to reset factory. Code is ${err.code}, message is ${err.message}`); 121}) 122``` 123 124## deviceControl.shutdown<sup>11+</sup> 125 126shutdown(admin: Want): void 127 128指定设备管理应用使设备关机。 129 130**需要权限:** ohos.permission.ENTERPRISE_REBOOT 131 132**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 133 134 135**参数:** 136 137| 参数名 | 类型 | 必填 | 说明 | 138| ----- | ----------------------------------- | ---- | ------- | 139| admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 设备管理应用。 | 140 141**错误码:** 142 143以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。 144 145| 错误码ID | 错误信息 | 146| ------- | ---------------------------------------------------------------------------- | 147| 9200001 | The application is not an administrator application of the device. | 148| 9200002 | The administrator application does not have permission to manage the device. | 149| 201 | Permission verification failed. The application does not have the permission required to call the API. | 150| 202 | Permission verification failed. A non-system application calls a system API. | 151| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 152 153**示例:** 154 155```ts 156import { deviceControl } from '@kit.MDMKit'; 157import { Want } from '@kit.AbilityKit'; 158 159let wantTemp: Want = { 160 bundleName: 'com.example.myapplication', 161 abilityName: 'EntryAbility', 162}; 163 164try { 165 deviceControl.shutdown(wantTemp); 166} catch (err) { 167 console.error(`Failed to shutdown device. Code is ${err.code}, message is ${err.message}`); 168} 169``` 170 171## deviceControl.reboot<sup>11+</sup> 172 173reboot(admin: Want): void 174 175指定设备管理应用使设备重启。 176 177**需要权限:** ohos.permission.ENTERPRISE_REBOOT 178 179**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 180 181 182**参数:** 183 184| 参数名 | 类型 | 必填 | 说明 | 185| ----- | ----------------------------------- | ---- | ------- | 186| admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 设备管理应用。 | 187 188**错误码:** 189 190以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。 191 192| 错误码ID | 错误信息 | 193| ------- | ---------------------------------------------------------------------------- | 194| 9200001 | The application is not an administrator application of the device. | 195| 9200002 | The administrator application does not have permission to manage the device. | 196| 201 | Permission verification failed. The application does not have the permission required to call the API. | 197| 202 | Permission verification failed. A non-system application calls a system API. | 198| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 199 200**示例:** 201 202```ts 203import { deviceControl } from '@kit.MDMKit'; 204import { Want } from '@kit.AbilityKit'; 205 206let wantTemp: Want = { 207 bundleName: 'com.example.myapplication', 208 abilityName: 'EntryAbility', 209}; 210 211try { 212 deviceControl.reboot(wantTemp); 213} catch (err) { 214 console.error(`Failed to reboot device. Code is ${err.code}, message is ${err.message}`); 215} 216``` 217 218## deviceControl.lockScreen<sup>11+</sup> 219 220lockScreen(admin: Want): void 221 222指定设备管理应用使设备屏幕锁定。 223 224**需要权限:** ohos.permission.ENTERPRISE_LOCK_DEVICE 225 226**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 227 228 229**参数:** 230 231| 参数名 | 类型 | 必填 | 说明 | 232| ----- | ----------------------------------- | ---- | ------- | 233| admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是 | 设备管理应用。 | 234 235**错误码:** 236 237以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)和[通用错误码](../errorcode-universal.md)。 238 239| 错误码ID | 错误信息 | 240| ------- | ---------------------------------------------------------------------------- | 241| 9200001 | The application is not an administrator application of the device. | 242| 9200002 | The administrator application does not have permission to manage the device. | 243| 201 | Permission verification failed. The application does not have the permission required to call the API. | 244| 202 | Permission verification failed. A non-system application calls a system API. | 245| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 246 247**示例:** 248 249```ts 250import { deviceControl } from '@kit.MDMKit'; 251import { Want } from '@kit.AbilityKit'; 252 253let wantTemp: Want = { 254 bundleName: 'com.example.myapplication', 255 abilityName: 'EntryAbility', 256}; 257 258try { 259 deviceControl.lockScreen(wantTemp); 260} catch (err) { 261 console.error(`Failed to lock screen. Code is ${err.code}, message is ${err.message}`); 262} 263```