feat: expose screen keep-on control (#413)

This commit is contained in:
yangchao
2026-11-18 17:28:15 +08:00
committed by GitHub
parent 7fb5ae075b
commit 5313b86ed1
2 changed files with 15 additions and 0 deletions
+1
View File
@@ -26,6 +26,7 @@ declare namespace jsb {
export function getDeviceOrientation(): number; export function getDeviceOrientation(): number;
export function getNetworkType(): number; // TODO: enum type export function getNetworkType(): number; // TODO: enum type
export function getSafeAreaEdge(): NativeSafeAreaEdge; export function getSafeAreaEdge(): NativeSafeAreaEdge;
export function setKeepScreenOn(enabled: boolean): void;
export function setAccelerometerEnabled(isEnabled: boolean); export function setAccelerometerEnabled(isEnabled: boolean);
export function setAccelerometerInterval(intervalInSeconds: number); export function setAccelerometerInterval(intervalInSeconds: number);
+14
View File
@@ -26,6 +26,8 @@
*/ */
import { screenAdapter, IScreenOptions } from 'pal/screen-adapter'; 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 { legacyCC } from '../global-exports';
import { Size } from '../math'; import { Size } from '../math';
import { settings, SettingsCategory } from '../settings'; import { settings, SettingsCategory } from '../settings';
@@ -101,6 +103,18 @@ export class Screen {
return screenAdapter.devicePixelRatio; 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. * @en Get and set the size of current window in physical pixels.
* NOTE: * NOTE: