I am trying to update a TextView which will show the system volume. I have managed to capture the current system volume and display it, but it doesn’t update when the volume is turned up/down (obviously).
I know there is an easy solution somewhere I just cant think! onKeyListeners?
I am now using this but it doesnt work:
TextView sysVol = (TextView)findViewById(R.id.systemVolume);
sysVol.setOnKeyListener(new OnKeyListener()
{
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_UP || keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
//system volume
int curVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
int i = curVolume * 4;
String aString = Integer.toString(i);
TextView sysVol = (TextView)findViewById(R.id.systemVolume);
sysVol.setText(aString);
return true;
}
return false;
}
});
Well, the pseudo approach would be the following scenario:
Basically, you’d want to implement the
View.onKey()-method for this to be a minor bump in the road, instead of the current roadblock-state.Summary: Add the following snippet to your code (some modification needed)