Android 2.2: Adjusting screen brightness

亡梦爱人 提交于 2019-11-28 02:09:20

Make sure that "Auto Brightness" is not enabled prior to setting the screen brightness. You can do this manually in Settings>Display or using code if you are using Android 2.2 or above SDK.

Something like:

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);
}

WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.screenBrightness = 0.5F; // set 50% brightness
getWindow().setAttributes(layoutParams);

Ensure the value is between 0.0F and 1.0F. A value of -1.0F uses the default brightness stored in the preferences. Per the documentation "A value of less than 0, the default, means to use the preferred screen brightness. 0 to 1 adjusts the brightness from dark to full bright."

liu

The value of screenBrightness between 0.0f and 1.0f, maybe your value is too big. I use the WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON flag and also can adjust the screen brightness

Adjust Brightness using a seekbar :

public class AndroidBrightnessActivity extends Activity {

private SeekBar brightbar;
private int brightness;
private ContentResolver cResolver;
private Window window;
TextView txtPerc;
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    brightbar = (SeekBar) findViewById(R.id.brightbar);
    txtPerc = (TextView) findViewById(R.id.txtPercentage);
    cResolver = getContentResolver();
    window = getWindow();
    brightbar.setMax(255);
    brightbar.setKeyProgressIncrement(1);

    try
    {
        brightness = System.getInt(cResolver, System.SCREEN_BRIGHTNESS);
    }
    catch (SettingNotFoundException e)
    {
        Log.e("Error", "Cannot access system brightness");
        e.printStackTrace();
    }
    brightbar.setProgress(brightness);
    brightbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
    {
        public void onStopTrackingTouch(SeekBar seekBar)
        {
            System.putInt(cResolver,System.SCREEN_BRIGHTNESS,brightness);
            LayoutParams layoutpars = window.getAttributes();
            layoutpars.screenBrightness = brightness / (float)255;
            window.setAttributes(layoutpars);
        }

        public void onStartTrackingTouch(SeekBar seekBar)
        {
            //Nothing handled here
        }

        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
        {
            if(progress<=20)
            {
              brightness=20;
            }
            else
            {
                brightness = progress;
            }
            float perc = (brightness /(float)255)*100;
            txtPerc.setText((int)perc +" %");
        }
    });   
}

}

Happy Coding...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!