This article describes the method of Android programming to adjust the screen brightness (background light) and keep the background light always on. Share it for your reference, as follows:
Applications written by Android can actually adjust the screen brightness. After checking the information, I found that this class contains parameters for adjusting the brightness:
lp = getWindow().getAttributes(); = 0.1f;
The parameter screenBrightness is to set the screen brightness of 0.0f, and the screen brightest 1.0f,
On this basis, I added a SeekBar to adjust the screen brightness.
The code reference is as follows:
In addition, the following sentence must be included in the callback function getWindow().setAttributes(lp); otherwise it will not work. At the beginning, I set the value directly in the onCreate() function. Without this function, I can adjust it directly, but it will not work if I don't add it in the callback function.
public class BLightActivity extends Activity { /** Called when the activity is first created. */ int Max_Brightness = 100; SeekBar bSeekBar = null; float fBrightness = 0.0f; lp = null; @Override public void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(); bSeekBar = (SeekBar) findViewById(); (seekListener); (Max_Brightness); lp = getWindow().getAttributes(); // = 0.1f; } OnSeekBarChangeListener seekListener = new OnSeekBarChangeListener() { public void onStopTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub } public void onStartTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub } public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { // TODO Auto-generated method stub fBrightness = (float)progress / (float)Max_Brightness; = fBrightness; // This sentence must be added, otherwise the screen brightness will not workgetWindow().setAttributes(lp); ("FY_" + fBrightness); } }; }
Here is a way to keep the background light constant:
First, you need to obtain permissions:
Below is an example of the control code
public class test extends Activity{ PowerManager powerManager = null; WakeLock wakeLock = null; @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); (); = (PowerManager)(Context.POWER_SERVICE); = (PowerManager.FULL_WAKE_LOCK, "My Lock"); (); } @Override protected void onResume() { (); // Re-acquire (); } @Override protected void onPause() { (); // Release wakeLock when Activity is destroyed (); } }
For more information about Android development, readers who are interested in reading the topic of this site:Android development introduction and advanced tutorial》
I hope this article will be helpful to everyone's Android programming design.