許多app會有需要顯示條碼的時候,在打開條碼時,通常會螢幕變亮使條碼的感應能夠靈敏。以下是食用方法:
fun getScreenBrightness(context: Context): Int {
var nowBrightnessValue = 0
val resolver: ContentResolver = context.getContentResolver()
try {
//亮度值範圍為0~255
nowBrightnessValue = Settings.System.getInt(
resolver,
Settings.System.SCREEN_BRIGHTNESS
)
} catch (e: Exception) {
e.printStackTrace()
}
return nowBrightnessValue
}
fun setScreenBrightnessLight(activity:Activity){
val lp = activity!!.window.attributes
//screenBrightness的值範圍為0~1 單位為float
lp.screenBrightness = java.lang.Float.valueOf(0.8f)
activity!!.window.attributes = lp
}
fun setScreenBrightnessOrigin(activity: Activity,context: Context){
val lp = activity!!.window.attributes
//記得將單位0~255(Int)轉換為0-1f
lp.screenBrightness = java.lang.Float.valueOf(getScreenBrightness(context!!) * (1f / 255f))
activity!!.window.attributes = lp
}