Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the System Brightness Programmatically

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

like image 737
Usman Riaz Avatar asked Aug 19 '13 11:08

Usman Riaz


People also ask

How do I force change brightness?

Use the keyboard shortcut Windows + A to open the Action Center, revealing a brightness slider at the bottom of the window. Moving the slider at the bottom of the Action Center left or right changes the brightness of your display.

How do I change brightness on Linux?

To change the brightness of your screen, click the system menu on the right side of the top bar and adjust the screen brightness slider to the value you want to use. The change should take effect immediately.


2 Answers

I had the same problem.

Two solutions:

here, brightness =(int) 0 to 100 range as i am using progressbar

1 SOLUTION

float brightness = brightness / (float)255; WindowManager.LayoutParams lp = getWindow().getAttributes();         lp.screenBrightness = brightness;         getWindow().setAttributes(lp); 

2 SOLUTION

I just used dummy activity to call when my progress bar stop seeking.

 Intent intent = new Intent(getBaseContext(), DummyBrightnessActivity.class);                     Log.d("brightend", String.valueOf(brightness / (float)255));                     intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //this is important                     //in the next line 'brightness' should be a float number between 0.0 and 1.0                     intent.putExtra("brightness value", brightness / (float)255);                      getApplication().startActivity(intent); 

Now coming to the DummyBrightnessActivity.class

 public class DummyBrightnessActivity extends Activity{  private static final int DELAYED_MESSAGE = 1;  private Handler handler;  @Override protected void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);                 handler = new Handler() {         @Override         public void handleMessage(Message msg) {             if(msg.what == DELAYED_MESSAGE) {                 DummyBrightnessActivity.this.finish();             }             super.handleMessage(msg);         }     };     Intent brightnessIntent = this.getIntent();     float brightness = brightnessIntent.getFloatExtra("brightness value", 0);     WindowManager.LayoutParams lp = getWindow().getAttributes();     lp.screenBrightness = brightness;     getWindow().setAttributes(lp);      Message message = handler.obtainMessage(DELAYED_MESSAGE);     //this next line is very important, you need to finish your activity with slight delay     handler.sendMessageDelayed(message,200);  }  } 

don't forget to register DummyBrightnessActivity to manifest.

hope it helps!!

like image 43
Bhoomika Brahmbhatt Avatar answered Sep 22 '22 05:09

Bhoomika Brahmbhatt


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 = Settings.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 Settings.System.putInt(     cResolver, Settings.System.SCREEN_BRIGHTNESS, brightness ); // Get the current window attributes LayoutParams layoutpars = window.getAttributes(); // Set the brightness of this window layoutpars.screenBrightness = brightness / 255f; // Apply attribute changes to this window window.setAttributes(layoutpars); 

Permission in manifest:

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

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

like image 53
Ritesh Gune Avatar answered Sep 21 '22 05:09

Ritesh Gune