2014-01-30 144 views
2

在我的應用程序中,我添加了一個控制屏幕亮度的SeekBar。它工作一些很好。 我在Oncreate()中獲取設備的屏幕亮度,並設置SeekBar的進度。屏幕亮度seekbar無法正常工作

但是,當我改變seekbar的進度,當我把它設置爲最小(即到零位)設備卡住了,我不得不重新啓動我的設備,讓它再次工作。

什麼是與我的代碼綁定。請幫我

oncreate()我已經

brightnessscrollbar.setOnSeekBarChangeListener(

      new OnSeekBarChangeListener() { 

    @Override 

    public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) { 
     // TODO Auto-generated method stub 

    BackLightValue = (int) ((float)arg1/100);   
     WindowManager.LayoutParams layoutParams = getWindow().getAttributes(); 
     layoutParams.screenBrightness = BackLightValue; 
     getWindow().setAttributes(layoutParams); 
    } 
    @Override 
    public void onStartTrackingTouch(SeekBar arg0) { 
    } 
    @Override 
    public void onStopTrackingTouch(SeekBar arg0) { 

     int SysBackLightValue = (int)(BackLightValue * 255); 
     android.provider.Settings.System.putInt(getContentResolver(), 
       android.provider.Settings.System.SCREEN_BRIGHTNESS, 
       SysBackLightValue); 
     } 
}); 

回答

4

我才知道,設備下面的代碼,

  try { 
      BackLightValue = android.provider.Settings.System.getInt(getContentResolver(),Settings.System.SCREEN_BRIGHTNESS); 

      brightnessscrollbar.setProgress(BackLightValue); 

      System.out.println("Baclightvalue is "+BackLightValue); 


     } catch (SettingNotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

seekbarclicklistener卡在零

所以我給了一個價值onProgresschanged的條件如下所示

public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) { 
    // TODO Auto-generated method stub 

BackLightValue = (int) ((float)arg1/100);  

    if(arg1>5){ 
    WindowManager.LayoutParams layoutParams = getWindow().getAttributes(); 
    layoutParams.screenBrightness = BackLightValue; 
    getWindow().setAttributes(layoutParams); 

    } 
} 

現在,它就像一個魅力。

0
 public void onProgressChanged(SeekBar seekBar, int progressValue, boolean fromUser) { 
      progress = progressValue; 
      if(progress <1) { 

       progress = 1; 

      } 
      android.provider.Settings.System.putInt(getActivity().getContentResolver(), android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE, android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL); 

      android.provider.Settings.System.putInt(getActivity().getContentResolver(), 
        android.provider.Settings.System.SCREEN_BRIGHTNESS, 
        progress); 


      WindowManager.LayoutParams layoutParams = window.getAttributes(); 
      layoutParams.screenBrightness = progress/(float)255; 
      window.setAttributes(layoutParams); 
     }