Try the following
AudioManager audioManager = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
//Obtenemos el click del boton de volumen + y -
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
int action = event.getAction();
int keyCode = event.getKeyCode();
int cont = 0;
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
if (action == KeyEvent.ACTION_DOWN) {
cont++;
//si queres ajustar el volumen
//audioManager.adjustVolume(AudioManager.ADJUST_RAISE, AudioManager.FLAG_PLAY_SOUND);
textView.setText("Volumen: "+cont);
}
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
if (action == KeyEvent.ACTION_DOWN) {
cont--;
//audioManager.adjustVolume(AudioManager.ADJUST_LOWER, AudioManager.FLAG_PLAY_SOUND);
textView.setText("Volumen: "+cont);
}
return true;
default:
return super.dispatchKeyEvent(event);
}
}
With that we generate a whole variable that is counter, in this case cont.
When we press the volume up button we increase the counter and we pass it to TextView
, when we lower the volume the opposite happens. Leave commented the lines that you can use to adjust the volume