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中要加入这句:<uses-permission android:name="android.permission.WRITE_SETTINGS" />


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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值