往期鸿蒙全套实战文章必看:(附带鸿蒙全栈学习资料)
如何保持屏幕常亮
获取窗口实例对象后,调用setWindowKeepScreenOn方法可设置屏幕是否常亮。
import { BusinessError } from '@kit.BasicServicesKit';
import { window } from '@kit.ArkUI';
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('Failed to set the screen to be always on. Cause: ' + JSON.stringify(err));
return;
}
console.info('Succeeded in setting the screen to be always on.');
});
} catch (exception) {
console.error('Failed to set the screen to be always on. Cause: ' + JSON.stringify(exception));
}