Add the following configuration to the features attribute in the manifest.json file:
{"name": "system.brightness"} Add the following configuration to the script element on the page where the API will be called:
import brightness from '@system.brightness'
Or
var brightness = require("@system.brightness") Item | Description |
|---|---|
Applicable devices | Mobile phones, tablets, and telematics devices |
Applicable regions | Global |
API | Description |
|---|---|
Obtains the current screen brightness. | |
Sets the screen brightness. | |
Obtains the current screen brightness adjustment mode. | |
Sets the screen brightness adjustment mode. | |
Sets whether to keep the screen steady on. |
Description
Obtains the current screen brightness.
OBJECT parameters
Parameter | Type | Mandatory | Description |
|---|---|---|---|
success | function | No | Callback upon success. |
fail | function | No | Callback upon failure. |
complete | function | No | Callback upon completion. |
Parameters returned by success
Parameter | Type | Description |
|---|---|---|
value | number | Screen brightness. The value ranges from 0 to 255. |
Sample code
brightness.getValue({
success:function(ret){console.log("handling success");}
}) Description
Sets the screen brightness.
OBJECT parameters
Parameter | Type | Mandatory | Description |
|---|---|---|---|
value | number | Yes | Screen brightness. The value is an integer ranging from 0 to 255.
|
success | function | No | Callback upon success. |
fail | function | No | Callback upon failure. |
complete | function | No | Callback upon completion. |
Sample code
brightness.setValue({
success:function(data){console.log("handling success"); },
fail: function(data, code){console.log("handling fail, code=" + code);}
}) Description
Obtains the current screen brightness adjustment mode.
OBJECT parameters
Parameter | Type | Mandatory | Description |
|---|---|---|---|
success | function | No | Callback upon success. |
fail | function | No | Callback upon failure. |
complete | function | No | Callback upon completion. |
Parameters returned by success
Parameter | Type | Description |
|---|---|---|
mode | number |
|
Sample code
brightness.getMode({
success:function(ret){console.log("handling success");}
}) Description
Sets the screen brightness adjustment mode.
OBJECT parameters
Parameter | Type | Mandatory | Description |
|---|---|---|---|
mode | number | Yes |
|
success | function | No | Callback upon success. |
fail | function | No | Callback upon failure. |
complete | function | No | Callback upon completion. |
Sample code
brightness.setMode({
success:function(data){console.log("handling success"); },
fail: function(data, code){console.log("handling fail, code=" + code);}
}) Description
Sets whether to keep the screen steady on.
OBJECT parameters
Parameter | Type | Mandatory | Description |
|---|---|---|---|
keepScreenOn | boolean | Yes | Indicates whether to keep the screen steady on. |
success | function | No | Callback upon success. |
fail | function | No | Callback upon failure. |
complete | function | No | Callback upon completion. |
Sample code
brightness.setKeepScreenOn({
keepScreenOn: true,
success: function () {
console.log(`handling success`)
},
fail: function (data, code) {
}
}) <template>
<div class="container">
<div class="item-container">
<div class="item-content">
<text>get brightness: {{value}}</text>
</div>
<input type="button" class="btn" onclick="getValue" value="getbrightness"/>
</div>
<div class="item-container">
<div class="item-content">
<text>set brightness value: {{sliderValue}}</text>
<slider style="width: 100%" min="0" max="255" step="1" value="0" onchange="getSliderValue"></slider>
<text>set brightness: {{status}} </text>
</div>
<input type="button" class="btn" onclick="setValue" value="setbrightness"/>
</div>
<div class="item-container">
<div class="item-content">
<text>brightness mode: {{mode}}</text>
</div>
<input type="button" class="btn" onclick="getMode" value="getbrightnessmode"/>
</div>
<div class="item-container">
<div class="item-content">
<text>modestatus: {{setModStatus}}</text>
</div>
<input type="button" class="btn" onclick="setMode(0)" value="set manual adjustment(0)"/>
<input type="button" class="btn" onclick="setMode(1)" value="set automatic adjustment(1)"/>
</div>
<div class="item-container">
<div class="item-content">
<text>setkeepScreenStatus: {{keepScreenStatus}}</text>
</div>
<input type="button" class="btn" onclick="setKeepScreenOn" value="setKeepScreenOn"/>
</div>
</div>
</template>
<style>
.container{
flex: 1;
flex-direction: column;
}
.item-container {
margin-bottom: 50px;
margin-right: 60px;
margin-left: 60px;
flex-direction: column;
}
.item-content {
flex-direction: column;
background-color: #ffffff;
padding: 30px;
margin-bottom: 50px;
align-items: flex-start;
}
.btn {
height: 80px;
text-align: center;
border-radius: 5px;
margin-right: 60px;
margin-left: 60px;
margin-bottom: 50px;
color: #ffffff;
font-size: 30px;
background-color: #0faeff;
}
</style>
<script>
import brightness from '@system.brightness'
export default {
private: {
componentName: 'Brightness',
value: 0,
sliderValue: 0,
status: '',
mode: '',
setModStatus: '',
keepScreenStatus:'',
},
onInit () {
this.$page.setTitleBar({text: 'Brightness'})
},
getValue () {
const self = this
brightness.getValue({
success: function (ret) {
self.value = ret.value
},
fail: function (error) {
self.value = error
}
})
},
getSliderValue (e) {
this.sliderValue = e.progress
},
setValue () {
const self = this
brightness.setValue({
value: self.sliderValue,
success: function () {
self.status = 'success';
},
fail: function (err) {
self.status = 'set brightness failed: ' + err;
}
})
},
getMode () {
const self = this
brightness.getMode({
success: function (ret) {
self.mode = ret.mode
}
})
},
setMode (index) {
const self = this
brightness.setMode({
mode: index,
success: function(){
self.setModStatus = 'mode=' + index
},
fail: function(err, code){
self.setModStatus = 'handling fail, code=' + code
}
})
},
setKeepScreenOn(){
let self = this;
brightness.setKeepScreenOn({
keepScreenOn: true,
success() {
self.keepScreenStatus = 'success'
console.log(`handling success`)
},
fail(data, code) {
console.log(`handling fail, data=${data},code=${code}`)
}
})
}
}
</script>
The following figure shows the final effect.
