项目场景:
作为毕业生第一次参加项目,做的是设置模块的部分功能,这里把本次项目的系统音量和系统亮度的调节的代码做一下保存,方便以后查看
解决方案:
最早写的亮度的工具类SystemBrightnessManager
package com.sprocomm.tablelamp.ui.setting.util;
import android.Manifest;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.view.WindowManager;
import androidx.core.app.ActivityCompat;
import static android.webkit.WebViewZygote.getPackageName;
import static com.blankj.utilcode.util.ActivityUtils.startActivity;
public class SystemBrightnessManager {
public static boolean isAutoBrightness(Context context) {
boolean autoBrightness = false;
ContentResolver contentResolver = context.getContentResolver();
try {
autoBrightness = Settings.System.getInt(contentResolver,
Settings.System.SCREEN_BRIGHTNESS_MODE) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
}
return autoBrightness;
}
public static int getBrightness(Context context) {
int brightValue = 0;
ContentResolver contentResolver = context.getContentResolver();
try {
brightValue = Settings.System.getInt(contentResolver,
Settings.System.SCREEN_BRIGHTNESS);
} catch (Settings.SettingNotFoundException e){
e.printStackTrace();
}
return brightValue;
}
public static void setBrightness(Context context, int brightValue) {
WindowManager.LayoutParams layoutParams = ((Activity)context).getWindow().getAttributes();
layoutParams.screenBrightness = (brightValue <= 0? -1.0f : brightValue / 255f);
((Activity)context).getWindow().setAttributes(layoutParams);
saveBrightness(context, brightValue);
}
public static void startAutoBrightness(Context context) {
Settings.System.putInt(context.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE,
Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
Uri uri = android.provider.Settings.System.getUriFor("screen_brightness");
context.getContentResolver().notifyChange(uri, null);
}
public static void stopAutoBrightness(Context context) {
Settings.System.putInt(context.getContentResolver(),
Settings.System.SCREEN_BRIGHTNESS_MODE,
Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
Uri uri = android.provider.Settings.System.getUriFor("screen_brightness");
context.getContentResolver().notifyChange(uri, null);
}
public static void setBrightnessMode(Context context, int brightMode) {
Settings.System.putInt(context.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, brightMode);
}
public static void saveBrightness(Context context, int brightness) {
try {
ContentResolver resolver = context.getContentResolver();
Settings.System.putInt(resolver, Settings.System.SCREEN_BRIGHTNESS, brightness);
} catch (Exception e){
e.printStackTrace();
}
}
}
其中包含系统亮度的get/set方法,setBrightness() 是针对当前屏幕亮度设定值,其中还调用了saveBrightness() ,将值作为系统亮度设定值并保存,即退出当前页面时,亮度的改变也生效。
在之后,我为了别人和自己更好更方便地调用,又写了一个SettingUtils 类用来展现更改系统亮度和系统音量的静态方法,这里的音量仅仅指媒体音量
package com.sprocomm.tablelamp.ui.setting.util;
import android.app.SprocommManager;
import android.content.Context;
import android.media.AudioManager;
import android.os.Build;
import android.util.Log;
import android.widget.Toast;
import androidx.annotation.RequiresApi;
import com.sprocomm.mvvm.data.local.KVUtils;
import com.sprocomm.tablelamp.R;
import com.sprocomm.tablelamp.constant.Setting;
@RequiresApi(api = Build.VERSION_CODES.P)
public class SettingUtils {
public static final String tag = "SettingUtils";
public static final String lInterval = "========================================";
public static final String sInterval = "==============================";
public static void updateBrightness (Context context, Boolean isAdd, int number) {
Log.d(tag,"进入updateBrightness()");
int currentProgress = getBrightness(context);
Log.d(tag,"当前亮度:" + currentProgress);
int addResult = currentProgress + number;
int reduceResult = currentProgress - number;
setBrightness(context, isAdd ? addResult : reduceResult);
Log.d(tag,"修改后亮度:"+ getBrightness(context));
}
public static void setMaxBrightness (Context context) {
Log.d(tag,"进入setMaxBrightness()");
setBrightness(context, getMaxBright());
Log.d(tag,"亮度已调至最亮");
}
public static void setMinBrightness (Context context) {
Log.d(tag,"进入setMinBrightness()");
setBrightness(context, getMinBright());
Log.d(tag,"亮度已调至最暗");
}
public static void setBrightness (Context context, int progress) {
setBrightness(context, progress, true);
}
public static void setBrightness (Context context, int progress, Boolean isSetBySeekbar) {
Log.d(tag, lInterval);
Log.d(tag,"进入setBrightness()");
progress = progress > 100 ? 100 : progress;
progress = progress < 1 ? 1 : progress;
int brightness = progressToBright(progress);
if (isSetBySeekbar) {
String preLightMode = getKVLightMode(context);
if (!preLightMode.equals(context.getString(R.string.custom))) {
Log.d(tag,"之前亮度模式为:" + preLightMode);
setKVLightMode(context.getString(R.string.custom));
Log.d(tag,"改为自定义");
}
}
SystemBrightnessManager.setBrightness(context, brightness);
Log.d(tag, lInterval);
}
public static void setLightMode(Context context, String lightMode) {
if (lightMode.equals(context.getString(R.string.vision_protection))) {
setBrightness(context, brightToProgress(90), false);
} else if (lightMode.equals(context.getString(R.string.standard_care))) {
setBrightness(context, brightToProgress(140), false);
} else if (lightMode.equals(context.getString(R.string.clearest))) {
setBrightness(context, brightToProgress(210), false);
}
setKVLightMode(lightMode);
}
public static String getKVLightMode (Context context) {
return KVUtils.getInstance().getString(Setting.LIGHT_MODE, context.getString(R.string.standard_care));
}
public static void setKVLightMode (String lightMode) {
KVUtils.getInstance().putString(Setting.LIGHT_MODE, lightMode);
}
public static int getBrightness (Context context) {
Log.d(tag, lInterval);
Log.d(tag,"进入getBrightness()");
int brightness = SystemBrightnessManager.getBrightness(context);
int progress = brightToProgress(brightness);
Log.d(tag,"获取当前亮度为(1-100):" + progress);
Log.d(tag, lInterval);
return progress;
}
public static int getMaxBright () {
return 100;
}
public static int getMinBright () {
return 1;
}
public static void updateVoice (Context context, Boolean isAdd, int number, Boolean isPercentage) {
Log.d(tag,"进入调节媒体音量静态方法");
int currentNumber = getVoice(context);
number = isPercentage ? number/4 : number;
int addResult = currentNumber + number;
int reduceResult = currentNumber - number;
setVoice(context, isAdd ? addResult : reduceResult, false);
Log.d(tag,"媒体音量调节成功");
}
public static int getVoice(Context context) {
Log.d(tag,"进入获取媒体音量静态方法");
AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
int voice = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
Log.d(tag,"获取当前音量为" + voice);
return voice;
}
public static void setMaxVoice(Context context) {
Log.d(tag,"进入媒体音量最大静态方法");
setVoice(context, getMaxVoice(context), false);
Log.d(tag,"媒体音量调至最大");
}
public static void setMinVoice(Context context) {
Log.d(tag,"进入媒体音量最小静态方法");
setVoice(context, getMinVoice(context), false);
Toast.makeText(context, "已调至静音", Toast.LENGTH_SHORT);
Log.d(tag,"媒体音量调至最小");
}
public static int getMaxVoice(Context context) {
AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
int voice = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
return voice;
}
public static int getMinVoice(Context context) {
AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
int voice = audioManager.getStreamMinVolume(AudioManager.STREAM_MUSIC);
return voice;
}
public static void setVoice (Context context, int voicePower) {
setVoice(context, voicePower, false);
}
public static void setVoice (Context context, int voicePower, Boolean isPercentage) {
Log.d(tag,"进入媒体音量调节特定值静态方法");
voicePower = isPercentage ? voicePower/4 : voicePower;
voicePower = voicePower < 0 ? 0 : (voicePower > 25 ? 25 : voicePower);
AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, voicePower, 0);
Log.d(tag,"媒体音量调至:"+ voicePower);
}
public static int brightToProgress(int bright) {
Log.d(tag, sInterval);
Log.d(tag, "进入brightToProgress()");
int progress = (int) ((bright - (2745f / 99)) * (99f / 225));
Log.d(tag, "bright = "+ bright + " to progress = " + progress);
Log.d(tag, sInterval);
return progress;
}
public static int progressToBright(int progress) {
Log.d(tag, sInterval);
Log.d(tag, "进入progressToBright()");
int bright = (int) ((225f / 99) * progress + (2745f / 99));
Log.d(tag, "progress = "+ progress + " to bright = "+ bright);
Log.d(tag, sInterval);
return bright;
}
}
这里亮度定义范围为30-255,映射成1-100展现 音量定义范围为0-25,映射成0-100展现
|