Android ¿Cómo escuchar los eventos del botón de volumen?

Sé que ustedes probablemente están cansados ​​de este tipo de publicaciones, pero ¿por qué no pasa nada cuando presiono el volumen hacia abajo? Solo bash hacer un código simple, pero aparentemente no funciona.

package com.cakemansapps.lightwriter; import android.app.Activity; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.widget.FrameLayout; import android.view.KeyEvent; import android.util.Log; public class LightWriter extends Activity implements OnTouchListener { private static final String TAG = "Touch" ; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); FrameLayout main = (FrameLayout) findViewById(R.id.main_view); } @Override public boolean onKeyLongPress(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) { Log.w("LightWriter", "I WORK BRO."); return true; } return super.onKeyLongPress(keyCode, event); } public boolean onTouch(View view, MotionEvent me) { throw new UnsupportedOperationException("Not supported yet."); } } 

No sé si puede obtener eventos de prensa largos para las claves de hardware.

He usado este código para escuchar el botón de volumen anteriormente.

  @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if ((keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)){ //Do something } return true; } 

Si eso no funciona, díganos en qué dispositivo está probando.

Otro enfoque

 @Override public boolean dispatchKeyEvent(KeyEvent event) { int action = event.getAction(); int keyCode = event.getKeyCode(); switch (keyCode) { case KeyEvent.KEYCODE_VOLUME_UP: if (action == KeyEvent.ACTION_DOWN) { //TODO } return true; case KeyEvent.KEYCODE_VOLUME_DOWN: if (action == KeyEvent.ACTION_DOWN) { //TODO } return true; default: return super.dispatchKeyEvent(event); } } 
 @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if ((keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)) { // ... your code return true; } else if ((keyCode == KeyEvent.KEYCODE_VOLUME_UP)) { // ... your code return true; } else return super.onKeyDown(keyCode, event); } 

prueba estos. acabo de probarlos:

 @Override public boolean onKeyLongPress(int keyCode, KeyEvent event) { super.onKeyLongPress(keyCode, event); if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) { Log.w("LightWriter", "I WORK BRO."); return true; } return false; } public boolean onKeyDown(int keyCode, KeyEvent event) { super.onKeyDown(keyCode, event); if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) { Log.w("LightWriter", "I WORK BRO."); return true; } return true; } 

use este código para manejar el evento clave de volumen

 @Override public boolean onKeyUp(int keyCode, KeyEvent event) { super.onKeyUp(keyCode, event); if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) { Toast.makeText(MainActivity.this,"Up working",Toast.LENGTH_SHORT).show(); return true; } return false; } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { super.onKeyDown(keyCode, event); if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) { Toast.makeText(MainActivity.this,"Down working",Toast.LENGTH_SHORT).show(); return true; } return false; }