关于设置屏幕常亮?

Api设置屏幕常亮,let promise = windowClass.setWindowKeepScreenOn(isKeepScreenOn);想知道windowClass获取的是哪个窗口对象,并没有写出来。是window.getLastWindow()获取,还是通过别的什么方法。

阅读 601
1 个回答

请参考以下代码:

import { BusinessError } from '@ohos.base';
import { window } from '@kit.ArkUI';
import common from '@ohos.app.ability.common';
@Entry
@Component
struct WebPage {
  private context = getContext(this) as common.UIAbilityContext;
  controller: webView.WebviewController = new webView.WebviewController();
  build() {
    Column() {
      Button('keeplight')
        .onClick(() => {
          // 1.获取应用主窗口。
          let windowClass: window.Window | undefined = undefined;
          try {
            window.getLastWindow(this.context, (err: BusinessError, data) => {
              const errCode: number = err.code;
              if (errCode) {
                console.error('Failed to obtain the top window. Cause: ' + JSON.stringify(err));
                return;
              }
              windowClass = data;
              console.info('Succeeded in obtaining the top window. Data: ' + JSON.stringify(data));
              try {
                windowClass.setWindowKeepScreenOn(true, (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));
              }
            });
          } catch (exception) {
            console.error('Failed to obtain the top window. Cause: ' + JSON.stringify(exception));
          }
        })
    }
  }
}

控制台输出

05-15 14:48:04.050   12178-12178  A03d00/JSAPP                   com.examp...lication  I     Succeeded in obtaining the top window. Data: {}
05-15 14:48:04.059   12178-12178  A03d00/JSAPP                   com.examp...lication  I     Succeeded in setting the screen to be always on.
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进