服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android调节屏幕亮度实现代码

Android调节屏幕亮度实现代码

2021-03-19 15:03安卓之家 Android

这篇文章主要介绍了Android调节屏幕亮度实现代码,调节屏幕亮度时,先设置当前activity亮度,再并保存为系统亮度即可,本文分别给出两个步骤的实现代码,需要的朋友可以参考下

调节屏幕亮度时,先设置当前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);
     }

延伸 · 阅读

精彩推荐