阅读(3605) (8)

百度智能小程序 获取屏幕亮度

2020-08-13 16:19:59 更新

swan.getScreenBrightness

解释:获取屏幕亮度。

Web 态说明:Web 态暂不支持获取屏幕亮度功能,调用该 API 会触发 fail 回调。

方法参数

Object object

object 参数说明

属性名类型必填默认值说明

success

Function

接口调用成功的回调

fail

Function

接口调用失败的回调函数

complete

Function

接口调用结束的回调函数(调用成功、失败都会执行)

success 返回参数说明

参数名类型说明

value

Number

屏幕亮度值,范围 0~1,0 最暗,1 最亮。

示例 

在开发者工具中打开


图片示例

代码示例

<view class="wrap">
    <view class="card-area">
        <view class="display-area">
            <view class="brightness-title">当前屏幕亮度</view>
            <view class="value">{{value/100}}</view>
        </view>
    </view>
    <view class="card-area">
        <view class="top-description border-bottom">设置屏幕亮度</view>
        <slider min="0" max="100" value="{{value}}" step="1" class="slider" bindchanging="brightnessChange"></slider>
    </view>
    <view class="card-area">
        <view class="item-scroll block">
            <text class="switch-text">设置保持屏幕常亮</text>
            <switch bindchange="keepScreenOn"></switch>
        </view>
     </view>
    <view class="tip-week">注:屏幕亮度及屏幕常亮设置仅在当前小程序生效,离开小程序后失效。</view>
</view>
Page({
    data: {
        value: 10
    },
    onLoad(e) {
        swan.getScreenBrightness({
            success: res => {
                let value = res.value || 0.1;
                value = (value * 100).toFixed(0);
                this.setData('value', value);
            },
            fail: err => {
                console.log('getScreenBrightness fail', err);
            }
        });
    },
    brightnessChange(e) {
        const value = e.detail.value;
        this.setData('value', value);

        swan.setScreenBrightness({
            value: parseFloat(value / 100),
            success: () => {
                console.log('setScreenBrightness success');
            },
            fail: err => {
                console.log('setScreenBrightness fail', err);
            }
        });
    },
    keepScreenOn(e) {
        swan.setKeepScreenOn({
            keepScreenOn: e.detail.checked,
            success: () => {
                console.log('keepScreenOn success');
            },
            fail: err => {
                console.log('keepScreenOn fail', err);
            }
        });
    }
});

错误码

Android

错误码说明

1001

执行失败