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

使用了

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

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

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

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

let winProp = window.getWindowProperties() 
let brightness = winProp.brightness 
  • 1.
  • 2.

设置屏幕亮度:

window.setWindowBrightness 
  • 1.

参考文档: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
        })
    }
  }
}
  • 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.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
分享
微博
QQ
微信
回复
2024-12-25 10:00:42


相关问题
HarmonyOS 屏幕亮度
383浏览 • 1回复 待解决
HarmonyOS 屏幕亮度设置
444浏览 • 1回复 待解决
xComponet示例代码不能使用
1422浏览 • 1回复 待解决
HarmonyOS 设置屏幕亮度问题
765浏览 • 1回复 待解决
HarmonyOS 如何控制屏幕亮度
291浏览 • 1回复 待解决
HarmonyOS 屏幕亮度变化回调
344浏览 • 1回复 待解决
HarmonyOS 如何设置屏幕亮度呢?
1234浏览 • 1回复 待解决
HarmonyOS 如何设置/获取屏幕亮度
494浏览 • 1回复 待解决
HarmonyOS hap中不能使用命名路由吗
477浏览 • 1回复 待解决
HarmonyOS .ets文件中不能使用方法重载
387浏览 • 1回复 待解决
HarmonyOS 怎么获取当前屏幕亮度
470浏览 • 1回复 待解决
获取系统的屏幕亮度
909浏览 • 1回复 待解决
HarmonyOS 获取和设置屏幕亮度方法?
404浏览 • 0回复 待解决
如何在native层获取屏幕亮度
2238浏览 • 1回复 待解决
HarmonyOS 使用APi中方法程序不能运行
564浏览 • 1回复 待解决
ts 声明式开发不能使用js的getApp()
5004浏览 • 1回复 待解决
获取和设置应用内屏幕亮度
1818浏览 • 1回复 待解决