Set screen brightness mode?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 mode?must declare the android.Manifest.permission#WRITE_SETTINGS permission in its manifest.

Demo Code

/*/*from  ww  w  .  j  ava 2  s . c  o 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.content.Context;
import android.provider.Settings;

public class Main {
  /**
   * Set screen brightness mode?must declare the
   * {@link android.Manifest.permission#WRITE_SETTINGS} permission in its
   * manifest.
   *
   * @param context
   * @param auto
   * @return
   */
  public static boolean setScreenBrightnessMode(Context context, boolean auto) {
    boolean result = true;
    if (isScreenBrightnessModeAuto(context) != auto) {
      result = Settings.System.putInt(context.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE,
          auto ? Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC : Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
    }
    return result;
  }

  /**
   * Judge screen brightness mode is auto mode?must declare the
   * {@link android.Manifest.permission#WRITE_SETTINGS} permission in its
   * manifest.
   *
   * @param context
   * @return true:auto;false: manual;default:true
   */
  public static boolean isScreenBrightnessModeAuto(Context context) {
    return getScreenBrightnessMode(context) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC ? true : false;
  }

  /**
   * Get screen brightness mode?must declare the
   * {@link android.Manifest.permission#WRITE_SETTINGS} permission in its
   * manifest.
   *
   * @param context
   * @return
   */
  public static int getScreenBrightnessMode(Context context) {
    return Settings.System.getInt(context.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE,
        Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
  }
}

Related Tutorials