设置屏幕亮度,并且监听屏幕亮度变化

公司有个需求,弹出对话框,设置当前屏幕亮度是最大亮度的90%,对话框消失,恢复到原来亮度。有一种特殊情况,就是在弹出对话框的界面打开之后,用户手动调节了亮度,需要在界面中监听亮度变化
工具类代码:

public class ScreenUtil
{
  /**
     * 判断是否开启了自动亮度调节
     */
    public static boolean isAutoBrightness(Context context)
    {
        ContentResolver resolver = context.getContentResolver();
        boolean automicBrightness = false;

        try
        {
            automicBrightness = Settings.System.getInt(resolver, Settings.System.SCREEN_BRIGHTNESS_MODE) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;

        }
        catch (Settings.SettingNotFoundException e)
        {
            e.printStackTrace();
        }

        return automicBrightness;
    }

    /**
     * 获取屏幕的亮度
     *
     * @param activity
     * @return
     */
    public static int getScreenBrightness(Activity activity) {
        int nowBrightnessValue = 0;
        ContentResolver resolver = activity.getContentResolver();
        try {
            nowBrightnessValue = Settings.System.getInt(
                    resolver, Settings.System.SCREEN_BRIGHTNESS);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return nowBrightnessValue;
    }

    /**
     * 设置亮度
     *
     * @param activity
     * @param brightness
     */
    public static void setBrightness(Activity activity, int brightness) {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();

        lp.screenBrightness = Float.valueOf(brightness) * (1f / 255f);
 
        activity.getWindow().setAttributes(lp);
    }

    /**
     * 停止自动亮度调节
     *
     * @param activity
     */
    public static void stopAutoBrightness(Activity activity) {

        Settings.System.putInt(activity.getContentResolver(),

                Settings.System.SCREEN_BRIGHTNESS_MODE,

                Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
    }


    /**
     * 开启亮度自动调节
     *
     * @param activity
     */
    public static void startAutoBrightness(Activity activity) {

        Settings.System.putInt(activity.getContentResolver(),

                Settings.System.SCREEN_BRIGHTNESS_MODE,

                Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
    }

    /**
     * 保存亮度设置状态
     *  不保存,只能在当前的activity中有作用
     * @param resolver
     * @param brightness
     */
    public static void saveBrightness(ContentResolver resolver, int brightness) {
        Uri uri = Settings.System
                .getUriFor("screen_brightness");

        Settings.System.putInt(resolver, "screen_brightness",
                brightness);
        // resolver.registerContentObserver(uri, true, myContentObserver);
        resolver.notifyChange(uri, null);
    }


}

对话框中相应的代码

 private int currentBrightness=0;//获得当前屏幕亮度
    private boolean isAutoBrightness=false;//记录屏幕是否开启自动调节
    .......
//对话框显示的时候,设置亮度为最大亮度的90%
  setBrightness90();
  this.setOnDismissListener(new OnDismissListener()
        {
            @Override
            public void onDismiss()
            {
                //对话框消失的时候,还原亮度
                setDefaultBrightness();
            }
        });
......
 /**
     * 设置屏幕亮度为最大亮度的90%
     */
    private void setBrightness90(){
        currentBrightness = ScreenUtil.getScreenBrightness(mContext);
        isAutoBrightness = ScreenUtil.isAutoBrightness(mContext);
        if(isAutoBrightness){
            ScreenUtil.stopAutoBrightness(mContext);
        }
        String model = Build.MODEL;
        int brightness =0;
        if ("ALP-AL00".equals(model)){
            //如果是华为Mate 10 就设置最大亮度是165
            brightness=165;
        }else {
            //其余大部分手机最大亮度是255
            brightness=255;
        }

        ScreenUtil.setBrightness(mContext, (int) (brightness*0.9));
    }

    /**
     * 还原屏幕亮度
     */
    private void setDefaultBrightness(){

        if(isAutoBrightness){
            ScreenUtil.startAutoBrightness(mContext);
        }

        ScreenUtil.setBrightness(mContext, currentBrightness);
    }

    public void setCurrentBrightnessAndAutoBrightness(int currentBrightness,boolean autoBrightness)
    {
        this.currentBrightness = currentBrightness;
        isAutoBrightness = autoBrightness;
    }

弹出对话框的界面

 /**
     * 监听屏幕亮度变化
     */
    private ContentObserver mBrightnessObserver = new ContentObserver(new Handler()) {
        @Override
        public void onChange(boolean selfChange) {
            if(!qrCodeWindow.isShowing()){
                //不显示对话框,根据系统设置调节屏幕亮度
                ScreenUtil.setBrightness(IDCardOneCodeActivity.this,ScreenUtil.getScreenBrightness(IDCardOneCodeActivity.this));
            }else{
                //显示对话框,不根据系统调节变化亮度,先设置当前亮度和是否自动调节亮度配置,等对话框消失以后,设置亮度为调整后的亮度
                qrCodeWindow.setCurrentBrightnessAndAutoBrightness(ScreenUtil.getScreenBrightness(IDCardOneCodeActivity.this),
                        ScreenUtil.isAutoBrightness(IDCardOneCodeActivity.this));
            }
        }
    };

    @Override
    protected void onResume()
    {
        super.onResume();
        getContentResolver().registerContentObserver(
                Settings.System.getUriFor(Settings.System.SCREEN_BRIGHTNESS), true,
                mBrightnessObserver);
    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        getContentResolver().unregisterContentObserver(
                mBrightnessObserver);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值