赞
踩
即仅在本 APP 处于前台时使用该屏幕亮度,一旦退出或者挂起 APP,屏幕亮度恢复原值
无需申请任何权限,拿来就用
获取当前窗口属性,设置属性的屏幕亮度值,然后在应用一下就好了!
亮度值 brightness
在 0.0-1.0
之间
fun setAppBrightness(window: Window, brightness: Float) {
val attributes = window.attributes
attributes.screenBrightness = brightness
window.attributes = attributes
}
亮度值为浮点数,范围 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
}
一旦设置,即更改手机全局屏幕亮度
先申请权限:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。