Change the System Brightness Programmatically

2019-01-03 02:08发布

I want to change the system brightness programmatically. For that purpose I am using this code:

WindowManager.LayoutParams lp = window.getAttributes();
lp.screenBrightness = (255);
window.setAttributes(lp);

because I heard that max value is 255.

but it does nothing. Please suggest any thing that can change the brightness. Thanks

10条回答
smile是对你的礼貌
2楼-- · 2019-01-03 02:45

This is the complete code on how to change system brightness

    private SeekBar brightbar;

    //Variable to store brightness value
    private int brightness;
    //Content resolver used as a handle to the system's settings
    private ContentResolver Conresolver;
    //Window object, that will store a reference to the current window
    private Window window;

    /** Called when the activity is first created. */
    @Override
    public void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //Instantiate seekbar object
        brightbar = (SeekBar) findViewById(R.id.ChangeBright);

        //Get the content resolver
        Conresolver = getContentResolver();

        //Get the current window
        window = getWindow();

        brightbar.setMax(255);

        brightbar.setKeyProgressIncrement(1);

        try {
            brightness = System.getInt(Conresolver, 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(Conresolver, System.SCREEN_BRIGHTNESS, brightness);

                LayoutParams layoutpars = window.getAttributes();

                layoutpars.screenBrightness = brightness / (float) 255;

                window.setAttributes(layoutpars);
            }

            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

                if (progress <= 20) {

                    brightness = 20;
                } else {

                    brightness = progress;
                }
            }
        });
    }

Or you may check this tutorial for complete code
happy coding:)

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-01-03 02:46
WindowManager.LayoutParams params = getWindow().getAttributes();
    params.screenBrightness = 10; // range from 0 - 255 as per docs
    getWindow().setAttributes(params);
    getWindow().addFlags(WindowManager.LayoutParams.FLAGS_CHANGED);

This worked for me. No need of a dummy activity. This works only for your current activity.

查看更多
戒情不戒烟
4楼-- · 2019-01-03 02:52

Reference link

  WindowManager.LayoutParams layout = getWindow().getAttributes();
 layout.screenBrightness = 1F;
  getWindow().setAttributes(layout);     
查看更多
我欲成王,谁敢阻挡
5楼-- · 2019-01-03 02:53

Please Try this , it's May help you. Worked fine for me

According to my experience

 1st method.
                     WindowManager.LayoutParams lp = getWindow().getAttributes();  
                 lp.screenBrightness = 75 / 100.0f;  
                 getWindow().setAttributes(lp);

where the brightness value very according to 1.0f.100f is maximum brightness.

The above mentioned code will increase the brightness of the current window. If we want to increase the brightness of the entire android device this code is not enough, for that we need to use

   2nd method.



       android.provider.Settings.System.putInt(getContentResolver(),
                         android.provider.Settings.System.SCREEN_BRIGHTNESS, 192); 

Where 192 is the brightness value which very from 1 to 255. The main problem of using 2nd method is it will show the brightness in increased form in android device but actually it will fail to increase android device brightness.This is because it need some refreshing.

That is why I find out the solution by using both codes together.

            if(arg2==1)
                {

         WindowManager.LayoutParams lp = getWindow().getAttributes();  
                 lp.screenBrightness = 75 / 100.0f;  
                 getWindow().setAttributes(lp);  
                 android.provider.Settings.System.putInt(getContentResolver(),
                         android.provider.Settings.System.SCREEN_BRIGHTNESS, 192);


                    }

It worked properly for me

查看更多
叛逆
6楼-- · 2019-01-03 03:01

this worked for me till kitkat 4.4 but not in android L

private void stopBrightness() {
    Settings.System.putInt(this.getContentResolver(),
            Settings.System.SCREEN_BRIGHTNESS, 0);
}
查看更多
迷人小祖宗
7楼-- · 2019-01-03 03:03

You can use following :

//Variable to store brightness value
private int brightness;
//Content resolver used as a handle to the system's settings
private ContentResolver cResolver;
//Window object, that will store a reference to the current window
private Window window;

In your onCreate write:

//Get the content resolver
cResolver = getContentResolver();

//Get the current window
window = getWindow();

    try
            {
               // To handle the auto
                Settings.System.putInt(cResolver,
                Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
 //Get the current system brightness
                brightness = System.getInt(cResolver, Settings.System.SCREEN_BRIGHTNESS);
            } 
            catch (SettingNotFoundException e) 
            {
                //Throw an error case it couldn't be retrieved
                Log.e("Error", "Cannot access system brightness");
                e.printStackTrace();
            }

Write the code to monitor the change in brightness.

then you can set the updated brightness as follows:

           //Set the system brightness using the brightness variable value
            System.putInt(cResolver, System.SCREEN_BRIGHTNESS, brightness);
            //Get the current window attributes
            LayoutParams layoutpars = window.getAttributes();
            //Set the brightness of this window
            layoutpars.screenBrightness = brightness / (float)255;
            //Apply attribute changes to this window
            window.setAttributes(layoutpars);

Permission in manifest:

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

For API > 23, you need to request the permission through Settings Activity, described here: Can't get WRITE_SETTINGS permission

查看更多
登录 后发表回答