title: ANDROID - Create Notification w/ Media Controls on Service Start
body: <p>I have a MediaPlayerService, currently started when the Play/Pause Button on a ListView item is clicked. See code below (CustomListAdapter):</p>
  
  <pre><code>Intent intent = new Intent(v.getContext(),MediaPlayerService.class);
  intent.putExtra("StreamLink",audio);
  activity.startService(intent);
  </code></pre>
  
  <p>When this service is started by the code above I want to create a Notification with a Play/Stop button. The user should be able to get out of the app, be able to stop Media Playback e.g. player.stop() and start player.start(). Also when the Notification is clicked it should return the user to the MainActivity.</p>
  
  <p>The code for my MediaPlayerService.java:</p>
  
  <pre><code>public class MediaPlayerService extends Service implements MediaPlayer.OnPreparedListener {
  
  MediaPlayer mMediaPlayer = null;
  public String audioStreamLink;
  
  public int onStartCommand(Intent intent, int flags, int startId) {
  
      // Get the Audio Streaming Link from the parsed JSON in the Main Activity
      audioStreamLink = intent.getStringExtra("StreamLink");
  
      // Instantiate MediaPlayer, set the Audio Type and acquire a wakelock, set the Media Player Data Source and Prepare.
      mMediaPlayer = new MediaPlayer();
      mMediaPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
      mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
      try {
          mMediaPlayer.setDataSource(audioStreamLink);
      } catch (IOException e) {
          e.printStackTrace();
      }
      mMediaPlayer.setOnPreparedListener(this);
      mMediaPlayer.prepareAsync();
  
      return START_STICKY;
  
  }
  
  @Nullable
  @Override
  public IBinder onBind(Intent intent) {
      return null;
  }
  
  /** Called when MediaPlayer is ready */
  @Override
  public void onPrepared(MediaPlayer player) {
      player.start();
  }
  
  @Override
  public void onDestroy() {
      mMediaPlayer.stop();
      mMediaPlayer.reset();
      if (mMediaPlayer != null) mMediaPlayer.release();
  
  }
  }
  </code></pre>