Android_ Adjust the screen brightness (background light) and keep the background light on the way (turn)

Source: Internet
Author: User

Android Write app can actually adjust the screen brightness, check the data found Windowmanager.layoutparams This class contains the parameters to adjust the brightness:

Windowmanager.layoutparams LP = GetWindow (). GetAttributes ();

lp.screenbrightness = 0.1f;

Where the parameter screenbrightness is the setting screen brightness of the 0.0f screen is the darkest ,1.0f screen Brightest

On this basis I added a SeekBar to adjust the screen brightness,

The code reference is as follows:

Also in the callback function must have the following sentence GetWindow (). SetAttributes (LP); Otherwise, it does not work. I set the value directly in the OnCreate () function at the beginning , without which it can be directly adjusted, but it does not work if the callback function is not added .

 Public classBlightactivityextendsActivity {intmax_brightness = 100; SeekBar Bseekbar=NULL;floatFbrightness = 0.0f; Windowmanager.layoutparams LP=NULL; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);               Setcontentview (R.layout.main); Bseekbar=(SeekBar) Findviewbyid (R.id.blightbar);        Bseekbar.setonseekbarchangelistener (Seeklistener);               Bseekbar.setmax (max_brightness); LP=GetWindow (). GetAttributes ();//lp.screenbrightness = 0.1f;} onseekbarchangelistener Seeklistener=NewOnseekbarchangelistener () { Public voidOnstoptrackingtouch (SeekBar SeekBar) {//TODO auto-generated Method Stub} Public voidOnstarttrackingtouch (SeekBar SeekBar) {//TODO auto-generated Method Stub} Public voidOnprogresschanged (SeekBar SeekBar,intProgress,BooleanFromuser) {//TODO auto-generated Method StubFbrightness = (float) Progress/(float) max_brightness;lp.screenbrightness=fbrightness;//this should be added, otherwise the screen brightness does not functionGetWindow (). SetAttributes (LP); System.out.println ("Fy_" +fbrightness);}};}
View Code

Here's how to keep the background light constant:

The first is to get permission:

<uses-permission android:name= "Android.permission.WAKE_LOCK" ></uses-permission>

Here is the control code instance

 Public classTestextendsactivity{PowerManager PowerManager=NULL; WakeLock WakeLock=NULL; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);  This. Setcontentview (R.layout.main);  This. PowerManager = (PowerManager) This. Getsystemservice (Context.power_service);  This. WakeLock = This. Powermanager.newwakelock (Powermanager.full_wake_lock, "My LOCK");  This. Wakelock.acquire (); } @Overrideprotected voidOnresume () {Super. Onresume (); //re-fetch          This. Wakelock.acquire (); } @Overrideprotected voidOnPause () {Super. OnPause (); //release Wakelock When the activity is destroyed          This. Wakelock.release (); } }
View Code

Transferred from: http://blog.csdn.net/zqiang_55/article/details/8043661#comments

Android_ Adjust the screen brightness (background light) and keep the background light on the way (turn)

Related Article

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.