博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android 系统设置中显示设置之亮度调节篇
阅读量:5878 次
发布时间:2019-06-19

本文共 7109 字,大约阅读时间需要 23 分钟。

Android 系统设置中显示设置之亮度调节篇 

  在上一篇博客中,我们学习了显示设置中休眠设置和屏保设置的基本内容。在这一篇中我们来学习设备亮度的调节,首先还是学习在DisplaySettings.xml增加了一个叫做BrightnessPreference的preference的选项用来控制亮度调节。

  步骤1:当用户点击BrightnessPreference的时候,由BrightnessPreference做对应的处理(注:其实是发送了一个广播)  

1 public class BrightnessPreference extends Preference { 2  3     public BrightnessPreference(Context context, AttributeSet attrs) { 4         super(context, attrs); 5     } 6  7     @Override 8     protected void onClick() { 9         //发送了一个显示亮度调节对话框的广播10         Intent intent = new Intent(Intent.ACTION_SHOW_BRIGHTNESS_DIALOG);11         getContext().sendBroadcastAsUser(intent, UserHandle.CURRENT_OR_SELF);12     }13 }

 

   步骤2:对广播的注册并处理,有一个SettingsUI的类继承自SystemUI,该类负责监听上面的广播并进行处理,详见:

1 public class SettingsUI extends SystemUI { 2     private static final String TAG = "SettingsUI"; 3     private static final boolean DEBUG = false; 4     private final Handler mHandler = new Handler(); 5     private BrightnessDialog mBrightnessDialog; 6     private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { 7         @Override 8         public void onReceive(Context context, Intent intent) { 9             String action = intent.getAction();10             if (action.equals(Intent.ACTION_SHOW_BRIGHTNESS_DIALOG)) {11                 if (DEBUG) Log.d(TAG, "showing brightness dialog");12                 if (mBrightnessDialog == null) {13                     mBrightnessDialog = new BrightnessDialog(mContext);14                     mBrightnessDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {15                         @Override16                         public void onDismiss(DialogInterface dialog) {17                             mBrightnessDialog = null;}});18                 }19                 if (!mBrightnessDialog.isShowing()) {20                     //显示亮度调节的dialog21                     mBrightnessDialog.show(); }22             } else {23                 Log.w(TAG, "unknown intent: " + intent);}}};24 25     public void start() {26         //监听ACTION_SHOW_BRIGHTNESS_DIALOG广播27         IntentFilter filter = new IntentFilter();28         filter.addAction(Intent.ACTION_SHOW_BRIGHTNESS_DIALOG);29         mContext.registerReceiverAsUser(mIntentReceiver, UserHandle.ALL, filter, null, mHandler);30     }

 

   步骤3:上面是负责显示亮度调节的对话框,具体的设置需要查看BrightnessDialog,详见:

 

1     /** 2      * Create the brightness dialog and any resources that are used for the 3      * entire lifetime of the dialog. 4      */ 5     @Override 6     public void onCreate(Bundle savedInstanceState) { 7         super.onCreate(savedInstanceState); 8          9         Window window = getWindow();10         window.setType(WindowManager.LayoutParams.TYPE_VOLUME_OVERLAY);11         window.getAttributes().privateFlags |=12                 WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;13         window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);14         window.requestFeature(Window.FEATURE_NO_TITLE);15         //dialog显示的内容quick_settings_brightness_dialog.xml16         setContentView(R.layout.quick_settings_brightness_dialog);17         setCanceledOnTouchOutside(true);18     }19 20 21     @Override22     protected void onStart() {23         super.onStart();24         //对亮度的调节主要是使用了BrightnessController类25         mBrightnessController = new BrightnessController(getContext(),26                 (ImageView) findViewById(R.id.brightness_icon),27                 (ToggleSlider) findViewById(R.id.brightness_slider));28         dismissBrightnessDialog(mBrightnessDialogLongTimeout);29         mBrightnessController.addStateChangedCallback(this);30     }
View Code

 

   步骤4: BrightnessController详解:

    我们这里主要还是学习下一些基本的内容,具体包括获取设备最大的亮度和最低的亮度值、如果设置屏幕的亮度、当前的亮度值和亮度调节模式保存和获取的位置。

      • 获取设备的最大和最低亮度值        
1         //获取PowerManager对象2         PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);3         //鑾峰彇璁惧鐨勬渶澶т寒搴?4         mMinimumBacklight = pm.getMinimumScreenBrightnessSetting();5         //鑾峰彇璁惧鐨勬渶浣庝寒搴?6         mMaximumBacklight = pm.getMaximumScreenBrightnessSetting();

 

      • 设置屏幕亮度
//首先获取IPowerManager对象        mPower = IPowerManager.Stub.asInterface(ServiceManager.getService("power"));
private void setBrightness(int brightness) {        try {            mPower.setTemporaryScreenBrightnessSettingOverride(brightness);        } catch (RemoteException ex) {        }    }

 

      • 亮度值和亮度模式的读取和存储
1     //设置亮度的设置模式,mode=0表示为手动,mode=1表示自动调节2     private void setMode(int mode) {3         //存储位置为Settings.system中4         Settings.System.putIntForUser(mContext.getContentResolver(),5                 Settings.System.SCREEN_BRIGHTNESS_MODE, mode,6                 mUserTracker.getCurrentUserId());7     } //以下的代码是负责取出设置模式的

int automatic;

try {
    automatic = Settings.System.getIntForUser(mContext.getContentResolver(),
    Settings.System.SCREEN_BRIGHTNESS_MODE,
    UserHandle.USER_CURRENT);
} catch (SettingNotFoundException snfe) {
    automatic = 0;
}

 

//以下的代码是将亮度值保存到Settings.system中        if (!tracking) {                AsyncTask.execute(new Runnable() {                        public void run() {                            Settings.System.putIntForUser(mContext.getContentResolver(),                                    Settings.System.SCREEN_BRIGHTNESS, val,                                    UserHandle.USER_CURRENT);                        }                    });            }

 

/** Fetch the brightness from the system settings and update the slider */    private void updateSlider() {        int value;        try {            //从Settings.System中获取亮度值            value = Settings.System.getIntForUser(mContext.getContentResolver(),                    Settings.System.SCREEN_BRIGHTNESS,                    UserHandle.USER_CURRENT);        } catch (SettingNotFoundException ex) {            value = mMaximumBacklight;        }        mControl.setMax(mMaximumBacklight - mMinimumBacklight);        mControl.setValue(value - mMinimumBacklight);    }

 

 

  杂谈:

    我们在BrightnessController中找到了一个内部类叫做BrightnessController,它继承自ContentObserver。该过程使用了观察者设计模式,主要是用来负责对ContentProvider中指定的URI进行监听,如果该URI对应的数据发生了变化就可以及时得到监听并处理。它的使用请参考:。

    BrightnessController主要是监听了

private final Uri BRIGHTNESS_MODE_URI =                Settings.System.getUriFor(Settings.System.SCREEN_BRIGHTNESS_MODE);//亮度调节模式        private final Uri BRIGHTNESS_URI =                Settings.System.getUriFor(Settings.System.SCREEN_BRIGHTNESS);//亮度值
1         //当监听的uri发生变化时,回调onChange函数 2         @Override 3         public void onChange(boolean selfChange, Uri uri) { 4             if (selfChange) return; 5             if (BRIGHTNESS_MODE_URI.equals(uri)) { 6                 updateMode();//更新模式 7             } else if (BRIGHTNESS_URI.equals(uri)) { 8                 updateSlider();//更新拖动条 9             } else {10                 updateMode();11                 updateSlider();12             }13             for (BrightnessStateChangeCallback cb : mChangeCallbacks) {14                 cb.onBrightnessLevelChanged();15             }16         }

 

  以上就是对亮度调节的基本内容,希望大家能够喜欢,您的支持是我不懈的动力,当然不喜勿喷,有错请指正!

 

转载于:https://www.cnblogs.com/yinhaojun/p/3876132.html

你可能感兴趣的文章
about 类和对象的零散知识
查看>>
评论列表显示及排序,个人中心显示
查看>>
Apache优化:修改最大并发连接数(转)
查看>>
rabbitmq学习——交换机
查看>>
信号处理——傅里叶变换(FT-DTFT-DFT)
查看>>
WebService代理类的详解
查看>>
常用弹出对话框函数
查看>>
2018暑期生活指导第三周
查看>>
php中字符串函数
查看>>
mysql中影响myisam引擎写入性能的三项设置
查看>>
python 中初始化二维数组的方法
查看>>
关于父类引用指向子类对象
查看>>
Oracle连接出现TNS:no listener或者ORA-12514: TNS:listener does not currently know
查看>>
第三次作业中遇到的困难和解决方法
查看>>
透过HT for Web 3D看动画Easing函数本质
查看>>
WebGL实现HTML5的3D贪吃蛇游戏
查看>>
JS 二维数组排序
查看>>
软件包管理 之 file.src.rpm 使用方法的简单介绍
查看>>
Perl语言的多线程(二)
查看>>
职场必须要会的餐桌礼仪
查看>>