赞
踩
前言:
1.获取当前系统屏幕亮度
2.修改app屏幕亮度,仅对当前app有效。-可用在出示二维码界面,需要单个界面提高亮度时使用
3.修改系统屏幕亮度,所有app有效。Android 6.0 以上需要动态申请权限
实现:
1.权限
- <uses-permission android:name="android.permission.WRITE_SETTINGS"
- tools:ignore="ProtectedPermissions" />
2.动态申请
- /**
- * 非系统签名应用,引导用户手动授权修改Settings 权限
- **/
- private static final int REQUEST_CODE_WRITE_SETTINGS = 1000;
-
- @RequiresApi(api = Build.VERSION_CODES.M)
- private void initPermission() {
- // Settings.System.canWrite(MainActivity.this)
- // 检测是否拥有写入系统 Settings 的权限
- if (!Settings.System.canWrite(MainActivity5.this)) {
- AlertDialog.Builder builder = new AlertDialog.Builder(this);
- builder.setTitle("请开启修改屏幕亮度权限");
- builder.setMessage("请点击允许开启");
- // 拒绝, 无法修改
- builder.setNegativeButton("拒绝",
- new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- Toast.makeText(MainActivity5.this, "您已拒绝修系统Setting的屏幕亮度权限", Toast.LENGTH_SHORT)
- .show();
- }
- });
- builder.setPositiveButton("去开启",
- new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // 打开允许修改Setting 权限的界面
- Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS, Uri.parse("package:" + getPackageName()));
- startActivityForResult(intent,
- REQUEST_CODE_WRITE_SETTINGS);
- }
- });
- builder.setCancelable(false);
- builder.show();
- }
- }
- @Override
- protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
- if (requestCode == REQUEST_CODE_WRITE_SETTINGS) {
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
- // Settings.System.canWrite方法检测授权结果
- if (Settings.System.canWrite(getApplicationContext())) {
- //授权通过
- } else {
- Toast.makeText(MainActivity5.this, "您已拒绝修系统Setting的屏幕亮度权限",
- Toast.LENGTH_SHORT).show();
- }
- }
- }
- }
3.获取系统屏幕亮度(如果不需要修改系统屏幕亮度的话不需要申请权限)
- /**
- * 获取app屏幕亮度
- *
- * @param activity
- * @return
- */
- public static int getScreenBrightness(Activity activity) {
- int value = 0;
- ContentResolver cr = activity.getContentResolver();
- try {
- value = Settings.System.getInt(cr, Settings.System.SCREEN_BRIGHTNESS);
- } catch (Settings.SettingNotFoundException e) {
- }
- return value;
- }
4.设置app屏幕亮度(仅对当前app有效,不需要申请权限)
- /**
- * 设置app屏幕亮度
- *
- * @param activity
- * @param value
- */
- public static void setScreenBrightness(Activity activity, int value) {
- WindowManager.LayoutParams params = activity.getWindow().getAttributes();
- params.screenBrightness = value / 255f;
- activity.getWindow().setAttributes(params);
- }
5.修改系统屏幕亮度(Android 6.0 以上必须动态申请权限)
- findViewById(R.id.mBtn_set_system).setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- //关闭光感
- setScreenManualMode(MainActivity5.this);
- //设置系统屏幕亮度
- ModifySettingsScreenBrightness(MainActivity5.this, 100);
- }
- });
-
- /**
- * 关闭光感,设置手动调节背光模式
- * SCREEN_BRIGHTNESS_MODE_AUTOMATIC 自动调节屏幕亮度模式值为1
- * SCREEN_BRIGHTNESS_MODE_MANUAL 手动调节屏幕亮度模式值为0
- **/
- public void setScreenManualMode(Context context) {
- ContentResolver contentResolver = context.getContentResolver();
- try {
- int mode = Settings.System.getInt(contentResolver, Settings.System.SCREEN_BRIGHTNESS_MODE);
- if (mode == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC) {
- Settings.System.putInt(contentResolver, Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
- }
- } catch (Settings.SettingNotFoundException e) {
- e.printStackTrace();
- }
- }
-
- /**
- * 修改Setting 中屏幕亮度值 亮度 0-255
- * 修改Setting的值需要动态申请权限 <uses-permission
- * android:name="android.permission.WRITE_SETTINGS"/>
- **/
- private void ModifySettingsScreenBrightness(Context context, int birghtessValue) {
- // 首先需要设置为手动调节屏幕亮度模式
- setScreenManualMode(context);
- ContentResolver contentResolver = context.getContentResolver();
- Settings.System.putInt(contentResolver, Settings.System.SCREEN_BRIGHTNESS, birghtessValue);
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。