my.getScreenBrightness

Get the screen brightness.

Sample Code

copy
<!-- API-DEMO page/API/screen/screen.axml-->
<view class="page">
  <view class="page-description">Screen brightness API</view>
  <view class="page-section">
    <view class="page-section-title">Set whether to keep screen on</view>
    <view class="page-section-demo">
      <switch checked="{{status}}" onChange="switchKeepScreenOn"/>
    </view>
  </view>
  <view class="page-section">
    <view class="page-section-title">Set screen brightness</view>
    <view class="page-section-demo">
      <slider value="{{brightness}}" max="1" min="0" onChange="sliderChange" step="0.02"/>
    </view>
  </view>
  <view class="page-section">
    <view class="page-section-title">Get screen brightness</view>
    <view class="page-section-demo">
      <button type="primary" onTap="getBrightness">Get screen brightness</button>
    </view>
  </view>
</view>
copy
// API-DEMO page/API/screen/screen.js
Page({
  data: {
    status: false,
    brightness: 1,
  },
  onLoad() {
    my.getScreenBrightness({
      success: res => {
        this.setData({
          brightness: res.brightness
        })
      },
    })
  },
  sliderChange(e) {
    my.setScreenBrightness({
      brightness: e.detail.value,
      success: (res) => {
        this.setData({
          brightness: e.detail.value,
        })
      }
    })
  },
  switchKeepScreenOn(e) {
    my.setKeepScreenOn({
      keepScreenOn: e.detail.value,
      success: (res) => {
        this.setData({
          status: e.detail.value,
        })
      }
    })
  },
  getBrightness() {
    my.getScreenBrightness({
      success: res => {
        my.alert({
          content: `Current screen brightness: ${res.brightness}`
        });
      }
    })
  }
});

Parameters

Object type with the following attributes:

Property

Type

Required

Description

success

Function

No

The callback function upon call success.

fail

Function

No

The callback function that is called when the JSAPI call fails. When executed, it receives an err object.

complete

Function

No

The callback function upon call completion (to be executed upon either call success or failure).

err object

Parameter

Data type

Required

Description

error

Number

Yes

An error code that represents the specific error encountered.

errorMessage

String

Yes

An error message that describes the corresponding error code.

Tips

The brightness value output by Xiaomi phones is not consistent with the actual device brightness, therefore this JSAPI cannot retrieve the correct device brightness.