Brightness adjustment function set by mobile phone

Source: Internet
Author: User

From: http://blog.csdn.net/cys410/article/details/7346114

1. brightness settings

// Determine whether to automatically adjust public static 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;} // obtain the current brightness public static int getscreenbrightness (context) {int nowbrightnessvalue = 0; contentresolver resolver = context. getcontentresolver (); try {nowbrightnessvalue = android. provider. settings. system. getint (Resolver, settings. system. screen_brightness);} catch (exception e) {e. printstacktrace ();} return nowbrightnessvalue;} // sets the brightness of the current screen. Public static void setbrightness (activity context, int brightness) {windowmanager. layoutparams Lp = context. getwindow (). getattributes (); LP. screenbrightness = float. valueof (brightness) * (1f/255f); context. getwindow (). setattributes (LP);} // turn off the screen brightness to automatically adjust public static void stopautobrightness (activity) {settings. system. putint (activity. getcontentresolver (), settings. system. screen_brightness_mode, settings. system. screen_brightness_mode_manual);} // enable the screen brightness to automatically adjust public static void startautobrightness (activity) {settings. system. putint (activity. getcontentresolver (), settings. system. screen_brightness_mode, settings. system. screen_brightness_mode_automatic);} // Save the brightness of the screen. Public static void savebrightness (contentresolver resolver, int brightness) {URI uri = android. provider. settings. system. geturifor ("screen_brighness"); settings. system. putint (Resolver, "screen_brightness", brightness); resolver. policychange (Uri, null );}

2. xml configuration file

Main. xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <Button        android:id="@+id/btn"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="btn" /></LinearLayout>

Dialog. xml

<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: Orientation = "vertical"> <checkbox Android: id = "@ + ID/CB" Android: layout_width = "220dip" Android: layout_height = "wrap_content" Android: text = "My name is xiaoq"/> <seekbar Android: id = "@ + ID/seek" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: layout_margin = "10dip" drag seekbar to manually adjust = "" Android: max = "500"/> </linearlayout>

3. Activity

Private Boolean FF = false; private button BTN; private seekbar Sb; private checkbox CB; private view V1; private alertdialog ALT; private int m; @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); BTN = (button) This. findviewbyid (R. id. BTN); FF = This. getsharedpreferences ("hoperun", mode_private ). getboolean ("checkable", true); M = This. get Sharedpreferences ("hoperun", mode_private ). getint ("light", brightutil. getscreenbrightness (this); toast. maketext (this, m + "", 10000 ). show (); V1 = This. mm (this); BTN. setonclicklistener (New onclicklistener () {@ overridepublic void onclick (view v) {dialogactivity. this. alt = new alertdialog. builder (dialogactivity. this ). setview (V1 ). settitle ("test "). seticon (R. drawable. ic_launcher ). setmessage ("Current brightness is" + M ). setp Ositivebutton ("ddddd", new dialoginterface. onclicklistener () {@ overridepublic void onclick (dialoginterface dialog, int which) {editor ET = dialogactivity. this. getsharedpreferences ("hoperun", mode_private ). edit (); If (FF) et. putboolean ("checkable", ff); else {et. putboolean ("checkable", ff); toast. maketext (dialogactivity. this, M + "", 10000 ). show (); et. putint ("light", m);} et. commit ();}}). show () ;}}); handle () ;} Private view mm (dialogactivity) {v1 = layoutinflater. from (this ). inflate (R. layout. dialog, null); sb = (seekbar) v1.findviewbyid (R. id. seek); Cb = (checkbox) v1.findviewbyid (R. id. CB); Return V1;} private void handle () {If (FF) {If (! Brightutil. isautobrightness (this. getcontentresolver () brightutil. startautobrightness (this); CB. setchecked (true); sb. setvisibility (sb. gone); M = brightutil. getscreenbrightness (this); brightutil. setbrightness (this, m);} else {If (brightutil. isautobrightness (this. getcontentresolver () {brightutil. stopautobrightness (this);} CB. setchecked (false); sb. setvisibility (sb. visible); sb. setprogress (m); sb. setonseekbarch Angelistener (New onseekbarchangelistener () {@ overridepublic void onprogresschanged (seekbar, int progress, Boolean fromuser) {M = progress; brightutil. setbrightness (dialogactivity. this, m); Alt. setmessage ("Current brightness is" + M); toast. maketext (dialogactivity. this, M + "", 10000 ). show () ;}@ overridepublic void onstarttrackingtouch (seekbar) {M = seekbar. getprogress (); brightutil. setbrightness (dialogacti Invalid. this, m); Alt. setmessage ("Current brightness is" + M); toast. maketext (dialogactivity. this, M + "", 10000 ). show () ;}@ overridepublic void onstoptrackingtouch (seekbar) {M = seekbar. getprogress (); brightutil. setbrightness (dialogactivity. this, m); Alt. setmessage ("Current brightness is" + M); toast. maketext (dialogactivity. this, M + "", 10000 ). show () ;}}) ;}cb. setoncheckedchangelistener (New oncheckedchangelistener () {@ overridepublic Void oncheckedchanged (compoundbutton buttonview, Boolean ischecked) {If (CB. ischecked () {If (! Brightutil. isautobrightness (dialogactivity. this. getcontentresolver () brightutil. startautobrightness (dialogactivity. this); CB. setchecked (true); sb. setvisibility (sb. gone); Alt. setmessage ("Current brightness:" + brightutil. getscreenbrightness (dialogactivity. this); FF = true;} else {FF = false; If (brightutil. isautobrightness (dialogactivity. this. getcontentresolver () {brightutil. stopautobrightness (dialogactivity. this);} CB. setchecked (false); sb. setvisibility (sb. visible); M = dialogactivity. this. getsharedpreferences ("hoperun", mode_private ). getint ("light", brightutil. getscreenbrightness (dialogactivity. this); sb. setprogress (m); Alt. setmessage ("Current brightness:" + M); sb. setonseekbarchangelistener (New onseekbarchangelistener () {@ overridepublic void onprogresschanged (seekbar, int progress, Boolean fromuser) {M = progress; brightutil. setbrightness (dialogactivity. this, m); Alt. setmessage ("Current brightness is" + M); toast. maketext (dialogactivity. this, brightutil. getscreenbrightness (dialogactivity. this) + "", 10000 ). show () ;}@ overridepublic void onstarttrackingtouch (seekbar) {M = seekbar. getprogress (); brightutil. setbrightness (dialogactivity. this, m); Alt. setmessage ("Current brightness is" + M);} @ overridepublic void onstoptrackingtouch (seekbar) {M = seekbar. getprogress (); brightutil. setbrightness (dialogactivity. this, m); Alt. setmessage ("Current brightness is" + M); toast. maketext (dialogactivity. this, brightutil. getscreenbrightness (dialogactivity. this) + "", 10000 ). show ();}});}}});}

 

Note:

This has not been tested on a real machine. I will test it on my mobile phone and delete it.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.