android调节系统亮度的实现方式

由于修改系统 Settings 中的屏幕亮度,由于会影响到所有APP,需要申请修改Settings的权限,同时需要取消光感自动调节屏幕亮度的功能,设置为手动调节模式,否则光感传感器会随着光照强度的变化修改系统屏幕亮度,并且非系统签名的APP,需要用户手动授权后才可以修改背光亮度。

以下就是系统亮度的实现方式:
1、效果图


2、布局文件

       

        

3、工具类

/**
 * 屏幕亮度工具类
 */
public class BrightnessUtil {

    /**
     *获取系统屏幕亮度
     */
    public static int getBrightness(Context context) {
        int brightness = -1;
        ContentResolver resolver = context.getContentResolver();
        try {
            brightness = Settings.System.getInt(resolver, Settings.System.SCREEN_BRIGHTNESS);
        } catch (Settings.SettingNotFoundException e) {
            e.printStackTrace();
        }
        Log.e("ccm=======>","getBrightness: "+brightness);
        return brightness;
    }

    /**
     *获取系统最大屏幕亮度
     */
    public static int getMaxBrightness(Context context) {
        int brightnessSettingMaximumId = context.getResources().getIdentifier("config_screenBrightnessSettingMaximum", "integer", "android");
        int brightnessSettingMaximum = context.getResources().getInteger(brightnessSettingMaximumId);
        Log.e("ccm=======>","getMaxBrightness: "+brightnessSettingMaximum);
        return brightnessSettingMaximum;
    }

    /**
     *调节当前屏幕亮度
     */
    public static void SetSystemLight(int lightnumber, Activity activity){
        Window window = activity.getWindow();//对当前窗口进行设置
        WindowManager.LayoutParams layoutparams = window.getAttributes();//获取窗口属性为后面亮度做铺垫作用
        layoutparams.screenBrightness =lightnumber / 255.0f;//用窗口管理(自定义的)layoutparams获取亮度值,android亮度值处于在0-255之间的整形数值
        window.setAttributes(layoutparams);//设置当前窗口屏幕亮度
    }

    /**
     * 5.修改Setting 中屏幕亮度值
     *
     * 修改Setting的值需要动态申请权限 
     * **/
    public static void ModifySettingsScreenBrightness(Context context, int birghtessValue) {
        // 首先需要设置为手动调节屏幕亮度模式
        setScreenManualMode(context);

        ContentResolver contentResolver = context.getContentResolver();
        Log.e("ccm=======>", "birghtessValue: "+birghtessValue);
        Settings.System.putInt(contentResolver,
                Settings.System.SCREEN_BRIGHTNESS, birghtessValue);
    }


    /**
     * 3.关闭光感,设置手动调节背光模式
     *
     * SCREEN_BRIGHTNESS_MODE_AUTOMATIC 自动调节屏幕亮度模式值为1
     *
     * SCREEN_BRIGHTNESS_MODE_MANUAL 手动调节屏幕亮度模式值为0
     * **/
    public static void setScreenManualMode(Context context) {
        ContentResolver contentResolver = context.getContentResolver();
        try {
            int mode = Settings.System.getInt(contentResolver,
                    Settings.System.SCREEN_BRIGHTNESS_MODE);
            if (mode == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) {
                Settings.System.putInt(contentResolver,
                        Settings.System.SCREEN_BRIGHTNESS_MODE,
                        Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
            }
        } catch (Settings.SettingNotFoundException e) {
            e.printStackTrace();
        }
    }

}

4、实现类

 private void initBrightness(){
        if (!Settings.System.canWrite(QuickCircularActivity.this)) {
            Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS, Uri.parse("package:" + getPackageName()));
            startActivityForResult(intent, REQUEST_CODE_WRITE_SETTINGS);
        }
        //将系统最大屏幕亮度值设为seekbar的最大进度值
        mCarBrightness.setMax(BrightnessUtil.getMaxBrightness(QuickCircularActivity.this));
        //将系统当前屏幕亮度值设为seekbar当前进度值
        Log.e("ccm=======>", "setOneProgress: " + BrightnessUtil.getBrightness(QuickCircularActivity.this));
        int brightness = BrightnessUtil.getBrightness(QuickCircularActivity.this);
        mCarBrightness.setProgress(brightness);
        mCarBrightness.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean b) {
                Log.e("ccm=======>", "onProgressChanged: " + progress);
                ModifySettingsScreenBrightness(QuickCircularActivity.this,progress);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

    }

你可能感兴趣的:(android调节系统亮度的实现方式)