Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發實例 >> Adnroid屏幕亮度調節之設置到指定的值與恢復到原來的值示例

Adnroid屏幕亮度調節之設置到指定的值與恢復到原來的值示例

編輯:Android開發實例

應用場景:手機上有一張條形碼圖片,需要用掃碼設備掃描。當手機屏幕太暗時,識別度低,因此我就想當切換到條形碼圖片所在的界面時,調亮屏幕方便掃碼設備識別。當離開該界面時恢復用戶原來設置的屏幕亮度調節模式和屏幕亮度值。

一、存儲當前屏幕亮度調節模式值和屏幕亮度值:

 

  1. /*  
  2.                     * 獲得當前屏幕亮度的模式   
  3.                     * SCREEN_BRIGHTNESS_MODE_AUTOMATIC=1 為自動調節屏幕亮度  
  4.                     * SCREEN_BRIGHTNESS_MODE_MANUAL=0 為手動調節屏幕亮度  
  5.                     */ 
  6.                     screenMode = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE);  
  7.                     Log.i(TAG, "screenMode = " + screenMode);  
  8.                       
  9.                     // 獲得當前屏幕亮度值 0--255  
  10.                     screenBrightness = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);  
  11.                     Log.i(TAG, "screenBrightness = " + screenBrightness);  


二、改變當前屏幕亮度調節模式值和屏幕亮度值:

  1. // 如果當前的屏幕亮度調節調節模式為自動調節,則改為手動調節屏幕亮度  
  2.                     if(screenMode == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC){  
  3.                         setScreenMode(Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);  
  4.                     }  
  5.                       
  6.                     // 設置屏幕亮度值為最大值255  
  7.                     setScreenBrightness(255.0F);  


三、離開時恢復當前屏幕亮度調節模式值和屏幕亮度值:

  1. // 設置屏幕亮度值為原來的  
  2.                   setScreenBrightness(screenBrightness);  
  3.                   // 設置當前屏幕亮度的模式 為原來的  
  4.                   setScreenMode(screenMode);  

四、所需權限:

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

五、完整代碼:

  1. package com.easipass.setting.screen.luminance;  
  2.  
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.provider.Settings;  
  6. import android.provider.Settings.SettingNotFoundException;  
  7. import android.util.Log;  
  8. import android.view.View;  
  9. import android.view.Window;  
  10. import android.view.WindowManager;  
  11.  
  12. /**  
  13.  * 功能描述:設置與恢復當前的屏幕亮度調節模式和屏幕亮度值  
  14.  *   
  15.  * @author android_ls  
  16.  */ 
  17. public class SettingScreenLuminanceActivity extends Activity {  
  18.  
  19.     private static final String TAG = "ScreenLuminance";  
  20.  
  21.     /**  
  22.      * 當前屏幕亮度的模式   
  23.      */ 
  24.     private int screenMode;  
  25.  
  26.     /**  
  27.      * 當前屏幕亮度值 0--255  
  28.      */ 
  29.     private int screenBrightness;  
  30.  
  31.     @Override 
  32.     public void onCreate(Bundle savedInstanceState) {  
  33.         super.onCreate(savedInstanceState);  
  34.         setContentView(R.layout.main);  
  35.  
  36.         // 調亮屏幕到指定的值  
  37.         super.findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {  
  38.  
  39.             @Override 
  40.             public void onClick(View v) {  
  41.  
  42.                 try {  
  43.                     /*  
  44.                      * 獲得當前屏幕亮度的模式   
  45.                      * SCREEN_BRIGHTNESS_MODE_AUTOMATIC=1 為自動調節屏幕亮度  
  46.                      * SCREEN_BRIGHTNESS_MODE_MANUAL=0 為手動調節屏幕亮度  
  47.                      */ 
  48.                     screenMode = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE);  
  49.                     Log.i(TAG, "screenMode = " + screenMode);  
  50.  
  51.                     // 獲得當前屏幕亮度值 0--255  
  52.                     screenBrightness = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);  
  53.                     Log.i(TAG, "screenBrightness = " + screenBrightness);  
  54.  
  55.                     // 如果當前的屏幕亮度調節調節模式為自動調節,則改為手動調節屏幕亮度  
  56.                     if (screenMode == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) {  
  57.                         setScreenMode(Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);  
  58.                     }  
  59.  
  60.                     // 設置屏幕亮度值為最大值255  
  61.                     setScreenBrightness(255.0F);  
  62.  
  63.                 } catch (SettingNotFoundException e) {  
  64.                     // TODO Auto-generated catch block  
  65.                     e.printStackTrace();  
  66.                 }  
  67.  
  68.             }  
  69.         });  
  70.  
  71.         // 恢復原來的亮度  
  72.         super.findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {  
  73.  
  74.             @Override 
  75.             public void onClick(View v) {  
  76.                 // 設置屏幕亮度值為原來的  
  77.                 setScreenBrightness(screenBrightness);  
  78.                 // 設置當前屏幕亮度的模式 為原來的  
  79.                 setScreenMode(screenMode);  
  80.             }  
  81.         });  
  82.  
  83.     }  
  84.  
  85.     /**  
  86.      * 設置當前屏幕亮度的模式   
  87.     * SCREEN_BRIGHTNESS_MODE_AUTOMATIC=1 為自動調節屏幕亮度  
  88.     * SCREEN_BRIGHTNESS_MODE_MANUAL=0 為手動調節屏幕亮度  
  89.     */ 
  90.     private void setScreenMode(int value) {  
  91.         Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, value);  
  92.     }  
  93.  
  94.     /**  
  95.     * 設置當前屏幕亮度值 0--255,並使之生效  
  96.     */ 
  97.     private void setScreenBrightness(float value) {  
  98.         Window mWindow = getWindow();  
  99.         WindowManager.LayoutParams mParams = mWindow.getAttributes();  
  100.         float f = value / 255.0F;  
  101.         mParams.screenBrightness = f;  
  102.         mWindow.setAttributes(mParams);  
  103.  
  104.         // 保存設置的屏幕亮度值  
  105.         Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, (int) value);  
  106.     }  
  107.  
  108. }  
  109.  

 另外特別提示:直接用 getWindow().setAttributes(WindowManager.LayoutParams) 可以只改變當前窗口的亮度,不影響系統設置。

轉自:http://blog.csdn.net/android_ls/article/details/8678313

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved