title: How to use the new MediaSession class to receive media button presses on Android 5.x?
body: <p>I’m attempting to receive media button presses using the new MediaSession class and so far I’ve been unsuccessful.  Has anyone managed to receive them using the new class?</p>
  
  <p>I’ve been successful in creating a MediaSession and using it to update song information on a remote display (an in-car entertainment system) but so far I’m unable to receive button presses from it, headphone controls, and controls on Bluetooth headsets.</p>
  
  <p>After I create the media session I’m executing the following in a service that I use to play audio:</p>
  
  <pre><code>    _mediaSession = new MediaSession(getApplicationContext(), Global.PACKAGE_NAME + "." + TAG);
  
      if (_mediaSession == null) {
          _log.e(TAG, "initMediaSession: _mediaSession = null");
          return;
      }
  
      _mediaSessionToken = _mediaSession.getSessionToken();
  
      _mediaSession.setCallback(new Callback() {
          public boolean onMediaButtonEvent(Intent mediaButtonIntent) {
              _log.d(TAG, "onMediaButtonEvent called: " + mediaButtonIntent);             
              return false;
          }
  
          public void onPause() {
              Log.d(TAG, "onPause called (media button pressed)");                
              super.onPause();
          }
  
          public void onPlay() {
              Log.d(TAG, "onPlay called (media button pressed)");             
              super.onPlay();
          }
  
          public void onStop() {
              Log.d(TAG, "onStop called (media button pressed)");             
              super.onStop();
          }
      });             
  
       _mediaSession.setFlags(MediaSession.FLAG_HANDLES_MEDIA_BUTTONS | MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS);
  
      PlaybackState state = new PlaybackState.Builder()
      .setActions(PlaybackState.ACTION_PLAY)
      .setState(PlaybackState.STATE_STOPPED, PlaybackState.PLAYBACK_POSITION_UNKNOWN, SystemClock.elapsedRealtime())
      .build();
  
      _mediaSession.setPlaybackState(state);
  
      _mediaSession.setActive(true);
  </code></pre>
  
  <p>but I’m still not receiving any button presses.</p>
  
  <p>Anyone have any ideas?</p>
  
  <p>Thanks</p>
  
  <p><strong>Update</strong> After changing</p>
  
  <pre><code>    PlaybackState state = new PlaybackState.Builder()
      .setActions(PlaybackState.ACTION_PLAY)
      .setState(PlaybackState.STATE_STOPPED, PlaybackState.PLAYBACK_POSITION_UNKNOWN, SystemClock.elapsedRealtime())
      .build();
  </code></pre>
  
  <p>to</p>
  
  <pre><code>    PlaybackState state = new PlaybackState.Builder()
      .setActions(PlaybackState.ACTION_PLAY)
      .setState(PlaybackState.STATE_STOPPED, PlaybackState.PLAYBACK_POSITION_UNKNOWN, 0)
      .build();
  </code></pre>
  
  <p>I'm now receiving button press notifications via the onMediaButtonEvent() callback (such as being notified that KEYCODE_MEDIA_PAUSE was pressed) but onPlay(), onPause(), and onStop() are never being called, any idea why?</p>