Sanat Pandey
Sanat Pandey

Reputation: 4103

Decrease the brightness of the screen on button click

I have a problem that I want to decrease the brightness of the screen on the button click but failed to do that. I don't know why? I have written the code of few lines but it doesn't work for me. Please suggest me the right solution regarding the same.

Code:

private void setBrightness() {  

        try {
            int curBrightnessValue = android.provider.Settings.System.getInt(getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS);
            WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
            layoutParams.screenBrightness = curBrightnessValue/100.0f;
            getWindow().setAttributes(layoutParams);
        } catch (SettingNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
      }  

Upvotes: 0

Views: 1727

Answers (2)

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132992

Use IHardwareService interface for this:

permission

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

Modify your Code like:

private void setBrightness(int brightness) {  
    try {  
      IHardwareService hardware = IHardwareService.Stub.asInterface(  
ServiceManager.getService("hardware"));  
      if (hardware != null) {  
        hardware.setScreenBacklight(brightness);  
      }  
    } catch (RemoteException doe) {            
    }  

full example here Changing the Screen Brightness

OR in your case maybe possible:

WindowManager.LayoutParams lp = getWindow().getAttributes();

Move this to After you do setContentView(R.layout.main); You can't do getWindow().getAttributes() before window is constructed.

Thus, your code will become

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
     // MY BRIGHTNESS VARIABLES


 WindowManager.LayoutParams lp;
 float fb;
 float lb = 0;
 float hb = 1;
 //////////////////////////////////////////////////////////////////////////
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    lp = getWindow().getAttributes();
    fb = lp.screenBrightness;

   // MY CODE FROM HERE DOWN

    Button button1=(Button)findViewById(R.id.button1);

    button1.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {

        if(lp.screenBrightness==fb) {
            lp.screenBrightness=lb;
            getWindow().setAttributes(lp);
        }
        if(lp.screenBrightness==lb){
            lp.screenBrightness=hb;
            getWindow().setAttributes(lp);
        }
        if(lp.screenBrightness==hb){
            lp.screenBrightness=fb;
            getWindow().setAttributes(lp);
        }

    }
} );
    //////////////////////////////////////////////




}

}

Upvotes: 2

user370305
user370305

Reputation: 109257

Your code seems to good, but I think for taking effect of brightness your activity has to wait for 500ms, Just use given below code,

 try {
      ContentResolver cr = getContentResolver();
      int brightness = Settings.System.getInt(cr,Settings.System.SCREEN_BRIGHTNESS);            
      Settings.System.putInt(cr, Settings.System.SCREEN_BRIGHTNESS, brightness);
      WindowManager.LayoutParams lp = getWindow().getAttributes();
      lp.screenBrightness = brightness / 255.0f;
      getWindow().setAttributes(lp);
      } catch (Exception e) {
         Log.d("Bright", "toggleBrightness: " + e);
      }

      final Activity activity = this;
      Thread t = new Thread(){
        public void run()
         {
          try {
               sleep(500);
              } catch (InterruptedException e) {}
              activity.finish();
            }
        };
        t.start();

UPDATE:

The main code is just,

WindowManager.LayoutParams layout = getWindow().getAttributes();
layout.screenBrightness = 1F;
getWindow().setAttributes(layout);

Also try this.. Try this and let me know what happen,

Upvotes: 0

Related Questions