给Android的音量按钮添加监听事件

@Override
public boolean onKeyDown (int keyCode, KeyEvent event) {
// 获取手机当前音量值
int i = getCurrentRingValue ();
switch (keyCode) {
// 音量减小
case KeyEvent.KEYCODE_VOLUME_DOWN:
Toast.makeText (Activity03.this, "当前音量值: " + i, Toast.LENGTH_SHORT).show ();
return true;
// 音量增大
case KeyEvent.KEYCODE_VOLUME_UP:
Toast.makeText (Activity03.this, "当前音量值: " + i, Toast.LENGTH_SHORT).show ();
return true;
}
return super.onKeyDown (keyCode, event);
}

你可能感兴趣的:(Android)