effect
屏幕亮度
The operation of modifying the system is quite common, and it is generally 多媒体
involved in development.
The emmm renderings do not seem to change. . But it is not very important. .
Operation dismantling
As you can see in the picture above, there are 加减按钮
and respectively seekbar
to control the brightness.
Related events will be involved later.
Get system screen brightness
/**
* 获取系统屏幕亮度(0-255)
*/
private fun getScreenBrightness(): Int {
try {
return Settings.System.getInt(this.contentResolver, Settings.System.SCREEN_BRIGHTNESS)
} catch (e: SettingNotFoundException) {
e.printStackTrace()
}
return 0
}
Note that the return value here is 0-255
interval.
Define two parameters:
- private var mScreenBrightness: Int = 0 //Current screen brightness
- private var ratio: Int = 25 //The ratio of each addition and subtraction
Because the maximum return value is 255, assuming that the brightness adjustment is 10 gears, each increase or decrease of 1 gear is about 25. This 精度
can be controlled by yourself.
Set the current application screen brightness, only currently valid
Plus and minus button operation
btn_add.setOnClickListener {
if (mScreenBrightness < (255 - ratio)) {
mScreenBrightness += ratio
} else {
mScreenBrightness = 255
}
setWindowBrightness(mScreenBrightness)
updateNum(mScreenBrightness)
}
btn_reduce.setOnClickListener {
if (mScreenBrightness > ratio) {
mScreenBrightness -= ratio
} else {
mScreenBrightness = 1
}
setWindowBrightness(mScreenBrightness)
updateNum(mScreenBrightness)
}
If the brightness value is greater than 255, no error will be reported, but it will return 初始值
, so when adding and subtracting operations, judge the maximum and minimum values.
Next look at the core method setWindowBrightness
:
/**
* 设置当前应用屏幕亮度,只当前有效
*/
private fun setWindowBrightness(brightness: Int) {
val window = window
val lp = window.attributes
lp.screenBrightness = brightness / 255.0f
window.attributes = lp
}
Very simple, just set window
the properties.
This is only valid for the current page. When you return to the page or exit to the background, the screen brightness will 恢复
reach the initial value.
updateNum
The method is to update the page display:
/**
* 更新页面显示
*/
private fun updateNum(mScreenBrightness: Int) {
//转float 取四舍五入
val i: Int = (mScreenBrightness / (ratio.toFloat())).roundToInt()
tv_brightness.text = i.toString()
seekBar.progress = i
}
In fact, most of the needs can be met here.
Set system screen brightness, affect all pages and apps
What I mentioned earlier is actually the brightness setting of a single page. You can also modify the screen brightness of the system, which affects all pages and apps. Generally, this operation is not performed.
This also involves a 高级
privacy permission, whether it is 允许修改系统设置
, and needs to be in the app settings page 手动授权
.
And you need to manifest
add it first :
<!-- 修改系统屏幕亮度 -->
<uses-permission
android:name="android.permission.WRITE_SETTINGS"
tools:ignore="ProtectedPermissions" />
Here are a few small steps:
- Judgment authority
- If yes, modify the brightness
- No guide authorization
seekBar operation
seekBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
Log.i("onProgressChanged----", "" + progress)
mScreenBrightness = progress * ratio
//判断是否有修改系统设置权限
if (Settings.System.canWrite(this@BrightnessActivity)) {
setScreenBrightness(mScreenBrightness)
updateNum(mScreenBrightness)
} else {
Toast.makeText(this@BrightnessActivity, "没有修改权限", Toast.LENGTH_SHORT).show()
// 打开允许修改系统设置权限的页面
val intent = Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS, Uri.parse("package:$packageName"))
startActivityForResult(intent, mRequestCode)
}
}
override fun onStartTrackingTouch(seekBar: SeekBar?) {
}
override fun onStopTrackingTouch(seekBar: SeekBar?) {
}
})
Use Settings.System.canWrite
to judge whether or not authorized.
Authorized
How to see setScreenBrightness
:
/**
* 设置系统屏幕亮度,影响所有页面和app
* 注意:这种方式是需要手动权限的(android.permission.WRITE_SETTINGS)
*/
private fun setScreenBrightness(brightness: Int) {
try {
//先检测调节模式
setScreenManualMode()
//再设置
Settings.System.putInt(this.contentResolver, Settings.System.SCREEN_BRIGHTNESS, brightness)
} catch (e: SettingNotFoundException) {
e.printStackTrace()
}
}
We see that before setting, there is one more operation first 检测调节模式
, because if the current brightness is adjusted automatically, it needs to be changed to manual.
/**
* 设置系统亮度调节模式(SCREEN_BRIGHTNESS_MODE)
* SCREEN_BRIGHTNESS_MODE_MANUAL 手动调节
* SCREEN_BRIGHTNESS_MODE_AUTOMATIC 自动调节
*/
private fun setScreenManualMode() {
try {
//获取当前系统亮度调节模式
val mode = Settings.System.getInt(this.contentResolver, Settings.System.SCREEN_BRIGHTNESS_MODE)
//如果是自动,则改为手动
if (mode == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) {
Settings.System.putInt(
this.contentResolver,
Settings.System.SCREEN_BRIGHTNESS_MODE,
Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL
)
}
} catch (e: SettingNotFoundException) {
e.printStackTrace()
}
}
Brightness adjustment mode
- SCREEN_BRIGHTNESS_MODE_MANUAL manual adjustment
- SCREEN_BRIGHTNESS_MODE_AUTOMATIC automatic adjustment
unauthorized
In case of unauthorized, prompt and 引导
user to authorize
Toast.makeText(this@BrightnessActivity, "没有修改权限", Toast.LENGTH_SHORT).show()
// 打开允许修改系统设置权限的页面
val intent = Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS, Uri.parse("package:$packageName"))
startActivityForResult(intent, mRequestCode)
At the same time, check the return result and process it
/**
* 处理返回结果
*/
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == mRequestCode) {
if (Settings.System.canWrite(this@BrightnessActivity)) {
setScreenBrightness(mScreenBrightness)
} else {
Toast.makeText(this@BrightnessActivity, "拒绝了权限", Toast.LENGTH_SHORT).show()
}
}
}
As you can see from the above, whether it is to change the mode or the brightness, it is the same Settings.System.putInt
method, that is, the system settings are modified to achieve the requirement of using the same brightness for all pages and apps.
Monitor system brightness changes
The above two methods are actually changed manually. If the user changes the brightness by himself, our page should also be changed accordingly. Therefore, we need to monitor the brightness change of the system.
There are also several small steps:
- Register to listen
- Deal with changes
- Logout monitor
Register to listen
/**
* 注册监听 系统屏幕亮度变化
*/
private fun registerContentObserver() {
this.contentResolver?.registerContentObserver(
Settings.System.getUriFor(Settings.System.SCREEN_BRIGHTNESS),
true,
mBrightnessObserver
)
}
Deal with changes
/**
* 监听系统亮度变化
*/
private val mBrightnessObserver = object : ContentObserver(Handler(Looper.getMainLooper())) {
override fun onChange(selfChange: Boolean) {
super.onChange(selfChange)
try {
this@BrightnessActivity.contentResolver?.let {
mScreenBrightness = Settings.System.getInt(it, Settings.System.SCREEN_BRIGHTNESS)
updateNum(mScreenBrightness)
setWindowBrightness(mScreenBrightness)
}
} catch (e: SettingNotFoundException) {
e.printStackTrace()
}
}
}
Logout monitor
override fun onDestroy() {
super.onDestroy()
//注销监听
this.contentResolver?.unregisterContentObserver(mBrightnessObserver)
}
ok, so far 修改屏幕亮度
the explanation about it is all over, if it is useful to you, just like it ^-^