HarmonyOS @system.brightness (屏幕亮度) 不能使用,APP无法运行

使用了

import brightness, { BrightnessModeResponse, BrightnessResponse } from '@system.brightness' 

获取和设置屏幕亮度,APP无法运行。

HarmonyOS
2024-12-25 07:28:06
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
put_get

@system.brightness接口已经弃用,可通过窗口获取和设置屏幕亮度。获取屏幕亮度:

let winProp = window.getWindowProperties() 
let brightness = winProp.brightness 

设置屏幕亮度:

window.setWindowBrightness 

参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-window-V5#setwindowbrightness9

可以参考下述demo:

import window from '@ohos.window';
import { BusinessError } from '@ohos.base';
import promptAction from '@ohos.promptAction';

@Entry
@Component
struct Index {
  @State currentScreenBrightness: number = 0;
  @State clickCount: number = 0;
  @State baseBrightness: number = 0.1;
  @State operation: string = 'increase';

  /**
   * 设置应用内屏幕亮度
   */
  setScreenBrightness(brightness: number) {
    window.getLastWindow(getContext(this)).then(currentWindow => {
      currentWindow.setWindowBrightness(brightness, (err: BusinessError) => {
        const errCode: number = err.code;
        if (errCode) {
          console.error('Failed to set the brightness. Cause: ' + JSON.stringify(err));
          return;
        }
        console.info('Succeeded in setting the brightness.');
      });
    });
  }

  /**
   * 获取应用内屏幕亮度
   */
  getScreenBrightness() {
    window.getLastWindow(getContext(this)).then(currentWindow => {
      let properties = currentWindow.getWindowProperties();
      this.currentScreenBrightness = properties.brightness;
      console.info(`properties:${JSON.stringify(properties)}`)
    });
  }

  build() {
    Column() {
      Text(`current screen brightness: ${this.currentScreenBrightness}`)
      Button(`click to ${this.operation} brightness`)
        .onClick(() => {
          if (this.operation === 'increase') {
            this.clickCount++;
            if (this.baseBrightness * this.clickCount <= 1.0) {
              this.setScreenBrightness(this.baseBrightness * this.clickCount);
            } else {
              promptAction.showToast({
                message: '亮度值已达到上限!'
              })
              this.operation = 'decrease';
            }
          } else if (this.operation === 'decrease') {
            this.clickCount--;
            if (this.baseBrightness * this.clickCount >= 0.0) {
              this.setScreenBrightness(this.baseBrightness * this.clickCount);
            } else {
              promptAction.showToast({
                message: '亮度值已达到下限!'
              })
              this.operation = 'increase';
            }
          }

        })
        .margin({
          top: 20,
          bottom: 20
        })
      Button('click to get brightness')
        .onClick(() => {
          this.getScreenBrightness();
        })
        .margin({
          bottom: 20
        })
    }
  }
}
分享
微博
QQ
微信
回复
2024-12-25 10:00:42
相关问题
HarmonyOS 屏幕亮度
228浏览 • 1回复 待解决
HarmonyOS 屏幕亮度设置
199浏览 • 1回复 待解决
xComponet示例代码不能使用
1138浏览 • 1回复 待解决
HarmonyOS 如何控制屏幕亮度
129浏览 • 1回复 待解决
HarmonyOS 设置屏幕亮度问题
467浏览 • 1回复 待解决
HarmonyOS hap中不能使用命名路由吗
262浏览 • 1回复 待解决
HarmonyOS .ets文件中不能使用方法重载
119浏览 • 1回复 待解决
HarmonyOS 屏幕亮度变化回调
151浏览 • 1回复 待解决
HarmonyOS 如何设置/获取屏幕亮度
195浏览 • 1回复 待解决
HarmonyOS 如何设置屏幕亮度呢?
939浏览 • 1回复 待解决
HarmonyOS 获取和设置屏幕亮度方法?
305浏览 • 0回复 待解决
HarmonyOS 怎么获取当前屏幕亮度
272浏览 • 1回复 待解决
获取系统的屏幕亮度
772浏览 • 1回复 待解决
ts 声明式开发不能使用js的getApp()
4765浏览 • 1回复 待解决
如何在native层获取屏幕亮度
2108浏览 • 1回复 待解决
HarmonyOS 使用APi中方法程序不能运行
339浏览 • 1回复 待解决
获取和设置应用内屏幕亮度
1379浏览 • 1回复 待解决