All Products
Search
Document Center

Screen brightness

Last Updated: Feb 04, 2021

my.setKeepScreenOn(OBJECT)

Note:
  • This interface is supported since basic library version 1.3.0. Compatibility processing is required for earlier versions. See Mini Program base library to learn more.
  • This interface is only supported in mPaaS 10.1.32 and later versions.

This interface is used to set whether to keep the screen on. The interface only works in the current Mini Program.

Parameters

Parameter Type Required Description
keepScreenOn Boolean Yes Whether to keep screen on.
success Function No Callback function upon call success.
fail Function No Callback function upon call failure.
complete Function No Callback function upon call completion (to be executed upon either call success or failure).

Sample code

 
  1. <!-- API-DEMO page/API/screen/screen.axml-->
  2. <view class="page">
  3. <view class="page-description">Screen brightness API</view>
  4. <view class="page-section">
  5. <view class="page-section-title">Set whether to keep screen on</view>
  6. <view class="page-section-demo">
  7. <switch checked="{{status}}" onChange="switchKeepScreenOn"/>
  8. </view>
  9. </view>
  10. <view class="page-section">
  11. <view class="page-section-title">Set screen brightness</view>
  12. <view class="page-section-demo">
  13. <slider value="{{brightness}}" max="1" min="0" onChange="sliderChange" step="0.02"/>
  14. </view>
  15. </view>
  16. <view class="page-section">
  17. <view class="page-section-title">Get screen brightness</view>
  18. <view class="page-section-demo">
  19. <button type="primary" onTap="getBrightness">Get screen brightness</button>
  20. </view>
  21. </view>
  22. </view>
 
  1. // API-DEMO page/API/screen/screen.js
  2. Page({
  3. data: {
  4. status: false,
  5. brightness: 1,
  6. },
  7. onLoad() {
  8. my.getScreenBrightness({
  9. success: res => {
  10. this.setData({
  11. brightness: res.brightness
  12. })
  13. },
  14. })
  15. },
  16. sliderChange(e) {
  17. my.setScreenBrightness({
  18. brightness: e.detail.value,
  19. success: (res) => {
  20. this.setData({
  21. brightness: e.detail.value,
  22. })
  23. }
  24. })
  25. },
  26. switchKeepScreenOn(e) {
  27. my.setKeepScreenOn({
  28. keepScreenOn: e.detail.value,
  29. success: (res) => {
  30. this.setData({
  31. status: e.detail.value,
  32. })
  33. }
  34. })
  35. },
  36. getBrightness() {
  37. my.getScreenBrightness({
  38. success: res => {
  39. my.alert({
  40. content: `Current screen brightness: ${res.brightness}`
  41. });
  42. }
  43. })
  44. }
  45. });

my.getScreenBrightness(OBJECT)

Note:
  • This interface is supported since basic library version 1.4.0. Compatibility processing is required for earlier versions. See Mini Program base library to learn more.
  • This interface is only supported in mPaaS 10.1.32 and later versions.

This interface is used to get the screen brightness.

Parameters

Parameter Type Required Description
success Function No Callback function upon call success.
fail Function No Callback function upon call failure.
complete Function No Callback function upon call completion (to be executed upon either call success or failure).

Sample code

 
  1. <!-- API-DEMO page/API/screen/screen.axml-->
  2. <view class="page">
  3. <view class="page-description">Screen brightness API</view>
  4. <view class="page-section">
  5. <view class="page-section-title">Set whether to keep screen on</view>
  6. <view class="page-section-demo">
  7. <switch checked="{{status}}" onChange="switchKeepScreenOn"/>
  8. </view>
  9. </view>
  10. <view class="page-section">
  11. <view class="page-section-title">Set screen brightness</view>
  12. <view class="page-section-demo">
  13. <slider value="{{brightness}}" max="1" min="0" onChange="sliderChange" step="0.02"/>
  14. </view>
  15. </view>
  16. <view class="page-section">
  17. <view class="page-section-title">Get screen brightness</view>
  18. <view class="page-section-demo">
  19. <button type="primary" onTap="getBrightness">Get screen brightness</button>
  20. </view>
  21. </view>
  22. </view>
 
  1. // API-DEMO page/API/screen/screen.js
  2. Page({
  3. data: {
  4. status: false,
  5. brightness: 1,
  6. },
  7. onLoad() {
  8. my.getScreenBrightness({
  9. success: res => {
  10. this.setData({
  11. brightness: res.brightness
  12. })
  13. },
  14. })
  15. },
  16. sliderChange(e) {
  17. my.setScreenBrightness({
  18. brightness: e.detail.value,
  19. success: (res) => {
  20. this.setData({
  21. brightness: e.detail.value,
  22. })
  23. }
  24. })
  25. },
  26. switchKeepScreenOn(e) {
  27. my.setKeepScreenOn({
  28. keepScreenOn: e.detail.value,
  29. success: (res) => {
  30. this.setData({
  31. status: e.detail.value,
  32. })
  33. }
  34. })
  35. },
  36. getBrightness() {
  37. my.getScreenBrightness({
  38. success: res => {
  39. my.alert({
  40. content: `Current screen brightness: ${res.brightness}`
  41. });
  42. }
  43. })
  44. }
  45. });

my.setScreenBrightness(OBJECT)

Note:
  • This interface is supported since basic library version 1.4.0. Compatibility processing is required for earlier versions. See Mini Program base library to learn more.
  • This interface is only supported in mPaaS 10.1.32 and later versions.

This interface is used to set the screen brightness.

Parameters

Parameter Type Required Description
brightness Number Yes The screen brightness to be set, ranging from 0-1
success Function No Callback function upon call success.
fail Function No Callback function upon call failure.
complete Function No Callback function upon call completion (to be executed upon either call success or failure).

Sample code

 
  1. <!-- API-DEMO page/API/screen/screen.axml-->
  2. <view class="page">
  3. <view class="page-description">Screen brightness API</view>
  4. <view class="page-section">
  5. <view class="page-section-title">Set whether to keep screen on</view>
  6. <view class="page-section-demo">
  7. <switch checked="{{status}}" onChange="switchKeepScreenOn"/>
  8. </view>
  9. </view>
  10. <view class="page-section">
  11. <view class="page-section-title">Set screen brightness</view>
  12. <view class="page-section-demo">
  13. <slider value="{{brightness}}" max="1" min="0" onChange="sliderChange" step="0.02"/>
  14. </view>
  15. </view>
  16. <view class="page-section">
  17. <view class="page-section-title">Get screen brightness</view>
  18. <view class="page-section-demo">
  19. <button type="primary" onTap="getBrightness">Get screen brightness</button>
  20. </view>
  21. </view>
  22. </view>
 
  1. // API-DEMO page/API/screen/screen.js
  2. Page({
  3. data: {
  4. status: false,
  5. brightness: 1,
  6. },
  7. onLoad() {
  8. my.getScreenBrightness({
  9. success: res => {
  10. this.setData({
  11. brightness: res.brightness
  12. })
  13. },
  14. })
  15. },
  16. sliderChange(e) {
  17. my.setScreenBrightness({
  18. brightness: e.detail.value,
  19. success: (res) => {
  20. this.setData({
  21. brightness: e.detail.value,
  22. })
  23. }
  24. })
  25. },
  26. switchKeepScreenOn(e) {
  27. my.setKeepScreenOn({
  28. keepScreenOn: e.detail.value,
  29. success: (res) => {
  30. this.setData({
  31. status: e.detail.value,
  32. })
  33. }
  34. })
  35. },
  36. getBrightness() {
  37. my.getScreenBrightness({
  38. success: res => {
  39. my.alert({
  40. content: `Current screen brightness: ${res.brightness}`
  41. });
  42. }
  43. })
  44. }
  45. });