HarmonyOS 如何设置/获取屏幕亮度

HarmonyOS
2025-01-09 16:46:13
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Heiang

先使用getLastWindow获取window示例,(也可以在EntryAbility的onWindowStageCreate里存储windowStage对象,在需要使用的地方直接通过AppStorage获取windowStage对象)

再使用setWindowBrightness来设置屏幕亮度,参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-window-V5#setwindowbrightness9

import window from '@ohos.window';
@Entry
@Component
struct WindowStagePage {

  aboutToAppear(): void {

    window.getLastWindow(getContext(),(err,data)=>{
      const errCode=err.code;
      if(errCode){
        console.log('window:',JSON.stringify(err))
        return;
      }
      data.setWindowBrightness(1.0);
    })
  }

  aboutToDisappear(): void {
    window.getLastWindow(getContext(),(err,data)=>{
      const errCode=err.code;
      if(errCode){
        console.log('window:',JSON.stringify(err))
        return;
      }
      data.setWindowBrightness(-1.0);
    })
  }

  build() {
    Column({space:10}) {
      Button("变亮")
        .onClick(() => {
          window.getLastWindow(getContext(),(err,data)=>{
            const errCode=err.code;
            if(errCode){
              console.log('window:',JSON.stringify(err))
              return;
            }
            data.setWindowBrightness(1.0);
          })
        })

      Button("变暗")
        .onClick(() => {
          window.getLastWindow(getContext(),(err,data)=>{
            const errCode=err.code;
            if(errCode){
              console.log('window:',JSON.stringify(err))
              return;
            }
            data.setWindowBrightness(0.0);
          })
        })

      Button("恢复")
        .onClick(() => {
          window.getLastWindow(getContext(),(err,data)=>{
            const errCode=err.code;
            if(errCode){
              console.log('window:',JSON.stringify(err))
              return;
            }
            data.setWindowBrightness(-1.0);
          })
        })
    }
    .width('100%')
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
分享
微博
QQ
微信
回复
2025-01-09 19:19:07
相关问题
HarmonyOS 获取设置屏幕亮度方法?
701浏览 • 0回复 待解决
HarmonyOS 屏幕亮度设置
738浏览 • 1回复 待解决
HarmonyOS 如何设置屏幕亮度呢?
1577浏览 • 1回复 待解决
获取设置应用内屏幕亮度
2244浏览 • 1回复 待解决
HarmonyOS 设置屏幕亮度问题
1199浏览 • 1回复 待解决
HarmonyOS 获取屏幕亮度值?
377浏览 • 1回复 待解决
如何在native层获取屏幕亮度
2568浏览 • 1回复 待解决
获取系统的屏幕亮度
1204浏览 • 1回复 待解决
HarmonyOS 怎么获取当前屏幕亮度
780浏览 • 1回复 待解决
HarmonyOS 屏幕亮度
703浏览 • 1回复 待解决
如何设置屏幕亮度有知道的吗?
2660浏览 • 1回复 待解决
HarmonyOS 如何控制屏幕亮度
579浏览 • 1回复 待解决
harmonyos如何实现屏幕亮度调节
283浏览 • 0回复 待解决
HarmonyOS 屏幕亮度变化回调
690浏览 • 1回复 待解决
是否有获取当前屏幕亮度的值的API
2912浏览 • 1回复 待解决
HarmonyOS 手机亮度设置
840浏览 • 1回复 待解决
HarmonyOS 如何获取系统当前亮度值?
741浏览 • 1回复 待解决
HarmonyOS 怎么获取当前环境的亮度
773浏览 • 1回复 待解决
如何设置背景色的饱和度和亮度
941浏览 • 1回复 待解决