diff --git a/@types/jsb.d.ts b/@types/jsb.d.ts index 8d6225b006..785280681f 100644 --- a/@types/jsb.d.ts +++ b/@types/jsb.d.ts @@ -26,6 +26,7 @@ declare namespace jsb { export function getDeviceOrientation(): number; export function getNetworkType(): number; // TODO: enum type export function getSafeAreaEdge(): NativeSafeAreaEdge; + export function setKeepScreenOn(enabled: boolean): void; export function setAccelerometerEnabled(isEnabled: boolean); export function setAccelerometerInterval(intervalInSeconds: number); diff --git a/cocos/core/platform/screen.ts b/cocos/core/platform/screen.ts index f57b210933..5bb496fb0e 100644 --- a/cocos/core/platform/screen.ts +++ b/cocos/core/platform/screen.ts @@ -26,6 +26,8 @@ */ import { screenAdapter, IScreenOptions } from 'pal/screen-adapter'; +import { systemInfo } from 'pal/system-info'; +import { OS } from '../../../pal/system-info/enum-type'; import { legacyCC } from '../global-exports'; import { Size } from '../math'; import { settings, SettingsCategory } from '../settings'; @@ -101,6 +103,18 @@ export class Screen { return screenAdapter.devicePixelRatio; } + /** + * @en Controls whether the screen stays on while the app is in the foreground. + * Supported on native Android and iOS. Has no effect on other platforms. + * @zh 控制应用在前台时是否保持屏幕常亮。仅在 Android 和 iOS 原生平台生效。 + * @param enabled @en Pass true to keep the screen on or false to restore normal screen timeout. @zh 传入 true 开启常亮,传入 false 恢复系统的自动熄屏行为。 + */ + public setKeepScreenOn (enabled: boolean): void { + if (systemInfo.isNative && (systemInfo.os === OS.ANDROID || systemInfo.os === OS.IOS)) { + jsb.device.setKeepScreenOn(enabled); + } + } + /** * @en Get and set the size of current window in physical pixels. * NOTE: