当前位置:   article > 正文

Android 获取及修改屏幕亮度_android 调节屏幕亮度

android 调节屏幕亮度

前言:

1.获取当前系统屏幕亮度

2.修改app屏幕亮度,仅对当前app有效。-可用在出示二维码界面,需要单个界面提高亮度时使用

3.修改系统屏幕亮度,所有app有效。Android 6.0 以上需要动态申请权限

实现:

1.权限

  1. <uses-permission android:name="android.permission.WRITE_SETTINGS"
  2. tools:ignore="ProtectedPermissions" />

2.动态申请

  1. /**
  2. * 非系统签名应用,引导用户手动授权修改Settings 权限
  3. **/
  4. private static final int REQUEST_CODE_WRITE_SETTINGS = 1000;
  5. @RequiresApi(api = Build.VERSION_CODES.M)
  6. private void initPermission() {
  7. // Settings.System.canWrite(MainActivity.this)
  8. // 检测是否拥有写入系统 Settings 的权限
  9. if (!Settings.System.canWrite(MainActivity5.this)) {
  10. AlertDialog.Builder builder = new AlertDialog.Builder(this);
  11. builder.setTitle("请开启修改屏幕亮度权限");
  12. builder.setMessage("请点击允许开启");
  13. // 拒绝, 无法修改
  14. builder.setNegativeButton("拒绝",
  15. new DialogInterface.OnClickListener() {
  16. @Override
  17. public void onClick(DialogInterface dialog, int which) {
  18. Toast.makeText(MainActivity5.this, "您已拒绝修系统Setting的屏幕亮度权限", Toast.LENGTH_SHORT)
  19. .show();
  20. }
  21. });
  22. builder.setPositiveButton("去开启",
  23. new DialogInterface.OnClickListener() {
  24. @Override
  25. public void onClick(DialogInterface dialog, int which) {
  26. // 打开允许修改Setting 权限的界面
  27. Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS, Uri.parse("package:" + getPackageName()));
  28. startActivityForResult(intent,
  29. REQUEST_CODE_WRITE_SETTINGS);
  30. }
  31. });
  32. builder.setCancelable(false);
  33. builder.show();
  34. }
  35. }
  36. @Override
  37. protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
  38. super.onActivityResult(requestCode, resultCode, data);
  39. if (requestCode == REQUEST_CODE_WRITE_SETTINGS) {
  40. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  41. // Settings.System.canWrite方法检测授权结果
  42. if (Settings.System.canWrite(getApplicationContext())) {
  43. //授权通过
  44. } else {
  45. Toast.makeText(MainActivity5.this, "您已拒绝修系统Setting的屏幕亮度权限",
  46. Toast.LENGTH_SHORT).show();
  47. }
  48. }
  49. }
  50. }

3.获取系统屏幕亮度(如果不需要修改系统屏幕亮度的话不需要申请权限)

  1. /**
  2. * 获取app屏幕亮度
  3. *
  4. * @param activity
  5. * @return
  6. */
  7. public static int getScreenBrightness(Activity activity) {
  8. int value = 0;
  9. ContentResolver cr = activity.getContentResolver();
  10. try {
  11. value = Settings.System.getInt(cr, Settings.System.SCREEN_BRIGHTNESS);
  12. } catch (Settings.SettingNotFoundException e) {
  13. }
  14. return value;
  15. }

4.设置app屏幕亮度(仅对当前app有效,不需要申请权限)

  1. /**
  2. * 设置app屏幕亮度
  3. *
  4. * @param activity
  5. * @param value
  6. */
  7. public static void setScreenBrightness(Activity activity, int value) {
  8. WindowManager.LayoutParams params = activity.getWindow().getAttributes();
  9. params.screenBrightness = value / 255f;
  10. activity.getWindow().setAttributes(params);
  11. }

5.修改系统屏幕亮度(Android 6.0 以上必须动态申请权限)

  1. findViewById(R.id.mBtn_set_system).setOnClickListener(new View.OnClickListener() {
  2. @Override
  3. public void onClick(View view) {
  4. //关闭光感
  5. setScreenManualMode(MainActivity5.this);
  6. //设置系统屏幕亮度
  7. ModifySettingsScreenBrightness(MainActivity5.this, 100);
  8. }
  9. });
  10. /**
  11. * 关闭光感,设置手动调节背光模式
  12. * SCREEN_BRIGHTNESS_MODE_AUTOMATIC 自动调节屏幕亮度模式值为1
  13. * SCREEN_BRIGHTNESS_MODE_MANUAL 手动调节屏幕亮度模式值为0
  14. **/
  15. public void setScreenManualMode(Context context) {
  16. ContentResolver contentResolver = context.getContentResolver();
  17. try {
  18. int mode = Settings.System.getInt(contentResolver, Settings.System.SCREEN_BRIGHTNESS_MODE);
  19. if (mode == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) {
  20. Settings.System.putInt(contentResolver, Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
  21. }
  22. } catch (Settings.SettingNotFoundException e) {
  23. e.printStackTrace();
  24. }
  25. }
  26. /**
  27. * 修改Setting 中屏幕亮度值 亮度 0-255
  28. * 修改Setting的值需要动态申请权限 <uses-permission
  29. * android:name="android.permission.WRITE_SETTINGS"/>
  30. **/
  31. private void ModifySettingsScreenBrightness(Context context, int birghtessValue) {
  32. // 首先需要设置为手动调节屏幕亮度模式
  33. setScreenManualMode(context);
  34. ContentResolver contentResolver = context.getContentResolver();
  35. Settings.System.putInt(contentResolver, Settings.System.SCREEN_BRIGHTNESS, birghtessValue);
  36. }

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

闽ICP备14008679号