首页 > 移动开发 > android中屏幕亮度相关设置

android中屏幕亮度相关设置

测试Activity:

[java][/java] view plaincopy

  1. package com.home.screenbrightness;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.provider.Settings;
  5. import android.widget.SeekBar;
  6. import android.widget.SeekBar.OnSeekBarChangeListener;
  7. public class TestScreenBrightnessActivity extends Activity {
  8.     private int brightness;
  9.     private SeekBar seekbar;
  10.     @Override
  11.     protected void onCreate(Bundle savedInstanceState) {
  12.         super.onCreate(savedInstanceState);
  13.         setContentView(R.layout.main);
  14.         seekbar = (SeekBar) findViewById(R.id.main_sb);
  15.         initBrightness();
  16.         seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
  17.             @Override
  18.             public void onStopTrackingTouch(SeekBar seekBar) {
  19.                 brightness = seekBar.getProgress();
  20.                 seekBar.setProgress(brightness);
  21.                 SetAndGetDataUtil.SetData(getApplicationContext(), "test",
  22.                         "light", brightness + "");
  23.             }
  24.             @Override
  25.             public void onStartTrackingTouch(SeekBar seekBar) {
  26.             }
  27.             @Override
  28.             public void onProgressChanged(SeekBar seekBar, int progress,
  29.                     boolean fromUser) {
  30.                 int curProgress = seekBar.getProgress();// 得到当前进度值
  31.                 // 当进度小于70时,设置成70,防止太黑看不见的情况。
  32.                 if (curProgress < 70) {
  33.                     curProgress = 70;
  34.                 }
  35.                 // 根据当前进度改变屏幕亮度
  36.                 Settings.System.putInt(getContentResolver(),
  37.                         Settings.System.SCREEN_BRIGHTNESS, curProgress);
  38.                 curProgress = Settings.System.getInt(getContentResolver(),
  39.                         Settings.System.SCREEN_BRIGHTNESS, -1);
  40.                 BrightnessUtil.setBrightness(TestScreenBrightnessActivity.this,
  41.                         curProgress);
  42.                 // BrightnessUtil.saveBrightness(TestScreenBrightnessActivity.this,
  43.                 // curProgress);
  44.             }
  45.         });
  46.     }
  47.     /**
  48.      * 初始化屏幕亮度值
  49.      */
  50.     private void initBrightness() {
  51.         // 如果开启了自动亮度调节,则关闭之
  52.         if (BrightnessUtil.isAutoBrightness(this)) {
  53.             BrightnessUtil.stopAutoBrightness(this);
  54.         }
  55.         // 读取文件中设置的亮度值
  56.         String light = SetAndGetDataUtil.GetData(this, "test", "light");
  57.         if (!"".equals(light)) {
  58.             brightness = Integer.valueOf(light);
  59.         } else {
  60.             brightness = BrightnessUtil.getScreenBrightness(this);
  61.         }
  62.         BrightnessUtil.setBrightness(this, brightness);
  63.         seekbar.setProgress(brightness);
  64.     }
  65. }

存取数据工具类:SetAndGetDataUtil

[java][/java] view plaincopy

  1. package com.home.screenbrightness;
  2. import android.content.Context;
  3. import android.content.SharedPreferences;
  4. import android.content.SharedPreferences.Editor;
  5. /**
  6.  * 保存数据的工具类
  7.  *
  8.  * @author Administrator
  9.  *
  10.  */
  11. public class SetAndGetDataUtil {
  12.     private static SharedPreferences sp;
  13.     @SuppressWarnings("static-access")
  14.     public static void SetData(Context context, String filename, String key,
  15.             String value) {
  16.         sp = context.getSharedPreferences(filename, context.MODE_PRIVATE);
  17.         Editor editor = sp.edit();
  18.         editor.putString(key, value);
  19.         editor.commit();
  20.     }
  21.     @SuppressWarnings("static-access")
  22.     public static String GetData(Context context, String filename, String key) {
  23.         String value = "";
  24.         sp = context.getSharedPreferences(filename, context.MODE_PRIVATE);
  25.         value = sp.getString(key, "");
  26.         return value;
  27.     }
  28. }

屏幕亮度处理工具类:

[java][/java] view plaincopy

  1. package com.home.screenbrightness;
  2. import android.app.Activity;
  3. import android.content.ContentResolver;
  4. import android.net.Uri;
  5. import android.provider.Settings;
  6. import android.provider.Settings.SettingNotFoundException;
  7. import android.view.WindowManager;
  8. /**
  9.  * 处理屏幕亮度的工具类
  10.  *
  11.  * @author Administrator
  12.  *
  13.  */
  14. public class BrightnessUtil {
  15.     /**
  16.      * 判断是否开启了自动亮度调节
  17.      *
  18.      * @param activity
  19.      * @return
  20.      */
  21.     public static boolean isAutoBrightness(Activity activity) {
  22.         boolean isAutoAdjustBright = false;
  23.         try {
  24.             isAutoAdjustBright = Settings.System.getInt(
  25.                     activity.getContentResolver(),
  26.                     Settings.System.SCREEN_BRIGHTNESS_MODE) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
  27.         } catch (SettingNotFoundException e) {
  28.             e.printStackTrace();
  29.         }
  30.         return isAutoAdjustBright;
  31.     }
  32.     /**
  33.      * 获取屏幕的亮度
  34.      *
  35.      * @param activity
  36.      * @return
  37.      */
  38.     public static int getScreenBrightness(Activity activity) {
  39.         int brightnessValue = 0;
  40.         try {
  41.             brightnessValue = android.provider.Settings.System.getInt(
  42.                     activity.getContentResolver(),
  43.                     Settings.System.SCREEN_BRIGHTNESS);
  44.         } catch (Exception e) {
  45.             e.printStackTrace();
  46.         }
  47.         return brightnessValue;
  48.     }
  49.     /**
  50.      * 设置屏幕亮度
  51.      *
  52.      * @param activity
  53.      * @param brightness
  54.      */
  55.     public static void setBrightness(Activity activity, int brightness) {
  56.         WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
  57.         lp.screenBrightness = Float.valueOf(brightness) * (1f / 255f);
  58.         activity.getWindow().setAttributes(lp);
  59.     }
  60.     /**
  61.      * 关闭亮度自动调节
  62.      *
  63.      * @param activity
  64.      */
  65.     public static void stopAutoBrightness(Activity activity) {
  66.         Settings.System.putInt(activity.getContentResolver(),
  67.                 Settings.System.SCREEN_BRIGHTNESS_MODE,
  68.                 Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
  69.     }
  70.     /**
  71.      * 开启亮度自动调节
  72.      *
  73.      * @param activity
  74.      */
  75.     public static void startAutoBrightness(Activity activity) {
  76.         Settings.System.putInt(activity.getContentResolver(),
  77.                 Settings.System.SCREEN_BRIGHTNESS_MODE,
  78.                 Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
  79.     }
  80.     /**
  81.      * 保存亮度设置状态
  82.      *
  83.      * @param activity
  84.      * @param brightness
  85.      */
  86.     public static void saveBrightness(Activity activity, int brightness) {
  87.         Uri uri = android.provider.Settings.System
  88.                 .getUriFor("screen_brightness");
  89.         ContentResolver resolver = activity.getContentResolver();
  90.         android.provider.Settings.System.putInt(resolver, "screen_brightness",
  91.                 brightness);
  92.         resolver.notifyChange(uri, null);
  93.     }
  94. }

布局文件:

[html][/html] view plaincopy

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.     android:layout_width="match_parent"
  3.     android:layout_height="match_parent"
  4.     android:gravity="center"
  5.     android:orientation="vertical" >
  6.     <SeekBar
  7.         android:id="@+id/main_sb"
  8.         style="@android:style/Widget.ProgressBar.Horizontal"
  9.         android:layout_width="match_parent"
  10.         android:layout_height="wrap_content"
  11.         android:max="255" />
  12. </LinearLayout>

记得加上权限:

[html][/html] view plaincopy

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

本文固定链接: http://www.devba.com/index.php/archives/3184.html | 开发吧

报歉!评论已关闭.