关于设置屏幕常亮问题

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

HarmonyOS
2024-08-11 13:22:40
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
put_get

请参考以下代码:

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.
分享
微博
QQ
微信
回复
2024-08-11 21:09:20
相关问题
屏幕如何实现,有哪位知道啊?
144浏览 • 1回复 待解决
如何保持屏幕,你知道吗?
1850浏览 • 1回复 待解决
如何保持屏幕,有api吗。
594浏览 • 1回复 待解决
HarmonyOS 如何保持屏幕不休眠
144浏览 • 1回复 待解决
如何监听手机屏幕的灭屏和屏?
202浏览 • 1回复 待解决
HarmonyOS 关于代理提醒的设置问题
161浏览 • 1回复 待解决
设置飞行模式,屏幕找图
3962浏览 • 1回复 待解决
HarmonyOS 如何设置屏幕亮度呢?
94浏览 • 1回复 待解决
如何设置屏幕方向为横屏
730浏览 • 1回复 待解决
获取和设置应用内屏幕亮度值
628浏览 • 1回复 待解决
如何设置屏幕的亮度有知道的吗?
1606浏览 • 1回复 待解决
OLED 屏幕输出编译时报错问题
10264浏览 • 2回复 待解决
HarmonyOS关于navigation问题
141浏览 • 1回复 待解决
关于NavPathStack的跳转问题
187浏览 • 1回复 待解决
关于Intent跳转桌面问题
7023浏览 • 1回复 待解决
关于属性动画的问题
9727浏览 • 3回复 待解决
禁用屏幕旋转的问题有知道的吗?
2280浏览 • 1回复 待解决
关于烧录时出现的问题
9782浏览 • 2回复 待解决
关于DevEco Studio harmonyos SDK问题
14500浏览 • 5回复 待解决
关于系统定位缓存问题咨询
163浏览 • 1回复 待解决
HarmonyOS关于API使用的问题
91浏览 • 1回复 待解决
关于Java UI Checkbox的问题?
5476浏览 • 1回复 待解决