SoFunction
Updated on 2025-04-09

Android implements the change in monitoring volume

This article shares the specific code for Android monitoring volume changes for your reference. The specific content is as follows

Recently, the project involves the need to monitor volume and make changes, so here is to mark it

There are two common methods to monitor the change in volume:

1. The change of monitor volume keys

method:Rewrite the activity onKeyDown function

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_VOLUME_UP:
                //Volume key up                return false;
            case KeyEvent.KEYCODE_VOLUME_DOWN:
                //Volume key down                return false;
            default:
                break;
        }
        return (keyCode, event);
}

advantage:Fast, convenient and simple

shortcoming:

1. It is easy to consume the volume key to change the event (returning false is sometimes useless), actual test
2. This listener is for the current activity, and different Acs need to rewrite the onKeyDown function.
3. From the second point, it can be seen that the entire application range cannot be monitored.

2. Get the broadcast of volume changes

When the system volume changes, the broadcast will be sent. Just monitor this. The action of volume change is
.VOLUME_CHANGED_ACTION
For details, see the code: (kotlin version)

class VolumeChangeHelper(var context: Context){

    private var mVolumeBroadCastReceiver: VolumeBroadCastReceiver? = null
    private var mVolumeChangeListener: VolumeChangeListener? = null

    companion object{
        const val  VOLUME_CHANGE_ACTION = ".VOLUME_CHANGED_ACTION"
        const val EXTRA_VOLUME_STREAM_TYPE = ".EXTRA_VOLUME_STREAM_TYPE"
    }

    private var audioManager: AudioManager? =  (Context.AUDIO_SERVICE) as? AudioManager

    fun registerVolumeChangeListener(volumeChangeListener: VolumeChangeListener){
        mVolumeChangeListener = volumeChangeListener
        mVolumeBroadCastReceiver = VolumeBroadCastReceiver()
        val filter = IntentFilter()
        (VOLUME_CHANGE_ACTION)
        if(mVolumeBroadCastReceiver != null){
            //Register this broadcast            (mVolumeBroadCastReceiver!!, filter)
        }
    }

    fun unregisterReceiver(){
        if(mVolumeBroadCastReceiver != null){
            (mVolumeBroadCastReceiver!!)
            mVolumeBroadCastReceiver = null
        }
    }

    interface VolumeChangeListener{
        fun onVolumeDownToMin()
        fun onVolumeUp()
    }
    
    //Define a broadcast recipient who wants to monitor volume changes    inner class VolumeBroadCastReceiver : BroadcastReceiver(){
        override fun onReceive(context: Context?, intent: Intent?) {
            if(intent?.action == VOLUME_CHANGE_ACTION && intent?.getIntExtra(EXTRA_VOLUME_STREAM_TYPE, -1) == AudioManager.STREAM_MUSIC){
                val currentVolume = audioManager?.getStreamVolume(AudioManager.STREAM_MUSIC) ?: -1
                if(currentVolume > 0){
                    mVolumeChangeListener?.onVolumeUp()
                } else  if( currentVolume == 0){
                    mVolumeChangeListener?.onVolumeDownToMin()
                }
            }
        }

    }
}

If you want to implement global monitoring, you can monitor it in a Context that exists throughout an app's life cycle, such as MainActivity

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.