my.setKeepScreenOn
Version requirements: Basic library 1.3.0 or higher version. If the version is low, you can programmatically check for Compatibility.
Set whether to keep screen on Takes effect in the current Mini Program only.
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:
Attributes | Type | Mandatory | Description |
keepScreenOn | Boolean | Yes | Whether to keep screen on |
success | Function | No | Callback function for interface call success |
fail | Function | No | Callback function for interface call failure |
complete | Function | No | Callback function for interface call completion (to be executed for both call success and failure) |