Android programming to adjust the screen brightness of background light and keep the background light on
- 2021-01-22 05:24:13
- OfStack
This article illustrates the Android programming to adjust the brightness of the screen (the background light) and keep the background light on. To share with you for your reference, as follows:
WindowManager.LayoutParams class contains parameters to adjust the brightness of the screen:
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = 0.1f;
0.0f screen is the darkest and 1.0f screen is the brightest
On this basis, I added an SeekBar to adjust the brightness of the screen.
The code reference is as follows:
getWindow().setAttributes (lp); getWindow().setAttributes (lp); Otherwise it doesn't work. The values I initially set in the onCreate() function can be adjusted directly without this function, but will not work unless added to 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;
WindowManager.LayoutParams lp = null;
@Override
public void onCreate(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 = 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;
lp.screenBrightness = fBrightness;
// This sentence must be added, otherwise the screen brightness will not be effective
getWindow().setAttributes(lp);
System.out.println("FY_" + fBrightness);
}
};
}
Here's how to keep the background light constant:
The first thing to do is get permission:
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
Here is an example of the control code
public class test extends Activity{
PowerManager powerManager = null;
WakeLock wakeLock = null;
@Override
protected void onCreate(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();
}
@Override
protected void onResume() {
super.onResume();
// To obtain
this.wakeLock.acquire();
}
@Override
protected void onPause() {
super.onPause();
// in Activity Release at the time of destruction wakeLock
this.wakeLock.release();
}
}
For more information on Android development, please check out the Android Introduction and Advanced Course.
Hope this article described to everyone Android program design is helpful.