set Screen Brightness for day or night - Android android.app

Android examples for android.app:Screen

Description

set Screen Brightness for day or night

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 = "day";
    int brightness = 90;
    try {//from www  .  j  a v  a  2  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