set Screen Brightness - Android User Interface

Android examples for User Interface:Screen Brightness

Description

set Screen Brightness

Demo Code

import android.app.Activity;
import android.content.Context;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.view.WindowManager;

public class Main {
  public static void setScreenBrightness(Context context) {

    String daynight = "daynight";// "day";
    int brightness = 90;
    try {//from  w  w w. j  a  va2  s . c  o  m
      brightness = android.provider.Settings.System.getInt(
          context.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
    } catch (SettingNotFoundException e) {
      e.printStackTrace();
    }
    if (getScreenMode(context) == 1) {// ??
      return;
    }

    if (daynight.equals("day")) {
      WindowManager.LayoutParams lp = ((Activity) context).getWindow()
          .getAttributes();
      lp.screenBrightness = Float.valueOf(brightness) * (1f / 255f);
      ((Activity) context).getWindow().setAttributes(lp);
    } else {
      WindowManager.LayoutParams lp = ((Activity) context).getWindow()
          .getAttributes();
      lp.screenBrightness = Float.valueOf(brightness) * (1f / 255f) * 0.6f;
      ((Activity) context).getWindow().setAttributes(lp);
    }
  }

  private static int getScreenMode(Context context) {
    int screenMode = 0;
    try {
      screenMode = Settings.System.getInt(context.getContentResolver(),
          Settings.System.SCREEN_BRIGHTNESS_MODE);
    } catch (Exception localException) {

    }
    return screenMode;
  }
}

Related Tutorials