当前位置:   article > 正文

Android Screen Brightness 屏幕亮度 的获取和修改_android screenbrightness

android screenbrightness

Android Screen Brightness 屏幕亮度 的获取和修改

一、获取系统的屏幕亮度值
二、修改App屏幕亮度(仅当前应用)
三、修改系统屏幕亮度(所有应用)

一、获取系统的屏幕亮度值

系统屏幕亮度值在(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屏幕亮度(仅当前应用)
2.修改系统屏幕亮度(所有应用)

二、修改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的权限

<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. package com.example.test;
  2. import android.app.Activity;
  3. import android.app.AlertDialog;
  4. import android.content.ContentResolver;
  5. import android.content.Context;
  6. import android.content.DialogInterface;
  7. import android.content.Intent;
  8. import android.net.Uri;
  9. import android.os.Build;
  10. import android.os.Bundle;
  11. import android.provider.Settings;
  12. import android.view.Window;
  13. import android.view.WindowManager;
  14. import android.widget.Toast;
  15. public class MainActivity extends Activity {
  16. @Override
  17. protected void onCreate(Bundle savedInstanceState) {
  18. super.onCreate(savedInstanceState);
  19. //获取屏幕亮度
  20. getScreenBrightness(this);
  21. Toast.makeText(this, "系统屏幕亮度值" + getScreenBrightness(this),
  22. Toast.LENGTH_SHORT).show();
  23. // 设置APP 屏幕亮度后,系统Setting亮度将对此app 不生效
  24. setAppScreenBrightness(100);
  25. allowModifySettings();
  26. setContentView(R.layout.activity_main);
  27. }
  28. /**
  29. * 1.获取系统默认屏幕亮度值 屏幕亮度值范围(0-255)
  30. * **/
  31. private int getScreenBrightness(Context context) {
  32. ContentResolver contentResolver = context.getContentResolver();
  33. int defVal = 125;
  34. return Settings.System.getInt(contentResolver,
  35. Settings.System.SCREEN_BRIGHTNESS, defVal);
  36. }
  37. /**
  38. * 2.设置 APP界面屏幕亮度值方法
  39. * **/
  40. private void setAppScreenBrightness(int birghtessValue) {
  41. Window window = getWindow();
  42. WindowManager.LayoutParams lp = window.getAttributes();
  43. lp.screenBrightness = birghtessValue / 255.0f;
  44. window.setAttributes(lp);
  45. }
  46. /**
  47. * 3.关闭光感,设置手动调节背光模式
  48. *
  49. * SCREEN_BRIGHTNESS_MODE_AUTOMATIC 自动调节屏幕亮度模式值为1
  50. *
  51. * SCREEN_BRIGHTNESS_MODE_MANUAL 手动调节屏幕亮度模式值为0
  52. * **/
  53. public void setScreenManualMode(Context context) {
  54. ContentResolver contentResolver = context.getContentResolver();
  55. try {
  56. int mode = Settings.System.getInt(contentResolver,
  57. Settings.System.SCREEN_BRIGHTNESS_MODE);
  58. if (mode == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) {
  59. Settings.System.putInt(contentResolver,
  60. Settings.System.SCREEN_BRIGHTNESS_MODE,
  61. Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
  62. }
  63. } catch (Settings.SettingNotFoundException e) {
  64. e.printStackTrace();
  65. }
  66. }
  67. /**
  68. * 4.非系统签名应用,引导用户手动授权修改Settings 权限
  69. * **/
  70. private static final int REQUEST_CODE_WRITE_SETTINGS = 1000;
  71. private void allowModifySettings() {
  72. // Settings.System.canWrite(MainActivity.this)
  73. // 检测是否拥有写入系统 Settings 的权限
  74. if (!Settings.System.canWrite(MainActivity.this)) {
  75. AlertDialog.Builder builder = new AlertDialog.Builder(this,
  76. android.R.style.Theme_Material_Light_Dialog_Alert);
  77. builder.setTitle("请开启修改屏幕亮度权限");
  78. builder.setMessage("请点击允许开启");
  79. // 拒绝, 无法修改
  80. builder.setNegativeButton("拒绝",
  81. new DialogInterface.OnClickListener() {
  82. @Override
  83. public void onClick(DialogInterface dialog, int which) {
  84. Toast.makeText(MainActivity.this,
  85. "您已拒绝修系统Setting的屏幕亮度权限", Toast.LENGTH_SHORT)
  86. .show();
  87. }
  88. });
  89. builder.setPositiveButton("去开启",
  90. new DialogInterface.OnClickListener() {
  91. @Override
  92. public void onClick(DialogInterface dialog, int which) {
  93. // 打开允许修改Setting 权限的界面
  94. Intent intent = new Intent(
  95. Settings.ACTION_MANAGE_WRITE_SETTINGS, Uri
  96. .parse("package:"
  97. + getPackageName()));
  98. startActivityForResult(intent,
  99. REQUEST_CODE_WRITE_SETTINGS);
  100. }
  101. });
  102. builder.setCancelable(false);
  103. builder.show();
  104. }
  105. }
  106. @Override
  107. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  108. // TODO Auto-generated method stub
  109. super.onActivityResult(requestCode, resultCode, data);
  110. if (requestCode == REQUEST_CODE_WRITE_SETTINGS) {
  111. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  112. // Settings.System.canWrite方法检测授权结果
  113. if (Settings.System.canWrite(getApplicationContext())) {
  114. // 5.调用修改Settings屏幕亮度的方法 屏幕亮度值 200
  115. ModifySettingsScreenBrightness(MainActivity.this, 200);
  116. Toast.makeText(this, "系统屏幕亮度值" + getScreenBrightness(this),
  117. Toast.LENGTH_SHORT).show();
  118. } else {
  119. Toast.makeText(MainActivity.this, "您已拒绝修系统Setting的屏幕亮度权限",
  120. Toast.LENGTH_SHORT).show();
  121. }
  122. }
  123. }
  124. }
  125. /**
  126. * 5.修改Setting 中屏幕亮度值
  127. *
  128. * 修改Setting的值需要动态申请权限 <uses-permission
  129. * android:name="android.permission.WRITE_SETTINGS"/>
  130. * **/
  131. private void ModifySettingsScreenBrightness(Context context,
  132. int birghtessValue) {
  133. // 首先需要设置为手动调节屏幕亮度模式
  134. setScreenManualMode(context);
  135. ContentResolver contentResolver = context.getContentResolver();
  136. Settings.System.putInt(contentResolver,
  137. Settings.System.SCREEN_BRIGHTNESS, birghtessValue);
  138. }
  139. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/120563
推荐阅读
相关标签
  

闽ICP备14008679号