【背景知识】
Window:当前窗口实例,窗口管理器管理的基本单元。
setWindowKeepScreenOn:用于设置窗口保持屏幕常亮状态。
【解决方案】
- 通过createWindow方法创建窗口。
- 通过findWindow方法获取窗口对象。
- 调用setWindowKeepScreenOn方法设置窗口保持屏幕常亮状态。
import { BusinessError } from '@kit.BasicServicesKit';
import { window } from '@kit.ArkUI';
@Entry
@Component
struct Index {
build() {
Column() {
Button('设置屏幕不休眠').onClick((event: ClickEvent) => {
let config: window.Configuration = {
name: "test",
windowType: window.WindowType.TYPE_DIALOG,
ctx: getContext(this)
};
try {
window.createWindow(config, () => {
let isKeepScreenOn: boolean = true;
let windowClass: window.Window = window.findWindow("test");
try {
windowClass.setWindowKeepScreenOn(isKeepScreenOn, (err: BusinessError) => {
const errCode: number = err.code;
if (errCode) {
console.error('设置屏幕常亮失败');
return;
}
console.info('设置屏幕常亮成功');
});
} catch (exception) {
console.error('设置屏幕常亮失败');
}
})
} catch (exception) {
console.error('创建窗口失败');
}
})
}
.width('100%')
.height('100%')
.justifyContent(FlexAlign.Center)
}
}