Set screen brightness and change system brightness, must declare the android.Manifest.permission#WRITE_SETTINGS permission in its manifest. - Android User Interface

Android examples for User Interface:Screen Brightness

Description

Set screen brightness and change system brightness, must declare the android.Manifest.permission#WRITE_SETTINGS permission in its manifest.

Demo Code

/*// www  . ja  v a 2 s  .  co  m
 * Copyright (C) 2016 venshine.cn@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import android.app.Activity;
import android.content.Context;
import android.provider.Settings;
import android.view.Window;
import android.view.WindowManager;

public class Main {
  /**
   * Set screen brightness and change system brightness, must declare the
   * {@link android.Manifest.permission#WRITE_SETTINGS} permission in its
   * manifest.
   *
   * @param activity
   * @param screenBrightness
   *          0-255
   * @return
   */
  public static boolean setScreenBrightnessAndApply(Activity activity, int screenBrightness) {
    boolean result = setScreenBrightness(activity, screenBrightness);
    if (result) {
      setWindowBrightness(activity, screenBrightness);
    }
    return result;
  }

  /**
   * Set screen brightness, cannot change window brightness.must declare the
   * {@link android.Manifest.permission#WRITE_SETTINGS} permission in its
   * manifest.
   *
   * @param context
   * @param screenBrightness
   *          0-255
   * @return
   */
  public static boolean setScreenBrightness(Context context, int screenBrightness) {
    int brightness = screenBrightness;
    if (screenBrightness < 1) {
      brightness = 1;
    } else if (screenBrightness > 255) {
      brightness = screenBrightness % 255;
      if (brightness == 0) {
        brightness = 255;
      }
    }
    boolean result = Settings.System.putInt(context.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS,
        brightness);
    return result;
  }

  /**
   * Set window brightness, cannot change system brightness.
   *
   * @param activity
   * @param screenBrightness
   *          0-255
   */
  public static void setWindowBrightness(Activity activity, float screenBrightness) {
    float brightness = screenBrightness;
    if (screenBrightness < 1) {
      brightness = 1;
    } else if (screenBrightness > 255) {
      brightness = screenBrightness % 255;
      if (brightness == 0) {
        brightness = 255;
      }
    }
    Window window = activity.getWindow();
    WindowManager.LayoutParams localLayoutParams = window.getAttributes();
    localLayoutParams.screenBrightness = brightness / 255.0f;
    window.setAttributes(localLayoutParams);
  }
}

Related Tutorials