• android brightness control


    package com.smarttpapers.reader.dialog;
    
    import android.app.Activity;
    import android.app.Dialog;
    import android.content.ContentResolver;
    import android.content.Context;
    import android.net.Uri;
    import android.provider.Settings;
    import android.provider.Settings.SettingNotFoundException;
    import android.view.WindowManager;
    import android.widget.SeekBar;
    import android.widget.SeekBar.OnSeekBarChangeListener;
    
    import com.smarttpapers.reader.R;
    
    public class DialogBrightness extends Dialog{
    
        private int brightnessValue;
        private boolean isAuto;
        public DialogBrightness(final Context context, int theme) {
            super(context, theme);
            setContentView(R.layout.popup_brightness);
            setTitle("Brightness");
            setCanceledOnTouchOutside(true);
            final SeekBar brightness = (SeekBar)findViewById(R.id.page_bightness);
            int nowBrightnessValue = 0;
            final ContentResolver resolver = ((Activity)context).getContentResolver();
            try {
                nowBrightnessValue = android.provider.Settings.System.getInt(
                        resolver, Settings.System.SCREEN_BRIGHTNESS);
            } catch (Exception e) {
                e.printStackTrace();
            }
            brightness.setProgress(nowBrightnessValue);
            isAuto=isAutoBrightness(resolver);
            if(isAuto){
                stopAutoBrightness((Activity)context);
            }
            brightness.setOnSeekBarChangeListener(
                    new OnSeekBarChangeListener() {
                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {
                    saveBrightness(resolver, brightnessValue);
                }
                
                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {}
                
                @Override
                public void onProgressChanged(SeekBar seekBar, int progress,
                        boolean fromUser) {
                    brightnessValue=progress+10;
                    setBrightness((Activity)context, brightnessValue);
                }
            });
        }
        public boolean isAutoBrightness(ContentResolver aContentResolver) {
            boolean automicBrightness = false;
            try {
                automicBrightness = Settings.System.getInt(aContentResolver,
                        Settings.System.SCREEN_BRIGHTNESS_MODE) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
            } catch (SettingNotFoundException e) {
                e.printStackTrace();
            }
            return automicBrightness;
        }
         public void setBrightness(Activity activity, int bright) {
                WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
                lp.screenBrightness = Float.valueOf(bright) * (1f / 255f);
                activity.getWindow().setAttributes(lp);
            }
        public void startAutoBrightness(Activity activity) {
            Settings.System.putInt(activity.getContentResolver(),
                    Settings.System.SCREEN_BRIGHTNESS_MODE,
                    Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
        }
        public void stopAutoBrightness(Activity activity) {
            Settings.System.putInt(activity.getContentResolver(),
                    Settings.System.SCREEN_BRIGHTNESS_MODE,
                    Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
        }
        public void saveBrightness(ContentResolver resolver, int bright) {
            Uri uri = android.provider.Settings.System
                    .getUriFor("screen_brightness");
            android.provider.Settings.System.putInt(resolver, "screen_brightness",
                    bright);
            resolver.notifyChange(uri, null);
        }
    }
  • 相关阅读:
    Contains,Exists,Any,Count 比较是否存在某个元素
    RabbitMQ (十六) 消息队列的应用场景 (转)
    Quartz.NET 3.0.7 + MySql 动态调度作业+动态切换版本+多作业引用同一程序集不同版本+持久化+集群(四)
    RabbitMQ (十五) 镜像集群 + HAProxy1.7.8 负载均衡
    RabbitMQ (十四) 普通集群
    RabbitMQ (十三) 集群+单机搭建(window)
    (转) HA的几种方案
    RabbitMQ (十二) 消息确认机制
    RabbitMQ (十一) 消息确认机制
    VIM操作
  • 原文地址:https://www.cnblogs.com/qiengo/p/2501029.html
Copyright © 2020-2023  润新知