Android屏幕亮度

Android屏幕亮度

本篇文章主要介绍下android 屏幕亮度相关的内容.

1: 申请权限

修改屏幕亮度需要申请WRITE_SETTINGS权限

xml 复制代码
<uses-permission android:name="android.permission.WRITE_SETTINGS"
    tools:ignore="ProtectedPermissions" />

WRITE_SETTINGS权限无法通过动态申请的方式来申请.

比如demo中我们单独只申请android.permission.WRITE_SETTINGS权限.

打开demo的应用信息,可以看到应用权限中提示的是未申请任何权限

那如何来申请权限呢?

我们可以通过跳转相关页面进行手动设置:

java 复制代码
startActivity(new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS))

2: 判断权限是否申请.

上面我们只是跳转了权限设置的页面,那么如何判断是否已经设置成功呢?

这里用到系统的方法:

java 复制代码
Settings.System.canWrite(this);

我们添加下判断方法:

java 复制代码
@RequiresApi(api = Build.VERSION_CODES.M)
private boolean isCanWrite(){
   return Settings.System.canWrite(this);
}

3:判断是否自动亮度

接着我们关注下屏幕亮度下的自动亮度功能.

首先我们可以根据屏幕亮度mode这个字段来判断:

java 复制代码
/**
 * Control whether to enable automatic brightness mode.
 */
public static final String SCREEN_BRIGHTNESS_MODE = "screen_brightness_mode";

mode有:

java 复制代码
/**
 * 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,则表示开启了自动亮度调节,具体的代码如下:

java 复制代码
/**
 * 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. 开启自动亮度

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

    java 复制代码
    private void closeAutoScreenBrightness(){
        Settings.System.putInt(getContentResolver(),Settings.System.SCREEN_BRIGHTNESS_MODE,Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
    }

5: 设置屏幕亮度

设置屏幕亮度只需要修改Settings.System.SCREEN_BRIGHTNESS即可.

代码如下:

java 复制代码
private void setScreenBrightness(int brightness) {
    Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, brightness);
}

同样我们可以根据Settings.System.SCREEN_BRIGHTNESS,获取当前的屏幕亮度值(0~255).

java 复制代码
/**
 * 获取当前屏幕亮度
 *
 * @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 复制代码
<?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代码:

java 复制代码
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命令操作

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

    adb shell settings get system screen_brightness_mode

  2. 查询当前屏幕亮度

    adb shell settings get system screen_brightness

相关推荐
Kapaseker36 分钟前
一杯美式搞懂 Any、Unit、Nothing
android·kotlin
黄林晴39 分钟前
你的 Android App 还没接 AI?Gemini API 接入全攻略
android
恋猫de小郭11 小时前
2026 Flutter VS React Native ,同时在 AI 时代 VS Native 开发,你没见过的版本
android·前端·flutter
冬奇Lab12 小时前
PowerManagerService(上):电源状态与WakeLock管理
android·源码阅读
BoomHe17 小时前
Now in Android 架构模式全面分析
android·android jetpack
二流小码农1 天前
鸿蒙开发:上传一张参考图片便可实现页面功能
android·ios·harmonyos
鹏程十八少1 天前
4.Android 30分钟手写一个简单版shadow, 从零理解shadow插件化零反射插件化原理
android·前端·面试
Kapaseker1 天前
一杯美式搞定 Kotlin 空安全
android·kotlin
三少爷的鞋1 天前
Android 协程时代,Handler 应该退休了吗?
android
火柴就是我2 天前
让我们实现一个更好看的内部阴影按钮
android·flutter