harmony 鸿蒙@ohos.power (Power Management) (System API)

  • 2025-06-12
  • 浏览 (4)

@ohos.power (Power Management) (System API)

The power module provides APIs for rebooting and shutting down the system, as well as querying the screen status.

NOTE

The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version.

This topic describes only the system APIs provided by the module. For details about its public APIs, see @ohos.power (Power Management).

Modules to Import

import {power} from '@kit.BasicServicesKit';

power.shutdown

shutdown(reason: string): void

Shuts down the system.

System API: This is a system API.

Required permission: ohos.permission.REBOOT

System capability: SystemCapability.PowerManager.PowerManager.Core

Parameters

Name Type Mandatory Description
reason string Yes Shutdown reason. The value must be a string.

Error codes

For details about the error codes, see Power Manager Error Codes.

ID Error Message
4900101 Failed to connect to the service.
401 Parameter error. Possible causes: 1.Incorrect parameter types.
201 Permission verification failed. The application does not have the permission required to call the API.
202 Permission verification failed. A non-system application calls a system API.

Example:

try {
    power.shutdown('shutdown_test');
} catch(err) {
    console.error('shutdown failed, err: ' + err);
}

power.reboot9+

reboot(reason: string): void

The device is restarted.

System API: This is a system API.

Required permission: ohos.permission.REBOOT

System capability: SystemCapability.PowerManager.PowerManager.Core

Parameters

Name Type Mandatory Description
reason string Yes Restart reason. The value must be a string.

Error codes

For details about the error codes, see Power Manager Error Codes.

ID Error Message
4900101 Failed to connect to the service.
401 Parameter error. Possible causes: 1.Incorrect parameter types.
201 Permission verification failed. The application does not have the permission required to call the API.
202 Permission verification failed. A non-system application calls a system API.

Example:

try {
    power.reboot('reboot_test');
} catch(err) {
    console.error('reboot failed, err: ' + err);
}

power.wakeup9+

wakeup(detail: string): void

Wakes up a device.

System API: This is a system API.

Required permissions: ohos.permission.POWER_MANAGER

For API version 9 to 17, no permission is required to use this API. Since API version 18, this permission is required.

System capability: SystemCapability.PowerManager.PowerManager.Core

Parameters

Name Type Mandatory Description
detail string Yes Wakeup reason. The value must be a string.

Error codes

For details about the error codes, see Power Manager Error Codes.

ID Error Message
4900101 Failed to connect to the service.
401 Parameter error. Possible causes: 1.Incorrect parameter types.
201 Permission verification failed. The application does not have the permission required to call the API.
202 Permission verification failed. A non-system application calls a system API.

Example:

try {
    power.wakeup('wakeup_test');
} catch(err) {
    console.error('wakeup failed, err: ' + err);
}

power.suspend9+

suspend(isImmediate?: boolean): void

Hibernates a device.

System API: This is a system API.

Required permissions: ohos.permission.POWER_MANAGER

For API version 9 to 17, no permission is required to use this API. Since API version 18, this permission is required.

System capability: SystemCapability.PowerManager.PowerManager.Core

Parameters

Name Type Mandatory Description
isImmediate10+ boolean No Whether to hibernate a device immediately. If this parameter is not specified, the default value false is used. The system automatically determines when to enter the hibernation state.
NOTE: This parameter is supported since API version 10.

Error codes

For details about the error codes, see Power Manager Error Codes.

ID Error Message
4900101 Failed to connect to the service.
201 Permission verification failed. The application does not have the permission required to call the API.
202 Permission verification failed. A non-system application calls a system API.
401 Parameter error. Possible causes: 1.Incorrect parameter types.

Example:

try {
    power.suspend();
} catch(err) {
    console.error('suspend failed, err: ' + err);
}

power.setPowerMode9+

setPowerMode(mode: DevicePowerMode, callback: AsyncCallback<void>): void

Sets the power mode of this device. This API uses an asynchronous callback to return the result.

System API: This is a system API.

Required permission: ohos.permission.POWER_OPTIMIZATION

System capability: SystemCapability.PowerManager.PowerManager.Core

Parameters

Name Type Mandatory Description
mode DevicePowerMode Yes Power mode. The value must be an enum.
callback AsyncCallback<void> Yes Callback invoked to return the result.
If the power mode is successfully set, err is undefined; otherwise, err is an error object.

Error codes

For details about the error codes, see Power Manager Error Codes.

ID Error Message
4900101 Failed to connect to the service.
401 Parameter error. Possible causes: 1.Parameter verification failed.
201 Permission verification failed. The application does not have the permission required to call the API.
202 Permission verification failed. A non-system application calls a system API.

Example:

power.setPowerMode(power.DevicePowerMode.MODE_PERFORMANCE, (err: Error) => {
    if (typeof err === 'undefined') {
        console.info('set power mode to MODE_PERFORMANCE');
    } else {
        console.error('set power mode failed, err: ' + err);
    }
});

power.setPowerMode9+

setPowerMode(mode: DevicePowerMode): Promise<void>

Sets the power mode of this device. This API uses a promise to return the result.

System API: This is a system API.

Required permission: ohos.permission.POWER_OPTIMIZATION

System capability: SystemCapability.PowerManager.PowerManager.Core

Parameters

Name Type Mandatory Description
mode DevicePowerMode Yes Power mode. The value must be an enum.

Return value

Type Description
Promise<void> Promise that returns no value.

Error codes

For details about the error codes, see Power Manager Error Codes.

ID Error Message
4900101 Failed to connect to the service.
401 Parameter error. Possible causes: 1.Parameter verification failed.
201 Permission verification failed. The application does not have the permission required to call the API.
202 Permission verification failed. A non-system application calls a system API.

Example:

power.setPowerMode(power.DevicePowerMode.MODE_PERFORMANCE)
.then(() => {
    console.info('set power mode to MODE_PERFORMANCE');
})
.catch((err : Error)=> {
    console.error('set power mode failed, err: ' + err);
});

power.setScreenOffTime12+

setScreenOffTime(timeout: number): void

Set the screen-off timeout duration.

System API: This is a system API.

Required permissions: ohos.permission.POWER_MANAGER

For API version 12 to 17, no permission is required to use this API. Since API version 18, this permission is required.

System capability: SystemCapability.PowerManager.PowerManager.Core

Parameters

Name Type Mandatory Description
timeout number Yes Screen-off timeout duration, in milliseconds. A value greater than 0 indicates the specified timeout duration is used, and the value -1 indicates that the default timeout duration is used. Other values are invalid.

Error codes

For details about the error codes, see Power Manager Error Codes.

ID Error Message
4900101 Failed to connect to the service.
401 Parameter error. Possible causes: 1. Parameter verification failed.
201 Permission verification failed. The application does not have the permission required to call the API.
202 Permission verification failed. A non-system application calls a system API.

Example:

try {
    power.setScreenOffTime(30000);
} catch(err) {
    console.error('set screen off time failed, err: ' + err);
}

power.hibernate12+

hibernate(clearMemory: boolean): void

Hibernates a device.

System API: This is a system API.

Required permissions: ohos.permission.POWER_MANAGER

For API version 12 to 17, no permission is required to use this API. Since API version 18, this permission is required.

System capability: SystemCapability.PowerManager.PowerManager.Core

Parameters

Name Type Mandatory Description
clearMemory boolean Yes Whether to clear the memory. The value true means to clear the memory before the system enters the hibernation state, and the value false means the opposite.

Error codes

For details about the error codes, see Power Manager Error Codes.

ID Error Message
4900101 Failed to connect to the service.
201 Permission verification failed. The application does not have the permission required to call the API.
202 Permission verification failed. A non-system application calls a system API.
401 Parameter error. Possible causes: 1.Incorrect parameter types.

Example:

try {
    power.hibernate(true);
} catch(err) {
    console.error('hibernate failed, err: ' + err);
}

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Basic Services Kit

harmony 鸿蒙DeviceInfo

harmony 鸿蒙InitSync

harmony 鸿蒙OH_Print

harmony 鸿蒙OsAccount

harmony 鸿蒙Pasteboard

harmony 鸿蒙Print_Margin

harmony 鸿蒙Print_PageSize

harmony 鸿蒙Print_PrintAttributes

harmony 鸿蒙Print_PrintDocCallback

0  赞