android中屏幕亮度相关设置

测试Activity:

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

存取数据工具类:SetAndGetDataUtil

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

屏幕亮度处理工具类:

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

布局文件:

[html]  view plain copy
 
  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.   
  7.     <SeekBar  
  8.         android:id="@+id/main_sb"  
  9.         style="@android:style/Widget.ProgressBar.Horizontal"  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:max="255" />  
  13.   
  14. </LinearLayout>  

 


记得加上权限:

[html]  view plain copy
  1. <uses-permission android:name="android.permission.WRITE_SETTINGS" />  

更多学习资料

猜你喜欢

转载自htmlunit26.iteye.com/blog/1962590
今日推荐