在Android Java中设置最小屏幕亮度:
final WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_OFF;
getWindow().setAttributes(lp);要在Android Kotlin中更改屏幕亮度:
val lp = this.window.attributes
lp.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_OFF
this.window.attributes = lp然而(作为Kotlin的初学者),我很惊讶地看到这一行也行得通:
window.attributes.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_OFF为什么这是可行的?这段代码有没有什么不想要的副作用?
发布于 2018-12-07 06:22:05
实际上,这一行代码行不通。您可以在IDE中打开它,以查看它大致转换为
val lp = this.window.attributes
lp.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_OFF
// this.window.attributes = lp — this one isn't called, explanation belowKotlin不会为您执行所有嵌套的set*调用,因此它不会调用this.window.setAttributes()方法。所有对实际窗口应用更改的代码都是由这个调用触发的。
发布于 2019-10-06 20:48:51
正如OleGG告诉我的,我试着用“一行模式”来做这件事,但是没有任何结果。我与你分享在API16(或更高版本)中唯一对我有效的事情,那就是使用SeekBar。
我在SettingsActivity中有一个伴生对象
companion object {
private const val WRITE_SETTINGS_PERMISSION = 100
private const val MAX_BRIGHTNESS = 255
private const val MIN_BRIGHTNESS = 20
}在SettingsActivity的onCreate()中,我调用
Settings.System.putInt(this.contentResolver, Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL)
skb_brightness.max = MAX_BRIGHTNESS
skb_brightness.progress = AppPreferences.getInstance(this).brightnessKey然后我像这样创建我的监听器:
object : SeekBar.OnSeekBarChangeListener {
var brightnessProgress = 0
override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
brightnessProgress = map(progress, 0, MAX_BRIGHTNESS, MIN_BRIGHTNESS, MAX_BRIGHTNESS)
if (hasPermissionsToWriteSettings(this@SettingsActivity)) {
Settings.System.putInt(this@SettingsActivity.contentResolver, Settings.System.SCREEN_BRIGHTNESS, brightnessProgress)
// Do not write this in one-line, won't work
val lp = this@SettingsActivity.window.attributes
lp.screenBrightness = brightnessProgress.toFloat() / 255
this@SettingsActivity.window.attributes = lp
}
}
override fun onStartTrackingTouch(seekBar: SeekBar?) { }
override fun onStopTrackingTouch(seekBar: SeekBar?) {
AppPreferences.getInstance(this@SettingsActivity).brightnessKey = brightnessProgress
}
}其中,hasPermissionsToWriteSettings(this@SettingsActivity)是一个检查我是否有权限执行此操作的方法。
private fun hasPermissionsToWriteSettings(context: Activity): Boolean {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Settings.System.canWrite(context)
} else {
ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_SETTINGS) == PackageManager.PERMISSION_GRANTED
}
}map函数用于映射最小值:
private fun map(currentValue: Int, inputMin: Int, inputMax: Int, outputMin: Int, outputMax: Int): Int {
return (currentValue - inputMin) * (outputMax - outputMin) / (inputMax - inputMin) + outputMin
}AppPreferences是我的助手类,用于存储键值共享首选项中的亮度值。它就是这样做的(BRIGHTNESS_CONTROL_KEY = "brightnessControlValue", DEFAULT_BRIGHTNESS = 100)
var brightnessKey: Int
get() = sharedPreferences.getInt(BRIGHTNESS_CONTROL_KEY, DEFAULT_BRIGHTNESS)
set(value) = sharedPreferences.edit().putInt(BRIGHTNESS_CONTROL_KEY, value).apply()我还在manifest.xml中添加了
<uses-permission android:name="android.permission.WRITE_SETTINGS" />...event如果Android Studio告诉我,那就错了。没有这个,我就不能改变亮度。
发布于 2022-01-05 11:39:05
在Kotlin中,更新活动中单个参数的最短方法如下所示:
window.attributes = window.attributes.apply {
screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_OFF
}这是因为您需要从android.view.getAttributes()获取当前的LayoutParams,更新screenBrightness值,然后使用更新后的LayoutParams调用android.view.setAttributes()。
https://stackoverflow.com/questions/52569632
复制相似问题