Screen Brightness

icon-note.gif Note:

  • The APIs of this module are no longer maintained since API version 7. It is recommended that you use @ohos.brightness instead.

  • The initial APIs of this module are supported since API version 3. Newly added APIs will be marked with a superscript to indicate their earliest API version.

Modules to Import

import brightness from '@system.brightness';

brightness.getValue

getValue(Object): void

Obtains the current screen brightness.

System capability: SystemCapability.PowerManager.DisplayPowerManager

Parameters

Name Type Mandatory Description
success Function No Called when the execution is successful.
fail Function No Called when the operation fails.
complete Function No Called when the execution is complete

The following values will be returned when the operation is successful.

Name Type Description
value number Screen brightness, which ranges from 1 to 255.

Example

export default {    
  getValue() {        
    brightness.getValue({            
      success: function(data){                
        console.log('success get brightness value:' + data.value);            
      },            
      fail: function(data, code) {                
        console.log('get brightness fail, code: ' + code + ', data: ' + data);            
      },
    });    
  },
}

brightness.setValue

setValue(Object): void

Sets the screen brightness.

System capability: SystemCapability.PowerManager.DisplayPowerManager

Parameters

Name Type Mandatory Description
value number Yes Screen brightness. The value is an integer ranging from 1 to 255.
- If the value is less than or equal to 0, value 1 will be used.
- If the value is greater than 255, value 255 will be used.
- If the value contains decimals, the integral part of the value will be used. For example, if value 8.1 is set, value 8 will be used.
success Function No Called when the execution is successful.
fail Function No Called when the operation fails.
complete Function No Called when the execution is complete.

Example

export default {    
  setValue() {        
    brightness.setValue({            
      value: 100,            
      success: function(){                
        console.log('handling set brightness success.');            
      },            
      fail: function(data, code){                
        console.log('handling set brightness value fail, code:' + code + ', data: ' + data);            
      },        
    });    
  },
}

brightness.getMode

getMode(Object): void

Obtains the screen brightness adjustment mode.

System capability: SystemCapability.PowerManager.DisplayPowerManager

Parameters

Name Type Mandatory Description
success Function No Called when the execution is successful.
fail Function No Called when the operation fails.
complete Function No Called when the execution is complete

The following values will be returned when the operation is successful.

Name Type Description
mode number The value can be 0 or 1.
0: The screen brightness is manually adjusted.
1: The screen brightness is automatically adjusted.

Example

export default {    
  getMode() {        
    brightness.getMode({            
      success: function(data){                
        console.log('success get mode:' + data.mode);            
      },            
      fail: function(data, code){                
        console.log('handling get mode fail, code:' + code + ', data: ' + data);            
      },
    });    
  },
}

brightness.setMode

setMode(Object): void

Sets the screen brightness adjustment mode.

System capability: SystemCapability.PowerManager.DisplayPowerManager

Parameters

Name Type Mandatory Description
mode number Yes The value can be 0 or 1.
0: The screen brightness is manually adjusted.
1: The screen brightness is automatically adjusted.
success Function No Called when the execution is successful.
fail Function No Called when the operation fails.
complete Function No Called when the execution is complete.

Example

export default {    
  setMode() {        
    brightness.setMode({            
      mode: 1,            
      success: function(){                
      console.log('handling set mode success.');            
    },            
     fail: function(data, code){                
       console.log('handling set mode fail, code:' + code + ', data: ' + data);            
     },
    });    
  },
}

brightness.setKeepScreenOn

setKeepScreenOn(Object): void

Sets whether to always keep the screen on. Call this API in onShow().

System capability: SystemCapability.PowerManager.DisplayPowerManager

Parameters

Name Type Mandatory Description
keepScreenOn boolean Yes Whether to always keep the screen on
success Function No Called when the execution is successful.
fail Function No Called when the operation fails.
complete Function No Called when the execution is complete.

Example

export default {    
  setKeepScreenOn() {        
    brightness.setKeepScreenOn({            
      keepScreenOn: true,            
      success: function () {                
        console.log('handling set keep screen on success.')            
      },            
      fail: function (data, code) {                
        console.log('handling set keep screen on fail, code:' + code + ', data: ' + data);            
      },        
    });    
  },
}