问题
There are apparently at least three different techniques for changing screen brightness in the Android OS. Two of them no longer work post-cupcake and the third accepted technique evidently has a bug.
I would like to increase screen brightness at the start of a one-view activity then turn the brightness back to the user setting at the end of the activity. No buttons, no second view or second activity. Just a maxed brightness at start and a return to original brightness setting at the end of the activity.
The examples of the current technique using WindowManager.LayoutParams that I have located range from 2 lines of code
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.screenBrightness = 100 / 100.0f;
getWindow().setAttributes(lp);
to several pages of source code and numerous activities.
Is there a simple example of maxing the screen brightness at start that someone can provide a link to?
回答1:
Its simple what you can do is make a SeekBar in xml using drag drop or write its xml
SeekBar
android:id="@+id/brightnesSseeker"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:max="255"
android:progress="255" />
in mainActivity class do this:
brightnesSseeker = (SeekBar) findViewById(R.id.brightnesSseeker);
brightnesSseeker
.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
public void onStopTrackingTouch(SeekBar seekBar) {
// Nothing handled here
}
public void onStartTrackingTouch(SeekBar seekBar) {
// Nothing handled here
}
public void onProgressChanged(SeekBar seekBar,
int progress, boolean fromUser) {
// Set the minimal brightness level
// if seek bar is 20 or any value below
if (progress <= 20) {
// Set the brightness to 20
screenBrightness(20);
} else {
// Set brightness variable based on the progress bar
screenBrightness(progress);
}
stobeVariable=progress*10;
}
});
and then write a simple function :
private void screenBrightness(double newBrightnessValue) {
/*
* WindowManager.LayoutParams settings = getWindow().getAttributes();
* settings.screenBrightness = newBrightnessValue;
* getWindow().setAttributes(settings);
*/
WindowManager.LayoutParams lp = getWindow().getAttributes();
float newBrightness = (float) newBrightnessValue;
lp.screenBrightness = newBrightness / (float) 255;
getWindow().setAttributes(lp);
}
Also do check the brightness mode before calling screenBrightness function because sometime the mode is selected to auto brightness and in that case the brightness won't change. You also need permission in manifest file too.
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
Call the function bellow in onCreate before setOnSeekBarChangeListener
void changeBrightnessMode(){
try {
int brightnessMode = Settings.System.getInt(getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE);
if (brightnessMode == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) {
Settings.System.putInt(getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE,
Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
}
} catch (Exception e) {
// do something useful
}
}
来源:https://stackoverflow.com/questions/2937365/increasing-screen-brightness-for-activity