home和音量键监听的onKeyDown与onKeyUp使用

public boolean onKeyDown(int keyCode, KeyEvent event) {
          if(keyCode==KeyEvent.KEYCODE_VOLUME_DOWN){
              return true;
          }
          else if(keyCode==KeyEvent.KEYCODE_VOLUME_UP) {
            return true;
         }else {
         	return super.onKeyDown(keyCode, event)
         };
    }

音量调节蔽掉了,唯独不能屏蔽掉调节音量的声音可以试试另一个
两个都可以起到监听作用

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_VOLUME_DOWN){
            return true;
        }else if(keyCode==KeyEvent.KEYCODE_VOLUME_UP){
            return true;
        }else {
        	return super.onKeyUp(keyCode, event);
		}
    }
音量键调节
 AudioManager audioManager  = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
 if (keyCode==KeyEvent.KEYCODE_VOLUME_UP){
            audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE,AudioManager.FX_FOCUS_NAVIGATION_UP);
        }if (keyCode==KeyEvent.KEYCODE_VOLUME_DOWN){
            audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_LOWER,AudioManager.FX_FOCUS_NAVIGATION_UP);
        }

你可能感兴趣的:(Android)