title: MediaSession onMediaButtonEvent works for a few seconds then quits - Android
body: <p>I've been trying to listen to media button intents on this Bluetooth button I bought off eBay for the past week and cannot figure out how to do it practically and reliably on my Oreo device. The first thing I tried was overriding <code>onKeyDown</code> in my main activity, which worked perfectly every time. However, I found it was impossible to listen to these calls once the application is minimized.</p>
  
  <p>Next I tried a very hacky solution of reading the logcat line by line and checking if <code>dispatched media key KeyEvent { action=ACTION_UP, keyCode=KEYCODE_MEDIA_NEXT</code> was present in that line. It worked alright until I again exited the app, which then it would completely stop.</p>
  
  <p>Finally I tried using a <code>MediaSession</code> and the <code>onMediaButtonEvent</code> method, which works perfectly for a few seconds to minutes after I start my activity then it just completely ceases to receive these intents.</p>
  
  <p>Here's my code right now (I copied another answer on StackOverflow who included playing a dummy audio clip to try and get focus):</p>
  
  <pre><code>        MediaSession ms = new MediaSession(getApplicationContext(), getPackageName());
          ms.setActive(true);
  
          ms.setCallback(new MediaSession.Callback() {
              @Override
              public boolean onMediaButtonEvent(Intent mediaButtonIntent) {
                  KeyEvent keyEvent = (KeyEvent) mediaButtonIntent.getExtras().get(Intent.EXTRA_KEY_EVENT);
                  if (keyEvent.getAction() == KeyEvent.ACTION_DOWN) {
                      switch (keyEvent.getKeyCode()) {
                          //do button specific stuff here
                      }
                  }
                  return super.onMediaButtonEvent(mediaButtonIntent);
              }
          });
          Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
          PendingIntent mediaButtonReceiverPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, mediaButtonIntent, 0);
  
          ms.setMediaButtonReceiver(mediaButtonReceiverPendingIntent);
  
          AudioTrack at = new AudioTrack(AudioManager.STREAM_MUSIC, 48000, AudioFormat.CHANNEL_OUT_STEREO, AudioFormat.ENCODING_PCM_16BIT,
                  AudioTrack.getMinBufferSize(48000, AudioFormat.CHANNEL_OUT_STEREO, AudioFormat.ENCODING_PCM_16BIT), AudioTrack.MODE_STREAM);
          at.play();
  
          // a little sleep
          at.stop();
          at.release();
  </code></pre>
  
  <p>I'm just trying to figure out now, why it seems to work for a few seconds perfectly, then it just stops out of nowhere. I don't have any other media app like Spotify open or playing whatsoever, and if that was the issue, I've tried running the audio code every 5 seconds or so which still has no effect. The only thing that will reset it is fully restarting the app.</p>
  
  <p>This code is in a service I have created. It only stops working if I am not in the app and simply press the home button to minimize it after a few seconds. I have a loop running to tell me that the service is still running, and it's seems it's running when it quits receiving the intents.</p>
  
  <p>Any help would be appreciated.</p>