android系统设置-修改睡眠时间,修改背光强度

1.设置睡眠时间的例子

protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
		
	//当睡眠值为-1的时候,则不睡眠。 输入的单位为毫秒
	super.onCreate(savedInstanceState);
	try {
	        SharedPreferences settings=getSharedPreferences("net.mensus.togglescreen", MODE_PRIVATE);
	        ContentResolver c=getContentResolver();
	        int val=android.provider.Settings.System.getInt(getContentResolver(), SCREEN_OFF_TIMEOUT);
	        if(val==-1 || val==0) {
	            int nval=settings.getInt("ScreenTimeout",60000);
	            if(nval<=0) nval=60000;
	            String t;
	            if(nval==30000) t="1/2 min";
	            else if(nval>=60000) t=""+(nval/60000)+" min";
	            else t=""+(nval/1000)+" sec";
	            Toast.makeText(this, "Screen Timeout set to "+ t,  Toast.LENGTH_LONG).show();
	            android.provider.Settings.System.putInt(getContentResolver(), SCREEN_OFF_TIMEOUT, nval);
	        } else {
	            android.provider.Settings.System.putInt(getContentResolver(), SCREEN_OFF_TIMEOUT, -1);
	            Toast.makeText(this, "Disabled Screen Timeout", Toast.LENGTH_LONG).show();
	            SharedPreferences.Editor editor = settings.edit();
	            editor.putInt("ScreenTimeout",val);
	            editor.commit();
	        }
	} catch(Throwable er) {
	    Toast.makeText(this, "Error "+er.getMessage(), Toast.LENGTH_LONG).show();
	} finally {
	    finish();
	}
}


int val=android.provider.Settings.System.getInt(getContentResolver(), SCREEN_OFF_TIMEOUT);
获得系统的睡眠时间,单位是毫秒
android.provider.Settings.System.putInt(getContentResolver(), SCREEN_OFF_TIMEOUT, nval);
设置系统的睡眠时间。

注意:在manifest中要加入这句:


2.设置系统背光强度

    private final int MAXIMUM_BACKLIGHT = 255;
    private final int MINIMUM_BACKLIGHT = 10;
// 设置自动背光
    private void  setAutoBright(boolean mode) {
      if (mode) {
        Settings.System.putInt(mainActivity.getContentResolver(), System.SCREEN_BRIGHTNESS_MODE, System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
        //autoOnOrOff.setText(String.valueOf(getAutoBrightnessMode()));
      } else {
        Settings.System.putInt(mainActivity.getContentResolver(), System.SCREEN_BRIGHTNESS_MODE, System.SCREEN_BRIGHTNESS_MODE_MANUAL);
        ///autoOnOrOff.setText(String.valueOf(getAutoBrightnessMode()));
      }
      
      java.lang.System.out.println("test" +  + getAutoBrightnessMode());
    }
    
    //判断是否自动背光  返回值为0表示false 1表示true
    private int getAutoBrightnessMode() {
        int brightnessMode; 
        
        try {
           brightnessMode = Settings.System.getInt(mainActivity.getContentResolver(), System.SCREEN_BRIGHTNESS_MODE);
        } catch (Settings.SettingNotFoundException e) {
           e.printStackTrace();
           brightnessMode = -10000;
        }
       return brightnessMode;
      }
    
    //刷新背光  如果参数为小于零,则为自动背光
    private void refreshBrightness(float brightness) {
        WindowManager.LayoutParams lp = mainActivity.getWindow().getAttributes();
        if (brightness < 0) {
            lp.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;
        } else {
            lp.screenBrightness = brightness;
        }
        mainActivity.getWindow().setAttributes(lp);
        
        setBrightness(brightness < 0 ? WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE : brightness);
    }

    private int getBrightnessLevel() {
        try {
            int value = Settings.System.getInt(mainActivity.getContentResolver(), System.SCREEN_BRIGHTNESS);
            return value;
        } catch (SettingNotFoundException e) {
            return MAXIMUM_BACKLIGHT;
        }
    }


参考网址:  http://www.mensus.net/phone/toggle.shtml

你可能感兴趣的:(ANDROID)