Android屏幕亮度
本篇文章主要介绍下android 屏幕亮度相关的内容.
1: 申请权限
修改屏幕亮度需要申请WRITE_SETTINGS权限
<uses-permission android:name="android.permission.WRITE_SETTINGS"
tools:ignore="ProtectedPermissions" />
WRITE_SETTINGS权限无法通过动态申请的方式来申请.
比如demo中我们单独只申请android.permission.WRITE_SETTINGS权限.
打开demo的应用信息,可以看到应用权限中提示的是未申请任何权限
那如何来申请权限呢?
我们可以通过跳转相关页面进行手动设置:
startActivity(new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS))
2: 判断权限是否申请.
上面我们只是跳转了权限设置的页面,那么如何判断是否已经设置成功呢?
这里用到系统的方法:
Settings.System.canWrite(this);
我们添加下判断方法:
@RequiresApi(api = Build.VERSION_CODES.M)
private boolean isCanWrite(){
return Settings.System.canWrite(this);
}
3:判断是否自动亮度
接着我们关注下屏幕亮度下的自动亮度功能.
首先我们可以根据屏幕亮度mode这个字段来判断:
/**
* Control whether to enable automatic brightness mode.
*/
public static final String SCREEN_BRIGHTNESS_MODE = "screen_brightness_mode";
mode有:
/**
* SCREEN_BRIGHTNESS_MODE value for manual mode.
*/
public static final int SCREEN_BRIGHTNESS_MODE_MANUAL = 0;
/**
* SCREEN_BRIGHTNESS_MODE value for automatic mode.
*/
public static final int SCREEN_BRIGHTNESS_MODE_AUTOMATIC = 1;
通过调用Settings.System.getInt()方法获取屏幕亮度模式,如果返回值为Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC,则表示开启了自动亮度调节,具体的代码如下:
/**
* SCREEN_BRIGHTNESS_MODE判断是否设置自动亮度
* @return
*/
private boolean isAutoScreenBrightness() {
try {
int mode = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE);
return mode == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
}
return false;
}
4: 关闭打开自动亮度
关闭打开自动亮度的开关也很简单,就是修改SCREEN_BRIGHTNESS_MODE的值即可,代码如下:
-
开启自动亮度
private void openAutoScreenBrightness(){ Settings.System.putInt(getContentResolver(),Settings.System.SCREEN_BRIGHTNESS_MODE,Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC); }
-
关闭自动亮度
private void closeAutoScreenBrightness(){ Settings.System.putInt(getContentResolver(),Settings.System.SCREEN_BRIGHTNESS_MODE,Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL); }
5: 设置屏幕亮度
设置屏幕亮度只需要修改Settings.System.SCREEN_BRIGHTNESS即可.
代码如下:
private void setScreenBrightness(int brightness) {
Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, brightness);
}
同样我们可以根据Settings.System.SCREEN_BRIGHTNESS,获取当前的屏幕亮度值(0~255).
/**
* 获取当前屏幕亮度
*
* @return
*/
private int getCurrentScreenBrightness() {
try {
int anInt = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
return anInt;
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
}
return 0;
}
6: demo源码如下:
首先是布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="申请权限"
android:id="@+id/btn_permission"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="是否自动亮度"
android:id="@+id/btn_check"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="去除自动亮度"
android:id="@+id/btn_close"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开启自动亮度"
android:id="@+id/btn_open"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:text=""
android:hint="请输入0-255"
android:id="@+id/edit_number"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="设置屏幕亮度"
android:id="@+id/btn_set"
/>
</LinearLayout>
最后是Activity代码:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static final String TAG = "MainActivity";
private Button btnPermission, btnCheck, btnClose, btnOpen, btnSet;
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
btnPermission = findViewById(R.id.btn_permission);
btnCheck = findViewById(R.id.btn_check);
btnClose = findViewById(R.id.btn_close);
btnOpen = findViewById(R.id.btn_open);
btnSet = findViewById(R.id.btn_set);
editText = findViewById(R.id.edit_number);
btnPermission.setOnClickListener(this);
btnCheck.setOnClickListener(this);
btnClose.setOnClickListener(this);
btnOpen.setOnClickListener(this);
btnSet.setOnClickListener(this);
}
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_permission:
if (!isCanWrite())
startActivityForResult(new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS), 1101);
else
Toast.makeText(this, "权限申请成功", Toast.LENGTH_SHORT).show();
break;
case R.id.btn_check:
Toast.makeText(this, isAutoScreenBrightness() ? "自动亮度" : "未设置自动亮度", Toast.LENGTH_SHORT).show();
break;
case R.id.btn_close:
closeAutoScreenBrightness();
break;
case R.id.btn_open:
openAutoScreenBrightness();
break;
case R.id.btn_set:
int currentScreenBrightness = getCurrentScreenBrightness();
Log.i(TAG, "onClick: currentScreenBrightness" + currentScreenBrightness);
String s = editText.getText().toString();
if (TextUtils.isEmpty(s)){
Toast.makeText(this, "请输入0-255的数值", Toast.LENGTH_SHORT).show();
return;
}
int i = Integer.parseInt(s);
if (i < 0 || i > 255) {
Toast.makeText(this, "请输入0-255的数值", Toast.LENGTH_SHORT).show();
return;
}
setScreenBrightness(i);
break;
}
}
@RequiresApi(api = Build.VERSION_CODES.M)
private boolean isCanWrite() {
return Settings.System.canWrite(this);
}
/**
* SCREEN_BRIGHTNESS_MODE判断是否设置自动亮度
*
* @return
*/
private boolean isAutoScreenBrightness() {
try {
int mode = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE);
return mode == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
}
return false;
}
private void openAutoScreenBrightness() {
Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
}
private void closeAutoScreenBrightness() {
Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
}
/**
* 获取当前屏幕亮度
*
* @return
*/
private int getCurrentScreenBrightness() {
try {
int anInt = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
return anInt;
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
}
return 0;
}
private void setScreenBrightness(int brightness) {
Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, brightness);
}
}
7:adb命令操作
-
查询是否设置自动屏幕亮度(0:未开启 1:开启)
adb shell settings get system screen_brightness_mode
-
查询当前屏幕亮度
adb shell settings get system screen_brightness