android调节屏幕亮度

首先 要知道当前设备是否处于自动调节的状态:

public static boolean isAutoBrightness(ContentResolver aContentResolver) {      
  
    boolean automicBrightness = false;      
  
    try{          
  
    automicBrightness = Settings.System.getInt(aContentResolver,                  
  
    Settings.System.SCREEN_BRIGHTNESS_MODE) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;     
  
     }   
  
    catch(SettingNotFoundException e)   
  
    {         
  
     e.printStackTrace();    
  
      }      
  
    return automicBrightness;  
    }  
随后,需要获取当前屏幕亮度,用于绑定给自定义的seekbar:

    public static int getScreenBrightness(Activity activity) {     
  
    int nowBrightnessValue = 0;      
  
    ContentResolver resolver = activity.getContentResolver();      
  
    try{          
  
    nowBrightnessValue = android.provider.Settings.System.getInt(resolver, Settings.System.SCREEN_BRIGHTNESS);    
  
      }  
  
     catch(Exception e) {         
  
     e.printStackTrace();    
  
      }      
  
    return nowBrightnessValue;  
    } 

紧接着 ,就需要设置屏幕亮度:

public static void setBrightness(Activity activity, int brightness) {     
  
  WindowManager.LayoutParams lp = activity.getWindow().getAttributes();     
  
     lp.screenBrightness = Float.valueOf(brightness) * (1f / 255f);    
<span style="white-space:pre">	</span>   activity.getWindow().setAttributes(lp);   
     }  
有的时候设置了并没有生效,那是因为没有关闭自动调节功能,下面的方法能够对自动调节进行关闭:

  public static void stopAutoBrightness(Activity activity) {     
  
     Settings.System.putInt(activity.getContentResolver(),            
  
      Settings.System.SCREEN_BRIGHTNESS_MODE,             
  
     Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);  
     }                       //最后一个参数设置为  <span style="font-family: Consolas, 'Courier New', Courier, mono, serif; line-height: 18px;">Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC的时候是开启自动调节。</span>
当然,设置了之后还需要对设置的值进行保存:

   public static void saveBrightness(ContentResolver resolver, int brightness) {      
  
    Uri uri = android.provider.Settings.System.getUriFor("screen_brightness");     
  
     android.provider.Settings.System.putInt(resolver, "screen_brightness", brightness);      
  
    // resolver.registerContentObserver(uri, true, myContentObserver);     
  
     resolver.notifyChange(uri, null);}  

如果要用seekbar实现  那就自定义一个seekbar的监听器

	class myOnSeekBarChangeListener implements OnSeekBarChangeListener{

			@Override
	    	public void onStopTrackingTouch(SeekBar seekBar) {

				 int tmpInt = seekBar.getProgress();  
				  
	                // 当进度小于80时,设置成80,防止太黑看不见的后果。  
	                if (tmpInt < 80) {  
	                    tmpInt = 80;  
	                }  
	  
	                // 根据当前进度改变亮度  
	                Settings.System.putInt(getActivity().getContentResolver(),  
	                        Settings.System.SCREEN_BRIGHTNESS, tmpInt);  
	                tmpInt = Settings.System.getInt(getActivity().getContentResolver(),  
	                        Settings.System.SCREEN_BRIGHTNESS, -1);  
	                WindowManager.LayoutParams wl = getActivity().getWindow().getAttributes();  
	  
	                float tmpFloat = (float) tmpInt / 255;  
	                if (tmpFloat > 0 && tmpFloat <= 1) {  
	                    wl.screenBrightness = tmpFloat;  
	                }  
	                getActivity().getWindow().setAttributes(wl);  
	    	}
	    	
	    	@Override
	    	public void onStartTrackingTouch(SeekBar seekBar) {
	    		// TODO Auto-generated method stub
	    		
	    	}
	    	
	    	@Override
	    	public void onProgressChanged(SeekBar seekBar, int progress,
	    			boolean fromUser) {
	    		
	    	}
			
		}






你可能感兴趣的:(android调节屏幕亮度)