Android屏幕亮度调节

本 APP 亮度设置

即仅在本 APP 处于前台时使用该屏幕亮度,一旦退出或者挂起 APP,屏幕亮度恢复原值

无需申请任何权限,拿来就用

获取当前窗口属性,设置属性的屏幕亮度值,然后在应用一下就好了!

亮度值 brightness0.0-1.0 之间

fun setAppBrightness(window: Window, brightness: Float) {
    val attributes = window.attributes
    attributes.screenBrightness = brightness
    window.attributes = attributes
}

屏幕亮度相关 API

获取当前系统屏幕亮度

亮度值为浮点数,范围 0.0f-255.0f

fun getCurrentBrightness(context: Context): Float {
    var resolver = context.contentResolver
    var brightness = 0.0f
    try {
        brightness = Settings.System.getFloat(resolver, Settings.System.SCREEN_BRIGHTNESS)
    } catch (e: SettingNotFoundException) {
        e.printStackTrace()
    }
    return brightness
}

获取亮度模式

分别为:自动调节(数值 1),手动调节(数值 0)

fun getBrightnessMode(context: Context): Int {
    var mode: Int = -1
    try {
        mode = Settings.System.getInt(
            context.contentResolver,
            Settings.System.SCREEN_BRIGHTNESS_MODE,
            -1
        )
    } catch (e: SettingNotFoundException) {
        e.printStackTrace()
    }
    return mode
}

手机屏幕亮度设置

一旦设置,即更改手机全局屏幕亮度

先申请权限:

你可能感兴趣的:(Android,android,kotlin)