当我开发Android应用程序时,如何检测音量是否设置为静音?
如果音量变为静音或取消静音,我的应用程序如何得到通知?
答案 0 :(得分:27)
您可以使用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);