如何检测音量是否设置为静音?

当我正在开发Android应用程序时,如何检测音量是否设置为静音?

如果音量变为静音或取消静音,我的应用程序如何得到通知?

Solutions Collecting From Web of "如何检测音量是否设置为静音?"

您可以使用AudioManager检查音量是静音还是不静音。

AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); switch( audio.getRingerMode() ){ case AudioManager.RINGER_MODE_NORMAL: break; case AudioManager.RINGER_MODE_SILENT: break; case AudioManager.RINGER_MODE_VIBRATE: break; } 

对于音量的改变,还有BroadcastReceiver。

 public class VolumeKeyReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { //Implement Logic } } 

注册BroadcastReceiver

 VolumeKeyReceiver keyReceiver = new VolumeKeyReceiver(); IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction("android.media.VOLUME_CHANGED_ACTION"); registerReceiver(keyReceiver, intentFilter);