title: Volume button listener even with the screen turned off and locked
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/10154118/listen-to-volume-buttons-in-background-service">Listen to volume buttons in background service?</a>  </p>
  </blockquote>
  
  
  
  <p>I want to listen to volume key changes in my application even with the locked screen and display powered off. The main problem that the service runs all time (with the help of wake locks) but I can read only the volume changes with my approach.</p>
  
  <p>If I use a service that runs all time (even with the locked screen and turned off display) I have a problem. It has broadcast receiver for volume changes with the help of <a href="https://stackoverflow.com/questions/7130455/volume-change-listener">this</a> post.</p>
  
  <pre><code>public BroadcastReceiver MediaButton_Receiver = new BroadcastReceiver(){
      @Override
      public void onReceive(Context context, Intent intent) {
          KeyEvent event = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
          if (event == null) {
              return;
          }
          int action = event.getAction();
          if (action == KeyEvent.KEYCODE_VOLUME_DOWN) {
              Log.d(TAG,"VOLUME_DOWN Pressed");   
          }
      };
  };
  </code></pre>
  
  <p><code>event</code> is always null in the service. It only calls when the volume is changed. Also BroadcastReceiver doesn't work when the screen is off.</p>
  
  <p>Another way to do it is to use an Activity with that overrides <code>dispatchKeyEvent()</code> but my Activity runs only a little bit, I tried wake lock, it didn't help. And I'm still not sure that it will work with the locked screen. I have searched all over the Internet but I cannot find the answer.</p>
  
  <p>Can I create an Activity that will catch pressing the volume buttons even with the locked and turned off screen? Or can I somehow do it with the service?</p>