码迷,mamicode.com
首页 > 移动开发 > 详细

android调节屏幕亮度

时间:2016-07-11 19:00:47      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:

 

Android的屏幕亮度好像在2.1+的时候提供了自动调节的功能, 所以,如果当开启自动调节功能的时候, 我们进行调节好像是没有一点作用的,只有进行判断,看是否开启了屏幕亮度的自动调节功能,如果开启了需要进行关闭。

 

 

package com.vm.common;

import android.app.Activity;
import android.content.ContentResolver;
import android.net.Uri;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.util.Log;
import android.view.WindowManager;


public class BrightnessTools {

    /** * 判断是否开启了自动亮度调节 */

    public static boolean isAutoBrightness(ContentResolver aContentResolver) {
        boolean automicBrightness = false;
        try {
            automicBrightness = Settings.System.getInt(aContentResolver,
            Settings.System.SCREEN_BRIGHTNESS_MODE) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
        }catch (SettingNotFoundException e){
            e.printStackTrace();
        }
        return automicBrightness;
    }

    /**
     * 获取屏幕的亮度
     * @param activity
     * @return
     */
    public static int getScreenBrightness(Activity activity) {
        int nowBrightnessValue = 0;
        ContentResolver resolver = activity.getContentResolver();
        try {
            nowBrightnessValue = android.provider.Settings.System.getInt(
                    resolver, Settings.System.SCREEN_BRIGHTNESS);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return nowBrightnessValue;
    }

    /**
     * 设置亮度
     * @param activity
     * @param brightness
     */
    public static void setBrightness(Activity activity, int brightness) {
        // Settings.System.putInt(activity.getContentResolver(),

        // Settings.System.SCREEN_BRIGHTNESS_MODE,

        // Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);

        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();

        lp.screenBrightness = Float.valueOf(brightness) * (1f / 255f);
        Log.d("lxy", "set  lp.screenBrightness == " + lp.screenBrightness);

        activity.getWindow().setAttributes(lp);
    }

    // 那么,能设置了,但是为什么还是会出现,设置了,没反映呢?

    // 嘿嘿,那是因为,开启了自动调节功能了,那如何关闭呢?这才是最重要的:

    /**
     * 停止自动亮度调节
     * @param activity
     */
    public static void stopAutoBrightness(Activity activity) {

        Settings.System.putInt(activity.getContentResolver(),

        Settings.System.SCREEN_BRIGHTNESS_MODE,

        Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
    }


    /**
     * 开启亮度自动调节 
     * @param activity
     */
    public static void startAutoBrightness(Activity activity) {

        Settings.System.putInt(activity.getContentResolver(),

        Settings.System.SCREEN_BRIGHTNESS_MODE,

        Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
    }

    // 至此,应该说操作亮度的差不多都有了,结束!
    // 哎,本来认为是应该结束了,但是悲剧得是,既然像刚才那样设置的话,只能在当前的activity中有作用,一段退出的时候,会发现毫无作用,悲剧,原来是忘记了保存了。汗!
    /**
     * 保存亮度设置状态
     * @param resolver
     * @param brightness
     */
    public static void saveBrightness(ContentResolver resolver, int brightness) {
        Uri uri = android.provider.Settings.System
                .getUriFor("screen_brightness");

        android.provider.Settings.System.putInt(resolver, "screen_brightness",
                brightness);
        // resolver.registerContentObserver(uri, true, myContentObserver);
        resolver.notifyChange(uri, null);
    }
}

 

测试

 

 //设置应用中的亮度 不保存
    public void Brightness(View view){
        boolean autoBrightness = BrightnessTools.isAutoBrightness(getContentResolver());
        if (autoBrightness) {
            T.showLong(this, "关闭自动调节亮度");
            BrightnessTools.stopAutoBrightness(this);
        }
        BrightnessTools.setBrightness(this, 255);
        T.showLong(this, "亮度已经设置为了最大");
    }
    //获取当前亮度 并设置最大的亮度保存
    public void saveBrightness(View view){
        int screenBrightness = BrightnessTools.getScreenBrightness(this);
        T.showLong(this, "当前屏幕亮度为:"+screenBrightness);
        BrightnessTools.saveBrightness(getContentResolver(), 255);
    }
    //开启关闭自动调节亮度
    public void offAuto(View view){
        boolean autoBrightness = BrightnessTools.isAutoBrightness(getContentResolver());
        if (autoBrightness) {
            T.showLong(this, "关闭自动调节亮度");
            BrightnessTools.stopAutoBrightness(this);
        }else {
            T.showLong(this, "开启自动调节亮度");
            BrightnessTools.startAutoBrightness(this);
        }
    }

 



android调节屏幕亮度

标签:

原文地址:http://www.cnblogs.com/zyw-205520/p/5660991.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!