服务器之家

服务器之家 > 正文

Android调节屏幕亮度实现代码

时间:2021-03-19 15:03     来源/作者:安卓之家

调节屏幕亮度时,先设置当前activity亮度,再并保存为系统亮度即可。

1 onCreate()里检查系统亮度并设置seekBar:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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亮度,并保存为系统亮度:
        

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//屏幕亮度
     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);
     }
标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部