简体   繁体   中英

How can I detect if the volume is set to mute?

When I am developing an Android application, how can I detect if volume is set to mute?

How can my application get notified if the volume changes to mute or gets unmuted?

You can use AudioManager to check volume is mute or not mute.

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;
}

and for Volume change there is BroadcastReceiver for that.

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

Register the BroadcastReceiver

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM