How can I mute Android phone ringing at an incoming call programmatically? (Like doing this by pressing power button at incoming call)?
I know about setStreamMute
and adjustStreamVolume
, but I think there is a better way.
How can I mute Android phone ringing at an incoming call programmatically? (Like doing this by pressing power button at incoming call)?
I know about setStreamMute
and adjustStreamVolume
, but I think there is a better way.
setStreamMute()
is deprecated in and above Build version 23 (marshmallow). You can use adjustStreamVolume()
for marshmallow and above.
public void adjustAudio(boolean setMute) {
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
int adJustMute;
if (setMute) {
adJustMute = AudioManager.ADJUST_MUTE;
} else {
adJustMute = AudioManager.ADJUST_UNMUTE;
}
audioManager.adjustStreamVolume(AudioManager.STREAM_NOTIFICATION, adJustMute, 0);
audioManager.adjustStreamVolume(AudioManager.STREAM_ALARM, adJustMute, 0);
audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, adJustMute, 0);
audioManager.adjustStreamVolume(AudioManager.STREAM_RING, adJustMute, 0);
audioManager.adjustStreamVolume(AudioManager.STREAM_SYSTEM, adJustMute, 0);
} else {
audioManager.setStreamMute(AudioManager.STREAM_NOTIFICATION, setMute);
audioManager.setStreamMute(AudioManager.STREAM_ALARM, setMute);
audioManager.setStreamMute(AudioManager.STREAM_MUSIC, setMute);
audioManager.setStreamMute(AudioManager.STREAM_RING, setMute);
audioManager.setStreamMute(AudioManager.STREAM_SYSTEM, setMute);
}
}
You can call this method like,
adjustAudio(true) // To mute all the system, ringer, alarm, music, notification sounds on any event like button click.
adjustAudio(false) // To unmute all the system, ringer, alarm, music, notification sounds on any event like button click.