Android 一体机研发之修改系统设置————屏幕亮度
Android 一体机研发之修改系统设置————声音
Android 一体机研发之修改系统设置————自动锁屏
前言
最近工作略微有点儿空闲,抽空给大家总结一下:近期一直搞得一体机app研发,适用于医疗渠道。
一体机找工厂定制了一下板子和系统,找他们fw后期app会以apk的形式封装到系统中,当然了开机logo也会封装进去。
在这版一体机,有一个可以更改系统设置的功能版块,记得之前总结过Android 调用系统设置,有兴趣的可以去看一下;这一篇将把部分功能做一个细分化
讲解。
但是在调用系统设置的时候,有些系统功能让用户一看就和手机一样的功能,并且有些功能是我们不需要的,这里就需要我们自己去实现修改系统权限的功能。
接下来,会给大家出几篇一体机研发之修改系统设置系列文章,欢迎我们共同学习进步!
屏幕亮度
修改系统屏幕亮度
这种操作还是挺常见的,一般在多媒体
开发中多少会涉及到。
从上图中虽然很难看出效果展示,但是大概都能猜到功能的具体实现,就是通过seekBar来控制亮度的百分比。
如果想实现修改屏幕亮度功能,这里涉及了一个高级
隐私权限,需要用户在app设置页面手动授权
。
设计思路
-
AndroidManifest设置权限;
-
获取系统屏幕亮度;
-
修改系统Settings 中屏幕亮度(注册监听屏幕亮度变化),影响所有页面和App;
3.1 判断权限
3.2 有则修改屏幕亮度
3.3 无则引导用户授权
具体实现
AndroidManifest设置权限
<!-- 修改系统设置 -->
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
<uses-permission android:name="android.permission.HARDWARE_TEST" />
获取系统屏幕亮度
- Java
/**
* 获取app屏幕亮度
*
* @param activity
* @return
*/
public static int getScreenBrightness(Activity activity) {
try {
return Settings.System.getInt(activity.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
}
return 0;
}
- Kotlin
/**
* 获取系统屏幕亮度(0-255)
*/
private fun getScreenBrightness(): Int {
try {
return Settings.System.getInt(this.contentResolver, Settings.System.SCREEN_BRIGHTNESS)
} catch (e: SettingNotFoundException) {
e.printStackTrace()
}
return 0
}
修改系统Settings 中屏幕亮度,影响所有页面和App
注册监听 系统屏幕亮度变化
- Java
private void registerContentObserver() {
ContentResolver contentResolver = getContentResolver();
if (contentResolver != null) {
contentResolver.registerContentObserver(Settings.System.getUriFor(Settings.System.SCREEN_BRIGHTNESS),
true,
mBrightnessObserver);
}
}
- Kotlin
private fun registerContentObserver() {
this.contentResolver?.registerContentObserver(
Settings.System.getUriFor(Settings.System.SCREEN_BRIGHTNESS),
true,
mBrightnessObserver
)
}
监听系统亮度变化
- Java
private ContentObserver mBrightnessObserver = new ContentObserver(new Handler()) {
public void onChange(boolean selfChange) {
super.onChange(selfChange);
ContentResolver contentResolver = getContentResolver();
if (contentResolver != null) {
mScreenBrightness = getScreenBrightness(BrightActivity.this);
Log.e("监听亮度 >>>", "init: " + mScreenBrightness);
updateNum(mScreenBrightness);
setWindowBrightness(mScreenBrightness);
}
}
};
- Kotlin
private val mBrightnessObserver = object : ContentObserver(Handler(Looper.getMainLooper())) {
override fun onChange(selfChange: Boolean) {
super.onChange(selfChange)
try {
this.contentResolver?.let {
mScreenBrightness = Settings.System.getInt(it, Settings.System.SCREEN_BRIGHTNESS)
updateNum(mScreenBrightness)
setWindowBrightness(mScreenBrightness)
}
} catch (e: SettingNotFoundException) {
e.printStackTrace()
}
}
}
注销监听
- Java
void onDestroy() {
super.onDestroy();
ContentResolver contentResolver = getContentResolver();
if (contentResolver != null) {
contentResolver.unregisterContentObserver((ContentObserver) mBrightnessObserver);
}
}
protected
- Kotlin
override fun onDestroy() {
super.onDestroy()
//注销监听
this.contentResolver?.unregisterContentObserver(mBrightnessObserver)
}
通过seekBar修改系统Settings 中屏幕亮度
- Java
brightBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
(api = Build.VERSION_CODES.M)
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
Log.e("动态屏幕亮度 >>>", "onProgressChanged: " + progress);
mScreenBrightness = progress * ratio;
if (Settings.System.canWrite(BrightActivity.this)) {
setScreenBrightness(mScreenBrightness);
updateNum(mScreenBrightness);
} else {
Toast.makeText(BrightActivity.this, "没有修改权限", Toast.LENGTH_SHORT).show();
// 打开允许修改Setting 权限的界面
Intent intent = new Intent(
Settings.ACTION_MANAGE_WRITE_SETTINGS, Uri
.parse("package:"
+ getPackageName()));
startActivityForResult(intent,
mRequestCode);
}
}
public void onStartTrackingTouch(SeekBar seekBar) {
}
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
- Kotlin
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)) {
setScreenBrightness(mScreenBrightness)
updateNum(mScreenBrightness)
} else {
Toast.makeText(this, "没有修改权限", 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?) {
}
})
用户已授权,设置屏幕亮度
- Java
/**
* 设置系统屏幕亮度,影响所有页面和app
* 注意:这种方式是需要手动权限的(android.permission.WRITE_SETTINGS)
*/
private final void setScreenBrightness(int brightness) {
//先检测调节模式
setScreenManualMode();
//再设置
Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, brightness);
}
private final void setScreenManualMode() {
try {
//获取当前系统亮度调节模式
int mode = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE);
//如果是自动,则改为手动
if (mode == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) {
Settings.System.putInt(getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE,
Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
}
} catch (Settings.SettingNotFoundException var2) {
var2.printStackTrace();
}
}
- Kotlin
/**
* 设置系统屏幕亮度,影响所有页面和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()
}
}
/**
* 设置系统亮度调节模式(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()
}
}
监听系统按键
//监听系统按键
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_DOWN:
if (mScreenBrightness > ratio) {
mScreenBrightness -= ratio;
} else {
mScreenBrightness = 1;
}
Log.e("Down >>>", "onKeyDown: " + mScreenBrightness);
setWindowBrightness(mScreenBrightness);
updateNum(mScreenBrightness);
return true;
case KeyEvent.KEYCODE_VOLUME_UP:
if (mScreenBrightness < (255 - ratio)) {
mScreenBrightness += ratio;
} else {
mScreenBrightness = 255;
}
Log.e("Up >>>", "onKeyDown: " + mScreenBrightness);
setWindowBrightness(mScreenBrightness);
updateNum(mScreenBrightness);
return true;
/* case KeyEvent.KEYCODE_VOLUME_MUTE:
return true;*/
}
return super.onKeyDown(keyCode, event);
}
完整操作
- Java
/**
* @author 拉莫帅
* @date 2023/2/8
* @address
* @Desc 修改系统亮度
*/
public class BrightActivity extends Activity {
/**
* 非系统签名应用,引导用户手动授权修改Settings 权限
**/
private int mRequestCode = 1;
private SeekBar brightBar;
//当前屏幕亮度
private int mScreenBrightness = 0;
//每次加减的比例
private int ratio = 25;
private ContentObserver mBrightnessObserver = new ContentObserver(new Handler()) {
public void onChange(boolean selfChange) {
super.onChange(selfChange);
ContentResolver contentResolver = getContentResolver();
if (contentResolver != null) {
mScreenBrightness = getScreenBrightness(BrightActivity.this);
Log.e("监听亮度 >>>", "init: " + mScreenBrightness);
updateNum(mScreenBrightness);
setWindowBrightness(mScreenBrightness);
}
}
};
(api = Build.VERSION_CODES.M)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bright);
init();
registerContentObserver();
setListener();
}
private void init() {
brightBar = findViewById(R.id.brightBar);
ImageView back = findViewById(R.id.back);
TextView title = findViewById(R.id.title);
title.setText("亮度");
//获取当前亮度
mScreenBrightness = getScreenBrightness(BrightActivity.this);
Log.e("当前亮度 >>>", "init: " + mScreenBrightness);
updateNum(mScreenBrightness);
back.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});
}
private void registerContentObserver() {
ContentResolver contentResolver = getContentResolver();
if (contentResolver != null) {
contentResolver.registerContentObserver(Settings.System.getUriFor(Settings.System.SCREEN_BRIGHTNESS),
true,
mBrightnessObserver);
}
}
protected void onDestroy() {
super.onDestroy();
ContentResolver contentResolver = getContentResolver();
if (contentResolver != null) {
contentResolver.unregisterContentObserver((ContentObserver) mBrightnessObserver);
}
}
private void setListener() {
brightBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
(api = Build.VERSION_CODES.M)
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
Log.e("动态屏幕亮度 >>>", "onProgressChanged: " + progress);
mScreenBrightness = progress * ratio;
if (Settings.System.canWrite(BrightActivity.this)) {
setScreenBrightness(mScreenBrightness);
updateNum(mScreenBrightness);
} else {
Toast.makeText(BrightActivity.this, "没有修改权限", Toast.LENGTH_SHORT).show();
// 打开允许修改Setting 权限的界面
Intent intent = new Intent(
Settings.ACTION_MANAGE_WRITE_SETTINGS, Uri
.parse("package:"
+ getPackageName()));
startActivityForResult(intent,
mRequestCode);
}
}
public void onStartTrackingTouch(SeekBar seekBar) {
}
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
(api = Build.VERSION_CODES.M)
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == mRequestCode) {
if (Settings.System.canWrite((Context) BrightActivity.this)) {
setScreenBrightness(mScreenBrightness);
updateNum(mScreenBrightness);
} else {
Toast.makeText((Context) this, "您已拒绝修系统Setting的屏幕亮度权限", Toast.LENGTH_SHORT).show();
}
}
}
/**
* 获取app屏幕亮度
*
* @param activity
* @return
*/
public static int getScreenBrightness(Activity activity) {
try {
return Settings.System.getInt(activity.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
}
return 0;
}
/**
* 更新页面显示
*
* @param mScreenBrightness
*/
private final void updateNum(int mScreenBrightness) {
//四舍五入
int i = (int) Math.floor(mScreenBrightness / ratio);
brightBar.setProgress(i);
}
/**
* 设置系统屏幕亮度,影响所有页面和app
* 注意:这种方式是需要手动权限的(android.permission.WRITE_SETTINGS)
*/
private final void setScreenBrightness(int brightness) {
//先检测调节模式
setScreenManualMode();
//再设置
Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, brightness);
}
private final void setScreenManualMode() {
try {
//获取当前系统亮度调节模式
int mode = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE);
//如果是自动,则改为手动
if (mode == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) {
Settings.System.putInt(getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE,
Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
}
} catch (Settings.SettingNotFoundException var2) {
var2.printStackTrace();
}
}
/**
* 设置当前应用屏幕亮度,只当前有效
*/
private final void setWindowBrightness(int brightness) {
Window window = this.getWindow();
WindowManager.LayoutParams lp = window.getAttributes();
lp.screenBrightness = (float) brightness / 255.0F;
window.setAttributes(lp);
}
//监听系统按键
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_DOWN:
if (mScreenBrightness > ratio) {
mScreenBrightness -= ratio;
} else {
mScreenBrightness = 1;
}
Log.e("Down >>>", "onKeyDown: " + mScreenBrightness);
setWindowBrightness(mScreenBrightness);
updateNum(mScreenBrightness);
return true;
case KeyEvent.KEYCODE_VOLUME_UP:
if (mScreenBrightness < (255 - ratio)) {
mScreenBrightness += ratio;
} else {
mScreenBrightness = 255;
}
Log.e("Up >>>", "onKeyDown: " + mScreenBrightness);
setWindowBrightness(mScreenBrightness);
updateNum(mScreenBrightness);
return true;
/* case KeyEvent.KEYCODE_VOLUME_MUTE:
return true;*/
}
return super.onKeyDown(keyCode, event);
}
}
- Kotlin
class BrightnessActivity : AppCompatActivity() {
//当前屏幕亮度
private var mScreenBrightness: Int = 0
//每次加减的比例
private var ratio: Int = 25
private var mRequestCode = 1
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_brightness)
init()
registerContentObserver()
setListener()
}
private fun init() {
mScreenBrightness = getScreenBrightness()
updateNum(mScreenBrightness)
}
/**
* 注册监听 系统屏幕亮度变化
*/
private fun registerContentObserver() {
this.contentResolver?.registerContentObserver(
Settings.System.getUriFor(Settings.System.SCREEN_BRIGHTNESS),
true,
mBrightnessObserver
)
}
/**
* 监听系统亮度变化
*/
private val mBrightnessObserver = object : ContentObserver(Handler(Looper.getMainLooper())) {
override fun onChange(selfChange: Boolean) {
super.onChange(selfChange)
try {
this.contentResolver?.let {
mScreenBrightness = Settings.System.getInt(it, Settings.System.SCREEN_BRIGHTNESS)
updateNum(mScreenBrightness)
setWindowBrightness(mScreenBrightness)
}
} catch (e: SettingNotFoundException) {
e.printStackTrace()
}
}
}
override fun onDestroy() {
super.onDestroy()
//注销监听
this.contentResolver?.unregisterContentObserver(mBrightnessObserver)
}
private fun setListener() {
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)) {
setScreenBrightness(mScreenBrightness)
updateNum(mScreenBrightness)
} else {
Toast.makeText(this, "没有修改权限", 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?) {
}
})
}
/**
* 处理返回结果
*/
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == mRequestCode) {
if (Settings.System.canWrite(this)) {
setScreenBrightness(mScreenBrightness)
updateNum(mScreenBrightness)
} else {
Toast.makeText(this, "拒绝了权限", Toast.LENGTH_SHORT).show()
}
}
}
/**
* 更新页面显示
*/
private fun updateNum(mScreenBrightness: Int) {
//转float 取四舍五入
val i: Int = (mScreenBrightness / (ratio.toFloat())).roundToInt()
tv_brightness.text = i.toString()
seekBar.progress = i
}
/**
* 获取系统屏幕亮度(0-255)
*/
private fun getScreenBrightness(): Int {
try {
return Settings.System.getInt(this.contentResolver, Settings.System.SCREEN_BRIGHTNESS)
} catch (e: SettingNotFoundException) {
e.printStackTrace()
}
return 0
}
/**
* 设置系统屏幕亮度,影响所有页面和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()
}
}
/**
* 设置系统亮度调节模式(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()
}
}
/**
* 设置当前应用屏幕亮度,只当前有效
*/
private fun setWindowBrightness(brightness: Int) {
val window = window
val lp = window.attributes
lp.screenBrightness = brightness / 255.0f
window.attributes = lp
}
}
最近忙里偷闲,↓↓↓↓【谁家de码农陈先生】↓↓↓↓,里面定时给大家分享技术博文、前方高能资讯内容!欢迎各位老板点赞关注,你们就是我的动力源泉!