Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android調節屏幕亮度實現代碼

Android調節屏幕亮度實現代碼

編輯:關於Android編程

調節屏幕亮度時,先設置當前activity亮度,再並保存為系統亮度即可。

1 onCreate()裡檢查系統亮度並設置seekBar:

     private void screenBrightness_check()
     {
          //先關閉系統的亮度自動調節
          try
          {
              if(android.provider.Settings.System.getInt(getContentResolver(),android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE) == android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC)
              {    
                   android.provider.Settings.System.putInt(getContentResolver(),
                        android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE,
                        android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
              }
          }
          catch (SettingNotFoundException e)
          {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
          //獲取當前亮度,獲取失敗則返回255
          intScreenBrightness=(int)(android.provider.Settings.System.getInt(getContentResolver(),
                   android.provider.Settings.System.SCREEN_BRIGHTNESS,
                   255));
          //文本、進度條顯示
          mSeekBar_light.setProgress(intScreenBrightness);
          mTextView_light.setText(""+intScreenBrightness*100/255);
         
     }


2 拖動seekBar時設置當前activity亮度,並保存為系統亮度:
        

//屏幕亮度
     private void setScreenBritness(int brightness)
     {
          //不讓屏幕全暗
          if(brightness<=1)
          {
              brightness=1;
          }
          //設置當前activity的屏幕亮度
          WindowManager.LayoutParams lp = this.getWindow().getAttributes();
          //0到1,調整亮度暗到全亮
          lp.screenBrightness = Float.valueOf(brightness/255f); 
          this.getWindow().setAttributes(lp);
    
          //保存為系統亮度方法1
          android.provider.Settings.System.putInt(getContentResolver(),
                   android.provider.Settings.System.SCREEN_BRIGHTNESS,
                   brightness);
         
          //保存為系統亮度方法2
//        Uri uri = android.provider.Settings.System.getUriFor("screen_brightness"); 
//        android.provider.Settings.System.putInt(getContentResolver(), "screen_brightness", brightness);  
//        // resolver.registerContentObserver(uri, true, myContentObserver); 
//        getContentResolver().notifyChange(uri, null);
         
          //更改亮度文本顯示
          mTextView_light.setText(""+brightness*100/255);
     }

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