In the previous blog, we learned the basics of the Hibernate settings and screensaver settings in the display settings. In this article, we learn the adjustment of the brightness of the device, first of all, or learn to add an option called Brightnesspreference in Displaysettings.xml to control the brightness adjustment.
Step 1: When the user clicks Brightnesspreference, by the brightnesspreference to do the corresponding processing (note: Actually sent a broadcast)
1 Public classBrightnesspreferenceextendsPreference {2 3 Publicbrightnesspreference (Context context, AttributeSet attrs) {4 Super(context, attrs);5 }6 7 @Override8 protected voidOnClick () {9 //sends a broadcast that displays a Brightness adjustment dialog boxTenIntent Intent =NewIntent (intent.action_show_brightness_dialog); One GetContext (). Sendbroadcastasuser (Intent, userhandle.current_or_self); A } -}
Step 2: Registration and processing of the broadcast, there is a Settingsui class inherited from the Systemui, this class is responsible for monitoring the above broadcast and processing, see:
1 Public classSettingsuiextendsSystemui {2 Private Static FinalString TAG = "Settingsui";3 Private Static Final BooleanDEBUG =false;4 Private FinalHandler Mhandler =NewHandler ();5 Private brightnessdialog mbrightnessdialog;6 PrivateBroadcastreceiver Mintentreceiver =NewBroadcastreceiver () {7 @Override8 Public voidOnReceive (Context context, Intent Intent) {9String action =intent.getaction ();Ten if(Action.equals (Intent.action_show_brightness_dialog)) { One if(DEBUG) LOG.D (TAG, "showing Brightness Dialog"); A if(Mbrightnessdialog = =NULL) { -Mbrightnessdialog =NewBrightnessdialog (mcontext); -Mbrightnessdialog.setondismisslistener (NewDialoginterface.ondismisslistener () { the @Override - Public voidOndismiss (Dialoginterface dialog) { -Mbrightnessdialog =NULL;}}); - } + if(!mbrightnessdialog.isshowing ()) { - //dialog for display brightness adjustment + mbrightnessdialog.show ();} A}Else { atLOG.W (TAG, "Unknown Intent:" +intent); }}; - - Public voidstart () { - //Monitor Action_show_brightness_dialog broadcast -Intentfilter filter =NewIntentfilter (); - filter.addaction (intent.action_show_brightness_dialog); inMcontext.registerreceiverasuser (Mintentreceiver, Userhandle.all, filter,NULL, Mhandler); -}
Step 3: Above is responsible for displaying the brightness Adjustment dialog box, the specific settings need to see Brightnessdialog, see:
1 /**2 * Create The Brightness dialog and any resources that is used for the3 * Entire lifetime of the dialog.4 */5 @Override6 Public voidonCreate (Bundle savedinstancestate) {7 Super. OnCreate (savedinstancestate);8 //The window is used here so that our brightness adjustment dialog can be displayed in the global interface .9window window =GetWindow ();Ten Window.settype (WindowManager.LayoutParams.TYPE_VOLUME_OVERLAY); OneWindow.getattributes (). Privateflags |= A WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS; - window.clearflags (WindowManager.LayoutParams.FLAG_DIM_BEHIND); - window.requestfeature (window.feature_no_title); the //Loading our dialog box layout file is actually a imageview (switch) and Com.android.systemui.settings.ToggleSlider (drag bar) - Setcontentview (r.layout.quick_settings_brightness_dialog); -Setcanceledontouchoutside (true);//touch other parts to hide the dialog - } + @Override - protected voidOnStart () { + Super. OnStart (); A //When we click or drag Toggleslider, there are brightnesscontroller to handle it. atMbrightnesscontroller =NewBrightnesscontroller (GetContext (), - (ImageView) Findviewbyid (R.id.brightness_icon), - (Toggleslider) Findviewbyid (R.id.brightness_slider)); - Dismissbrightnessdialog (mbrightnessdialoglongtimeout); -Mbrightnesscontroller.addstatechangedcallback ( This); -}
View Code
Step 4:brightnesscontroller Detailed