Android中屏幕亮度相关设置

jopen 10年前

测试Activity:

 
import android.app.Activity;    import android.os.Bundle;    import android.provider.Settings;    import android.widget.SeekBar;    import android.widget.SeekBar.OnSeekBarChangeListener;        public class TestScreenBrightnessActivity extends Activity {        private int brightness;        private SeekBar seekbar;            @Override        protected void onCreate(Bundle savedInstanceState) {            super.onCreate(savedInstanceState);            setContentView(R.layout.main);            seekbar = (SeekBar) findViewById(R.id.main_sb);            initBrightness();            seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {                    @Override                public void onStopTrackingTouch(SeekBar seekBar) {                    brightness = seekBar.getProgress();                    seekBar.setProgress(brightness);                    SetAndGetDataUtil.SetData(getApplicationContext(), "test",                            "light", brightness + "");                }                    @Override                public void onStartTrackingTouch(SeekBar seekBar) {                }                    @Override                public void onProgressChanged(SeekBar seekBar, int progress,                        boolean fromUser) {                    int curProgress = seekBar.getProgress();// 得到当前进度值                    // 当进度小于70时,设置成70,防止太黑看不见的情况。                    if (curProgress < 70) {                        curProgress = 70;                    }                    // 根据当前进度改变屏幕亮度                    Settings.System.putInt(getContentResolver(),                            Settings.System.SCREEN_BRIGHTNESS, curProgress);                    curProgress = Settings.System.getInt(getContentResolver(),                            Settings.System.SCREEN_BRIGHTNESS, -1);                    BrightnessUtil.setBrightness(TestScreenBrightnessActivity.this,                            curProgress);                    // BrightnessUtil.saveBrightness(TestScreenBrightnessActivity.this,                    // curProgress);                }            });        }            /**        * 初始化屏幕亮度值        */        private void initBrightness() {            // 如果开启了自动亮度调节,则关闭之            if (BrightnessUtil.isAutoBrightness(this)) {                BrightnessUtil.stopAutoBrightness(this);            }            // 读取文件中设置的亮度值            String light = SetAndGetDataUtil.GetData(this, "test", "light");            if (!"".equals(light)) {                brightness = Integer.valueOf(light);            } else {                brightness = BrightnessUtil.getScreenBrightness(this);            }            BrightnessUtil.setBrightness(this, brightness);            seekbar.setProgress(brightness);        }    }  

存取数据工具类:SetAndGetDataUtil

    package com.home.screenbrightness;                import android.content.Context;        import android.content.SharedPreferences;        import android.content.SharedPreferences.Editor;                /**        * 保存数据的工具类        *         * @author Administrator        *         */        public class SetAndGetDataUtil {            private static SharedPreferences sp;                    @SuppressWarnings("static-access")            public static void SetData(Context context, String filename, String key,                    String value) {                sp = context.getSharedPreferences(filename, context.MODE_PRIVATE);                Editor editor = sp.edit();                editor.putString(key, value);                editor.commit();            }                    @SuppressWarnings("static-access")            public static String GetData(Context context, String filename, String key) {                String value = "";                sp = context.getSharedPreferences(filename, context.MODE_PRIVATE);                value = sp.getString(key, "");                return value;            }        }  
</div> </div>

屏幕亮度处理工具类:

 
import android.app.Activity;    import android.content.ContentResolver;    import android.net.Uri;    import android.provider.Settings;    import android.provider.Settings.SettingNotFoundException;    import android.view.WindowManager;        /**    * 处理屏幕亮度的工具类    *     * @author Administrator    *     */    public class BrightnessUtil {        /**        * 判断是否开启了自动亮度调节        *         * @param activity        * @return        */        public static boolean isAutoBrightness(Activity activity) {            boolean isAutoAdjustBright = false;            try {                isAutoAdjustBright = Settings.System.getInt(                        activity.getContentResolver(),                        Settings.System.SCREEN_BRIGHTNESS_MODE) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;            } catch (SettingNotFoundException e) {                e.printStackTrace();            }            return isAutoAdjustBright;        }            /**        * 获取屏幕的亮度        *         * @param activity        * @return        */        public static int getScreenBrightness(Activity activity) {            int brightnessValue = 0;            try {                brightnessValue = android.provider.Settings.System.getInt(                        activity.getContentResolver(),                        Settings.System.SCREEN_BRIGHTNESS);            } catch (Exception e) {                e.printStackTrace();            }            return brightnessValue;        }            /**        * 设置屏幕亮度        *         * @param activity        * @param brightness        */        public static void setBrightness(Activity activity, int brightness) {            WindowManager.LayoutParams lp = activity.getWindow().getAttributes();            lp.screenBrightness = Float.valueOf(brightness) * (1f / 255f);            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);        }            /**        * 保存亮度设置状态        *         * @param activity        * @param brightness        */        public static void saveBrightness(Activity activity, int brightness) {            Uri uri = android.provider.Settings.System                    .getUriFor("screen_brightness");            ContentResolver resolver = activity.getContentResolver();            android.provider.Settings.System.putInt(resolver, "screen_brightness",                    brightness);            resolver.notifyChange(uri, null);        }        }  

布局文件:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:gravity="center"            android:orientation="vertical" >                    <SeekBar                android:id="@+id/main_sb"                style="@android:style/Widget.ProgressBar.Horizontal"                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:max="255" />                </LinearLayout>  
</div> </div>


记得加上权限:

 
<uses-permission android:name="android.permission.WRITE_SETTINGS" />