I already made a solution that monitors volume level while in a phone call, so this should be similar.
You can either listen for the key presses in your activity (onKey) and check for the corresponding keyCode (VOLUME_UP or VOLUME_DOWN).
Or, if you can't listen for the key events for some reason (some remote view), just poll the volume level at a fast enough frequency:
Use
am.getStreamVolume(AudioManager.STREAM_MUSIC)
to poll the volume level.Run this at your desired frequency. For example:
private Runnable mVolumeUpdateRunnable = new Runnable() { public void run() { if (shouldUpdate) { updateCurrentVolume(); new Handler().postDelayed(mVolumeUpdateRunnable, VOLUME_SAMPLING_DELAY); } } };
The calls are not expensive so it shouldn't be a big problem to update at a high frequency.