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

相关推荐
- 羊羊不超越 -33 分钟前
App渠道来源追踪方案全面分析(iOS/Android/鸿蒙)
android·ios·harmonyos
wk灬丨1 小时前
Android Kotlin Flow 冷流 热流
android·kotlin·flow
千雅爸爸1 小时前
Android MVVM demo(使用DataBinding,LiveData,Fresco,RecyclerView,Room,ViewModel 完成)
android
晨曦_子画2 小时前
编程语言之战:AI 之后的 Kotlin 与 Java
android·java·开发语言·人工智能·kotlin
孤客网络科技工作室2 小时前
AJAX 全面教程:从基础到高级
android·ajax·okhttp
Mr Lee_3 小时前
android 配置鼠标右键快捷对apk进行反编译
android
顾北川_野4 小时前
Android CALL关于电话音频和紧急电话设置和获取
android·音视频
&岁月不待人&4 小时前
Kotlin by lazy和lateinit的使用及区别
android·开发语言·kotlin
Winston Wood6 小时前
Android Parcelable和Serializable的区别与联系
android·序列化