当前位置:   article > 正文

Android 修改屏幕亮度方案_android settings.system.putint设置亮度无效

android settings.system.putint设置亮度无效

本篇文章主要介绍 Android 开发中的部分知识点,通过阅读本篇文章,您将收获以下内容:

一、获取系统Settings 中的亮度
二、修改APP界面屏幕亮度,不会影响其他APP
三、修改系统Settings 中屏幕亮度,影响所有APP
四、完整代码实现


一、获取系统Settings 中的亮度

系统屏幕亮度值在(0~255)之间,获取方法很简单,只需要调用以下方法即可。

  1. /**
  2. * 1.获取系统默认屏幕亮度值 屏幕亮度值范围(0-255)
  3. * **/
  4. private int getScreenBrightness(Context context) {
  5. ContentResolver contentResolver = context.getContentResolver();
  6. int defVal = 125;
  7. return Settings.System.getInt(contentResolver,
  8. Settings.System.SCREEN_BRIGHTNESS, defVal);
  9. }

修改屏幕亮度包含两种:
1.修改APP界面屏幕亮度,不会影响其他APP
2.修改系统 Settings 中屏幕亮度,影响所有APP。


二,修改APP界面屏幕亮度,不会影响其他APP

修改自身 APP 亮度很简单,只需要在Activity OnCreate方法调用如下代码即可。

  1. /**
  2. * 2.设置 APP界面屏幕亮度值方法
  3. * **/
  4. private void setAppScreenBrightness(int birghtessValue) {
  5. Window window = getWindow();
  6. WindowManager.LayoutParams lp = window.getAttributes();
  7. lp.screenBrightness = birghtessValue / 255.0f;
  8. window.setAttributes(lp);
  9. }

三、修改系统Settings 中屏幕亮度,影响所有APP

修改系统 Settings 中的屏幕亮度,由于会影响到所有APP,需要申请修改Settings的权限<uses-permission * android:name="android.permission.WRITE_SETTINGS"/>,同时需要取消光感自动调节屏幕亮度的功能,设置为手动调节模式,否则光感传感器会随着光照强度的变化修改系统屏幕亮度,并且非系统签名的APP,需要用户手动授权后才可以修改背光亮度。

关闭光感,设置手动调节背光模式实现方法如下:

  1. /**
  2. * 3.关闭光感,设置手动调节背光模式
  3. *
  4. * SCREEN_BRIGHTNESS_MODE_AUTOMATIC 自动调节屏幕亮度模式值为1
  5. *
  6. * SCREEN_BRIGHTNESS_MODE_MANUAL 手动调节屏幕亮度模式值为0
  7. * **/
  8. public void setScreenManualMode(Context context) {
  9. ContentResolver contentResolver = context.getContentResolver();
  10. try {
  11. int mode = Settings.System.getInt(contentResolver,
  12. Settings.System.SCREEN_BRIGHTNESS_MODE);
  13. if (mode == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) {
  14. Settings.System.putInt(contentResolver,
  15. Settings.System.SCREEN_BRIGHTNESS_MODE,
  16. Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
  17. }
  18. } catch (Settings.SettingNotFoundException e) {
  19. e.printStackTrace();
  20. }
  21. }

非系统签名应用,引导用户手动授权修改Settings 权限

非系统签名应用,无法直接修改Settings,需要引导用户手动授权。

  1. /**
  2. * 4.非系统签名应用,引导用户手动授权修改Settings 权限
  3. * **/
  4. private static final int REQUEST_CODE_WRITE_SETTINGS = 1000;
  5. private void allowModifySettings() {
  6. // Settings.System.canWrite(MainActivity.this)
  7. // 检测是否拥有写入系统 Settings 的权限
  8. if (!Settings.System.canWrite(MainActivity.this)) {
  9. AlertDialog.Builder builder = new AlertDialog.Builder(this,
  10. android.R.style.Theme_Material_Light_Dialog_Alert);
  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(MainActivity.this,
  19. "您已拒绝修系统Setting的屏幕亮度权限", Toast.LENGTH_SHORT)
  20. .show();
  21. }
  22. });
  23. builder.setPositiveButton("去开启",
  24. new DialogInterface.OnClickListener() {
  25. @Override
  26. public void onClick(DialogInterface dialog, int which) {
  27. // 打开允许修改Setting 权限的界面
  28. Intent intent = new Intent(
  29. Settings.ACTION_MANAGE_WRITE_SETTINGS, Uri
  30. .parse("package:"
  31. + getPackageName()));
  32. startActivityForResult(intent,
  33. REQUEST_CODE_WRITE_SETTINGS);
  34. }
  35. });
  36. builder.setCancelable(false);
  37. builder.show();
  38. }
  39. }
  40. @Override
  41. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  42. // TODO Auto-generated method stub
  43. super.onActivityResult(requestCode, resultCode, data);
  44. if (requestCode == REQUEST_CODE_WRITE_SETTINGS) {
  45. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  46. // Settings.System.canWrite方法检测授权结果
  47. if (Settings.System.canWrite(getApplicationContext())) {
  48. // 5.调用修改Settings屏幕亮度的方法 屏幕亮度值 200
  49. ModifySettingsScreenBrightness(MainActivity.this, 200);
  50. Toast.makeText(this, "系统屏幕亮度值" + getScreenBrightness(this),
  51. Toast.LENGTH_SHORT).show();
  52. } else {
  53. Toast.makeText(MainActivity.this, "您已拒绝修系统Setting的屏幕亮度权限",
  54. Toast.LENGTH_SHORT).show();
  55. }
  56. }
  57. }
  58. }

修改Setting 中屏幕亮度值 实现

拥有系统签名的应用可以直接调用此方法修改系统屏幕亮度,非系统签名应用,只有用户授权后才可以修改。

  1. /**
  2. * 5.修改Setting 中屏幕亮度值
  3. *
  4. * 修改Setting的值需要动态申请权限 <uses-permission
  5. * android:name="android.permission.WRITE_SETTINGS"/>
  6. * **/
  7. private void ModifySettingsScreenBrightness(Context context,
  8. int birghtessValue) {
  9. // 首先需要设置为手动调节屏幕亮度模式
  10. setScreenManualMode(context);
  11. ContentResolver contentResolver = context.getContentResolver();
  12. Settings.System.putInt(contentResolver,
  13. Settings.System.SCREEN_BRIGHTNESS, birghtessValue);
  14. }

四、完整代码实现

  1. public class MainActivity extends Activity {
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. //获取屏幕亮度
  6. getScreenBrightness(this);
  7. Toast.makeText(this, "系统屏幕亮度值" + getScreenBrightness(this),
  8. Toast.LENGTH_SHORT).show();
  9. // 设置APP 屏幕亮度后,系统Setting亮度将对此app 不生效
  10. setAppScreenBrightness(100);
  11. allowModifySettings();
  12. setContentView(R.layout.activity_main);
  13. }
  14. /**
  15. * 1.获取系统默认屏幕亮度值 屏幕亮度值范围(0-255)
  16. * **/
  17. private int getScreenBrightness(Context context) {
  18. ContentResolver contentResolver = context.getContentResolver();
  19. int defVal = 125;
  20. return Settings.System.getInt(contentResolver,
  21. Settings.System.SCREEN_BRIGHTNESS, defVal);
  22. }
  23. /**
  24. * 2.设置 APP界面屏幕亮度值方法
  25. * **/
  26. private void setAppScreenBrightness(int birghtessValue) {
  27. Window window = getWindow();
  28. WindowManager.LayoutParams lp = window.getAttributes();
  29. lp.screenBrightness = birghtessValue / 255.0f;
  30. window.setAttributes(lp);
  31. }
  32. /**
  33. * 3.关闭光感,设置手动调节背光模式
  34. *
  35. * SCREEN_BRIGHTNESS_MODE_AUTOMATIC 自动调节屏幕亮度模式值为1
  36. *
  37. * SCREEN_BRIGHTNESS_MODE_MANUAL 手动调节屏幕亮度模式值为0
  38. * **/
  39. public void setScreenManualMode(Context context) {
  40. ContentResolver contentResolver = context.getContentResolver();
  41. try {
  42. int mode = Settings.System.getInt(contentResolver,
  43. Settings.System.SCREEN_BRIGHTNESS_MODE);
  44. if (mode == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) {
  45. Settings.System.putInt(contentResolver,
  46. Settings.System.SCREEN_BRIGHTNESS_MODE,
  47. Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
  48. }
  49. } catch (Settings.SettingNotFoundException e) {
  50. e.printStackTrace();
  51. }
  52. }
  53. /**
  54. * 4.非系统签名应用,引导用户手动授权修改Settings 权限
  55. * **/
  56. private static final int REQUEST_CODE_WRITE_SETTINGS = 1000;
  57. private void allowModifySettings() {
  58. // Settings.System.canWrite(MainActivity.this)
  59. // 检测是否拥有写入系统 Settings 的权限
  60. if (!Settings.System.canWrite(MainActivity.this)) {
  61. AlertDialog.Builder builder = new AlertDialog.Builder(this,
  62. android.R.style.Theme_Material_Light_Dialog_Alert);
  63. builder.setTitle("请开启修改屏幕亮度权限");
  64. builder.setMessage("请点击允许开启");
  65. // 拒绝, 无法修改
  66. builder.setNegativeButton("拒绝",
  67. new DialogInterface.OnClickListener() {
  68. @Override
  69. public void onClick(DialogInterface dialog, int which) {
  70. Toast.makeText(MainActivity.this,
  71. "您已拒绝修系统Setting的屏幕亮度权限", Toast.LENGTH_SHORT)
  72. .show();
  73. }
  74. });
  75. builder.setPositiveButton("去开启",
  76. new DialogInterface.OnClickListener() {
  77. @Override
  78. public void onClick(DialogInterface dialog, int which) {
  79. // 打开允许修改Setting 权限的界面
  80. Intent intent = new Intent(
  81. Settings.ACTION_MANAGE_WRITE_SETTINGS, Uri
  82. .parse("package:"
  83. + getPackageName()));
  84. startActivityForResult(intent,
  85. REQUEST_CODE_WRITE_SETTINGS);
  86. }
  87. });
  88. builder.setCancelable(false);
  89. builder.show();
  90. }
  91. }
  92. @Override
  93. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  94. // TODO Auto-generated method stub
  95. super.onActivityResult(requestCode, resultCode, data);
  96. if (requestCode == REQUEST_CODE_WRITE_SETTINGS) {
  97. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  98. // Settings.System.canWrite方法检测授权结果
  99. if (Settings.System.canWrite(getApplicationContext())) {
  100. // 5.调用修改Settings屏幕亮度的方法 屏幕亮度值 200
  101. ModifySettingsScreenBrightness(MainActivity.this, 200);
  102. Toast.makeText(this, "系统屏幕亮度值" + getScreenBrightness(this),
  103. Toast.LENGTH_SHORT).show();
  104. } else {
  105. Toast.makeText(MainActivity.this, "您已拒绝修系统Setting的屏幕亮度权限",
  106. Toast.LENGTH_SHORT).show();
  107. }
  108. }
  109. }
  110. }
  111. /**
  112. * 5.修改Setting 中屏幕亮度值
  113. *
  114. * 修改Setting的值需要动态申请权限 <uses-permission
  115. * android:name="android.permission.WRITE_SETTINGS"/>
  116. * **/
  117. private void ModifySettingsScreenBrightness(Context context,
  118. int birghtessValue) {
  119. // 首先需要设置为手动调节屏幕亮度模式
  120. setScreenManualMode(context);
  121. ContentResolver contentResolver = context.getContentResolver();
  122. Settings.System.putInt(contentResolver,
  123. Settings.System.SCREEN_BRIGHTNESS, birghtessValue);
  124. }
  125. }

没了,不过到这里了,估计别白来,让你们心情好点!嘿嘿~

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

闽ICP备14008679号