title: Android BroadCastReceiver for volume key up and down
body: <p>If a user presses volume key up or down is it possible to detect it in my broadcast receiver? I need the full code.</p>
  
  <p>Here is my Intent filter</p>
  
  <pre><code>IntentFilter filter = new IntentFilter();
  filter.addAction("android.media.VOLUME_CHANGED_ACTION");
  </code></pre>
  
  <p>and my <code>onReceive</code> method is </p>
  
  <pre><code>public void onReceive(Context arg0, Intent intent) {
        KeyEvent ke = (KeyEvent)intent.getExtras().get(Intent.EXTRA_KEY_EVENT);
          if (ke .getKeyCode() == KeyEvent.KEYCODE_VOLUME_UP) {
                  System.out.println("I got volume up event");
           }else if (ke .getKeyCode() == KeyEvent.KEYCODE_VOLUME_DOWN) {
                  System.out.println("I got volume key down event");
           }
      }
  </code></pre>
  
  <p>It gives me a null KeyEvent... any idea?</p>