Android屏幕亮度

news/2024/10/18 8:35:14/

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的值即可,代码如下:

  1. 开启自动亮度

    private void openAutoScreenBrightness(){Settings.System.putInt(getContentResolver(),Settings.System.SCREEN_BRIGHTNESS_MODE,Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
    }
    
  2. 关闭自动亮度

    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"><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="申请权限"android:id="@+id/btn_permission"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="是否自动亮度"android:id="@+id/btn_check"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="去除自动亮度"android:id="@+id/btn_close"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="开启自动亮度"android:id="@+id/btn_open"/><EditTextandroid:layout_width="match_parent"android:layout_height="wrap_content"android:inputType="number"android:text=""android:hint="请输入0-255"android:id="@+id/edit_number"/><Buttonandroid: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;@Overrideprotected 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)@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.btn_permission:if (!isCanWrite())startActivityForResult(new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS), 1101);elseToast.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命令操作

  1. 查询是否设置自动屏幕亮度(0:未开启 1:开启)

    adb shell settings get system screen_brightness_mode

  2. 查询当前屏幕亮度

    adb shell settings get system screen_brightness


http://www.ppmy.cn/news/1431021.html

相关文章

蓝桥杯第17169题——兽之泪II

问题描述 在蓝桥王国&#xff0c;流传着一个古老的传说&#xff1a;在怪兽谷&#xff0c;有一笔由神圣骑士留下的宝藏。 小蓝是一位年轻而勇敢的冒险家&#xff0c;他决定去寻找宝藏。根据远古卷轴的提示&#xff0c;如果要找到宝藏&#xff0c;那么需要集齐 n 滴兽之泪&#…

oracle_申明与赋值

1.格式 --1.程序块结构 declare --申明部分 begin --执行部分 end&#xff1b; 2.写一个空的程序块 --1.程序块结构 declare --申明部分 begin --执行部分 null&#xff1b; end&#xff1b; 在控制台输出【hello world】 --2.简单的程序输入 DECLARE --申明部分 BEGIN --…

OpenCV-基于阴影勾勒的图纸清晰度增强算法

作者&#xff1a;翟天保Steven 版权声明&#xff1a;著作权归作者所有&#xff0c;商业转载请联系作者获得授权&#xff0c;非商业转载请注明出处 实现原理 大家在工作和学习中&#xff0c;无论是写报告还是论文&#xff0c;经常有截图的需求&#xff0c;比如图表、图纸等&…

Ruby中Rack中间件的作用是什么?如何应用?

在 Ruby 中&#xff0c;Rack 是一个 Web 服务器接口&#xff0c;它允许开发者使用统一的方式构建 Web 应用程序。Rack 中间件是 Rack 框架的一个核心概念&#xff0c;它可以在请求被传递给应用程序之前或之后对请求和响应进行处理。 Rack 中间件的作用包括但不限于&#xff1a…

【氮化镓】GaN HEMT SEEs效应影响因素和机制

研究背景&#xff1a;AlGaN/GaN HEMT因其在高电压、高温和高频率下的操作能力而受到关注&#xff0c;尤其在航空航天和汽车应用中&#xff0c;其辐射响应变得尤为重要。重离子辐射可能导致绝缘体失效&#xff0c;即单事件效应&#xff08;SEEs&#xff09;引起的栅介质击穿。 …

【入门篇】本章包括创建云项目、数据库的使用、云存储管理、云函数的基本使用、实战举例(小程序之云函数开发入门到使用发布上线实操)

云函数 云函数相当于服务器接口的概念,它并属于小程序端代码。它是以函数的形式运行后端代码来响应事件以及调用其他服务。运行环境是Node.js。 一、基创建云函数项目 打开微信开发者工具: 打开微信开发者工具,并登录你的微信开发者账号。 创建项目: 如果还没有创建项目,你…

npm 打包后自动压缩成zip文件

在package.json里面的scripts下面的build添加 powershell -NoProfile -ExecutionPolicy Unrestricted -Command ./zip.ps1 新的build就是 "build": "vite build && esno ./build/script/postBuild.ts && powershell -NoProfile -ExecutionP…

【Java基础】23.接口

文章目录 一、接口的概念1.接口介绍2.接口与类相似点3.接口与类的区别4.接口特性5.抽象类和接口的区别 二、接口的声明三、接口的实现四、接口的继承五、接口的多继承六、标记接口 一、接口的概念 1.接口介绍 接口&#xff08;英文&#xff1a;Interface&#xff09;&#xf…