Android调节屏幕亮度实现代码
程序员文章站
2022-07-03 18:56:19
调节屏幕亮度时,先设置当前activity亮度,再并保存为系统亮度即可。
1 oncreate()里检查系统亮度并设置seekbar:
private...
调节屏幕亮度时,先设置当前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); }