title: Catching key presses when android phone is in deep sleep mode
body: <h2>Problem Description</h2>
  <p>I am trying to write an application which will catch Volume Up/Down and also Bluetooth Headset buttons pressing when application is in</p>
  <ul>
  <li>Background</li>
  <li>Foreground</li>
  <li>Background and phone is in sleeping mode (Power button is pressed)</li>
  <li>Foreground and phone is in sleeping mode (Power button is pressed)</li>
  </ul>
  <p>For first two points I have write some test application and everything works in my case, I send application to background and press volume up/down and my application catch this events.
  After I press on a power button and my phone goes to sleep mode. Then I press volume up/down buttons phone do not react to that events, and I can't see any loge in logcat, like everything stop work.</p>
  <h2>Applications which detect keypresses in sleep mode.</h2>
  <p>I think that this problem can't be solved, but then I remember that Android standard Media Player catch this events when you press volume up/down buttons while music plays. I think that this application can do that as it is system application, but then I download Winpm player and it work in a same way. Winapm catch volume up/down events when phone is in sleeping mode. And music player applications do that.</p>
  <h2>Question</h2>
  <p>I want to understand how this can be done ? How I can write a simple application which will catch volume up/down button presses. Do I need to play some music at that time, or I can do that ? I mean maybe the main reason that Winamp and other applications catch that event is that music is played at that time. Or maybe this problem can't be solved and if it can't be solved I want to know why ? I need some arguments why it can't be solved.</p>
  <h2>Source code</h2>
  <p>I have tested some application and here are the results, I have write a simple application that catch volume up and down key presses in background and foreground and also when application is in sleep mode. Everything work perfect when application was in background and forground, my test application catch events, but when I press on a power button it stop doing that, then <em><strong>I press start on media pleyer and it start to play some music, after I press power button again and then press volume up and doun and my application stat catching volume up and down keypresses</strong></em>. It mean that then music is played my application can catch that events, so my next question how I can simulate like my phone plays somem music ? may be this is solution ?</p>
  <p><em><strong>Manifest.xml</strong></em></p>
  <pre><code>&lt;receiver android:name=&quot;com.vito.backgroundworker.VolumeBroadcast&quot; android:enabled=&quot;true&quot;&gt;
      &lt;intent-filter&gt;
      &lt;action android:name=&quot;android.media.VOLUME_CHANGED_ACTION&quot; /&gt;
      &lt;/intent-filter&gt;
  &lt;/receiver&gt;
          
  &lt;receiver android:name=&quot;RemoteControlReceiver&quot;&gt;
      &lt;intent-filter&gt;
      &lt;action android:name=&quot;android.intent.action.MEDIA_BUTTON&quot; /&gt;
      &lt;/intent-filter&gt;
  &lt;/receiver&gt;
  </code></pre>
  <p><em><strong>MainActivity.java</strong></em></p>
  <pre><code>public class MainActivity extends Activity {
      private PowerManager.WakeLock wl;
      //private BroadcastReceiver vol = new VolumeBroadcast();
      private AudioManager mAudioManager;
      private ComponentName mRemoteControlResponder;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          
          PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
          wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, &quot;DoNjfdhotDimScreen&quot;);
          
          mAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
          mRemoteControlResponder = new ComponentName(getPackageName(),
                  RemoteControlReceiver.class.getName());
      }
  
      @Override
      protected void onPause() {
          super.onPause();
          mAudioManager.registerMediaButtonEventReceiver(
                  mRemoteControlResponder);
          wl.release();
      }//End of onPause
  
      @Override
      protected void onResume() {
          super.onResume();
          wl.acquire();
      }//End of onResume
      
      @Override
      public void onDestroy() {
          super.onDestroy();
          mAudioManager.unregisterMediaButtonEventReceiver(
                  mRemoteControlResponder);
      }
  }
  </code></pre>