Android音频相关
音量类型
铃声:STREAM_RING 闹钟:STREAM_ALARM 系统:STREAM_SYSTEM 通话:STREAM_VOICE_CALL 通知:STREAM_NOTIFICATION 媒体音量:STREAM_MUSIC
获取当前媒体音量
// 获取当前媒体音量 public static int getCurrentVolume(Context context){ if (context == null){ return -1; } AudioManager audioManager = (AudioManager)context.getSystemService(context.AUDIO_SERVICE); return audioManager.getStreamVolume(AudioManager.STREAM_MUSIC); }
获取最大媒体音量
// 获// 获取最大媒体音量 public static int getMaxVolume(Context context){ if (context == null){ return -1; } AudioManager audioManager = (AudioManager)context.getSystemService(context.AUDIO_SERVICE); return audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC); }
设置媒体音量
// 设置媒体音量 public static void setVolume(Context context){ if (context == null){ return; } AudioManager audioManager = (AudioManager)context.getSystemService(context.AUDIO_SERVICE); int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC); /** * setStreamVolume * streamType:音量类型 * index:需设置的音量值 * flags:附加参数: * AudioManager.FLAG_SHOW_VIBRATE_HINT:震动提示 * AudioManager.FLAG_SHOW_SILENT_HINT:静音提示 * AudioManager.FLAG_PLAY_SOUND:调整音量时播放声音 * AudioManager.FLAG_SHOW_UI:调整音量时显示系统音量进度条;0则不显示 */ audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,((int)maxVolume / 2 + 1),0); }
Android屏幕亮度相关
获取当前屏幕亮度
// 获取当前屏幕亮度 public static int getCurrentScreenBrightness(Context context) throws Settings.SettingNotFoundException { if (context == null){ return -1; } return Settings.System.getInt(context.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS); }
获取系统亮度模式
// 获取系统亮度模式(自动亮度:1 手动亮度:0) public static int getBrightnessMode(Context context) throws Settings.SettingNotFoundException { if (context == null){ return -1; } return Settings.System.getInt(context.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE); }
获取最大屏幕亮度
// 获取最大屏幕亮度 public static int getMaxScreenBrightness(Context context) throws Settings.SettingNotFoundException { if (context == null){ return -1; } int brightnessSettingMaximumId = context.getResources().getIdentifier("config_screenBrightnessSettingMaximum", "integer", "android"); return context.getResources().getInteger(brightnessSettingMaximumId); }
设置屏幕亮度
需要在AndroidManifest.xml中添加此权限
<!-- 修改系统屏幕亮度 --> <uses-permission android:name="android.permission.WRITE_SETTINGS" tools:ignore="ProtectedPermissions" />
// 设置屏幕亮度 @RequiresApi(api = Build.VERSION_CODES.M) public static void setScreenBrightness(Context context){ //判断是否有系统设置权限 if (Settings.System.canWrite(context)){ Settings.System.putInt(context.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, 0);//设置手动亮度 int brightnessSettingMaximumId = context.getResources().getIdentifier("config_screenBrightnessSettingMaximum", "integer", "android"); int brightnessSettingMaximum = context.getResources().getInteger(brightnessSettingMaximumId); Settings.System.putInt(context.getContentResolver(),Settings.System.SCREEN_BRIGHTNESS, brightnessSettingMaximum);//修改系统的亮度 }else { // 具体操作... } }
带权限返回值
package cn.com.lttc.loginui; import android.content.Context; import android.content.Intent; import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.provider.Settings; import android.view.View; import android.widget.Toast; import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; import androidx.appcompat.app.AppCompatActivity; import cn.com.lttc.loginui.util.SystemServiceUtil; public class TextActivity extends AppCompatActivity { public static final int REQUEST_CODE = 100; @RequiresApi(api = Build.VERSION_CODES.M) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main_register_step_one); setScreenBrightness(); } // 设置屏幕亮度 @RequiresApi(api = Build.VERSION_CODES.M) public void setScreenBrightness(){ if (Settings.System.canWrite(this)){ Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, 0);//设置手动亮度 int brightnessSettingMaximumId = getResources().getIdentifier("config_screenBrightnessSettingMaximum", "integer", "android"); int brightnessSettingMaximum = getResources().getInteger(brightnessSettingMaximumId);//获取系统亮度最大值 Settings.System.putInt(getContentResolver(),Settings.System.SCREEN_BRIGHTNESS, brightnessSettingMaximum);//修改系统的亮度 }else { // 进入设置页面手动添加权限 Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS, Uri.parse("package:" + getPackageName())); startActivityForResult(intent,REQUEST_CODE); } } @RequiresApi(api = Build.VERSION_CODES.M) @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == REQUEST_CODE) { if (Settings.System.canWrite(this)) { setScreenBrightness(); } else { Toast.makeText(this, "拒绝了权限", Toast.LENGTH_SHORT).show(); } } } }
SystemServiceUtil类
package cn.com.lttc.loginui.util; import android.content.Context; import android.media.AudioManager; import android.os.Build; import android.provider.Settings; import androidx.annotation.RequiresApi; public class SystemServiceUtil { // 获取当前媒体音量 public static int getCurrentVolume(Context context){ if (context == null){ return -1; } AudioManager audioManager = (AudioManager)context.getSystemService(context.AUDIO_SERVICE);//获取音频管理器 return audioManager.getStreamVolume(AudioManager.STREAM_MUSIC); } // 获取最大媒体音量 public static int getMaxVolume(Context context){ if (context == null){ return -1; } AudioManager audioManager = (AudioManager)context.getSystemService(context.AUDIO_SERVICE); return audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC); } // 设置媒体音量 public static void setVolume(Context context){ if (context == null){ return; } AudioManager audioManager = (AudioManager)context.getSystemService(context.AUDIO_SERVICE); int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC); /** * setStreamVolume * streamType:音量类型 * index:需设置的音量值 * flags:附加参数: * AudioManager.FLAG_SHOW_VIBRATE_HINT:震动提示 * AudioManager.FLAG_SHOW_SILENT_HINT:静音提示 * AudioManager.FLAG_PLAY_SOUND:调整音量时播放声音 * AudioManager.FLAG_SHOW_UI:调整音量时显示系统音量进度条;0则不显示 */ audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,((int)maxVolume / 2 + 1),0); } // 获取当前屏幕亮度 public static int getCurrentScreenBrightness(Context context) throws Settings.SettingNotFoundException { if (context == null){ return -1; } return Settings.System.getInt(context.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS); } // 获取系统亮度模式(自动亮度:1 手动亮度:0) public static int getBrightnessMode(Context context) throws Settings.SettingNotFoundException { if (context == null){ return -1; } return Settings.System.getInt(context.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE); } // 获取最大屏幕亮度 public static int getMaxScreenBrightness(Context context) throws Settings.SettingNotFoundException { if (context == null){ return -1; } int brightnessSettingMaximumId = context.getResources().getIdentifier("config_screenBrightnessSettingMaximum", "integer", "android"); return context.getResources().getInteger(brightnessSettingMaximumId); } // 设置屏幕亮度 @RequiresApi(api = Build.VERSION_CODES.M) public static void setScreenBrightness(Context context){ if (Settings.System.canWrite(context)){ Settings.System.putInt(context.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, 0);//设置手动亮度 int brightnessSettingMaximumId = context.getResources().getIdentifier("config_screenBrightnessSettingMaximum", "integer", "android"); int brightnessSettingMaximum = context.getResources().getInteger(brightnessSettingMaximumId);//获取系统亮度最大值 Settings.System.putInt(context.getContentResolver(),Settings.System.SCREEN_BRIGHTNESS, brightnessSettingMaximum);//修改系统的亮度 } } }