当前位置:   article > 正文

Android 设置屏幕亮度调节源代码_android开发修改屏幕亮度

android开发修改屏幕亮度

public class SetUpActivity extends AppCompatActivity {

private TextView btnReturnTrend;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_set_up);
    if (Build.VERSION.SDK_INT >= 21) {
        View decorView = getWindow().getDecorView();
        int option = View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
        decorView.setSystemUiVisibility(option);
        getWindow().setNavigationBarColor(Color.TRANSPARENT);
        getWindow().setStatusBarColor(Color.TRANSPARENT);
    }

    btnReturnTrend = findViewById(R.id.btnReturn);
    btnReturnTrend.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivity(new Intent(SetUpActivity.this,MainActivity.class));
            finish();
        }
    });


    SeekBar seekBar = findViewById(R.id.seekBar);

    // 进度条绑定最大亮度,255是最大亮度
    seekBar.setMax(255);
    // 取得当前亮度
    int normal = Settings.System.getInt(getContentResolver(),
            Settings.System.SCREEN_BRIGHTNESS, 255);
    // 进度条绑定当前亮度
    seekBar.setProgress(normal);

    seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {


            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                //判断是否可以写入数据到系统
                if (!Settings.System.canWrite(SetUpActivity.this)) {
                 /*   Intent i = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
                    i.setData(Uri.parse("package:" + SetUpActivity.this.getPackageName()));
                    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    SetUpActivity.this.startActivity(i);*/
                    Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_WRITE_SETTINGS);
                    intent.setData(Uri.parse("package:" + getApplicationContext().getPackageName()));
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    getApplicationContext().startActivity(intent);
                } else {

                    //处理逻辑
                    // 取得当前进度
                    int tmpInt = seekBar.getProgress();

                    // 当进度小于80时,设置成80,防止太黑看不见的后果。
                    if (tmpInt < 80) {
                        tmpInt = 80;
                    }

                    // 根据当前进度改变亮度
                    Settings.System.putInt(getContentResolver(),
                            Settings.System.SCREEN_BRIGHTNESS, tmpInt);
                    tmpInt = Settings.System.getInt(getContentResolver(),
                            Settings.System.SCREEN_BRIGHTNESS, -1);
                    WindowManager.LayoutParams wl = getWindow().getAttributes();

                    float tmpFloat = (float) tmpInt / 255;
                    if (tmpFloat > 0 && tmpFloat <= 1) {
                        wl.screenBrightness = tmpFloat;
                    }
                    getWindow().setAttributes(wl);
                }
            }

        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress,
                                      boolean fromUser) {
            // TODO Auto-generated method stub
        }
    });
}
  • 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
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93

}
在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/713801
推荐阅读
相关标签
  

闽ICP备14008679号