Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android SDK: How do you set the screen brightness

Tags:

java

android

I have been looking around and I could only find code that will set the brightness on that one Activity. I am trying to change the actual phone settings. The code I have tried is:

public class AutoPowerManagerActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        try {
            adjustBright();
        } catch (SettingNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private void adjustBright() throws SettingNotFoundException {
        // TODO Auto-generated method stub
        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;
        getWindow().setAttributes(layoutParams);
    }
}
like image 303
RedShirt Avatar asked Nov 04 '22 14:11

RedShirt


1 Answers

You can set the brightness back to automatic by setting by using following coding, its works for me.

layoutParams.screenBrightness=-1;
getWindow().setAttributes(layoutParams);

Here is the full coding:

public class MainActivity extends Activity {

WindowManager.LayoutParams layoutParams;
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    try {
        adjustBright();
    } catch (SettingNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    Button btn=(Button)findViewById(R.id.button1);

    btn.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            // TODO Auto-generated method stub
            layoutParams.screenBrightness=-1;
            getWindow().setAttributes(layoutParams);
        }
    });



}

private void adjustBright() throws SettingNotFoundException {
    // TODO Auto-generated method stub
    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);
    }

    layoutParams = getWindow().getAttributes();
    layoutParams.screenBrightness = 0.1F;
    getWindow().setAttributes(layoutParams);
}
}
like image 105
Abdul Rahman Avatar answered Nov 11 '22 11:11

Abdul Rahman