user3453575
user3453575

Reputation: 71

Foreground service is destroyed after receiving the pending intent

I am trying to write a music player for android and am using foreground service to run the music player. I am sending a pendingIntent from the UI controls to the service to play songs. After receiving the intent, onStartCommand and onDestroy are called immediately. I am not sure how to stop the onDestroy the call.

I tried changing the pending intent to startService/ContextCompat.startForegroundService but the issue still persists.

Service:

import android.app.Service;
public class PlayerService extends Service{
private TransportControls transportControls;
@Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    if (mediaSessionManager == null) {
      initMediaSession();
    }
    handleIntent(intent);

    return START_STICKY;
  }

private initMediaSession(){
     ...
     transportControls = mediaSession.getController().getTransportControls();

     mediaSession.setCallback(new MediaSessionCompat.Callback() {
      // Implement callbacks
      @Override
      public void onPlay() {
        play();
      }

     ...
  }

  handleIntent(Intent intent){
     // Initial checks
     if(/* action in intent is play*/) transportControls.play();
  }


  private void play(SongInfo songInfo){
     ...
      buildNotificaiton(//play action);
  }

  private void buildNotification(){
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
        this, "default").setContentIntent(intent)
        .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
        .setStyle(new MediaStyle()
            .setMediaSession(mediaSession.getSessionToken())
            .setShowActionsInCompactView(0, 1, 2))
        .setContentText(StringUtils.parseArtists(songInfo.artists()))
        .setContentTitle(songInfo.displayName())
        .setContentInfo(songInfo.album())
        .setSound(null);

    NotificationManager notificationManager = (NotificationManager) getSystemService(
        Context.NOTIFICATION_SERVICE);
    if (notificationManager == null) {
      return;
    }

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
      NotificationChannel channel = new NotificationChannel("default",
          "Bhatki media notification",
          NotificationManager.IMPORTANCE_HIGH);
      channel.setDescription(
          "Notification displayed when music is being played. This notification is "
              + "required for the music to play.");
      notificationManager.createNotificationChannel(channel);
    }

    // Execution is reaching this line.
    startForeground(NOTIFICATION_ID, notificationBuilder.build());
  }
}

In activity:

Intent intent = new Intent();
    intent.setClass(context, PlayerService.class);
    intent.setAction(action);
    intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);

// one approach
    ContextCompat.startForegroundService(context, intent);

// Different approach
//    PendingIntent pendingIntent = PendingIntent
//        .getService(serviceContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
//    try {
//      pendingIntent.send(serviceContext, 0, intent);
//    } catch (CanceledException e) {
//      Log.d(TAG, "sendIntent: failed");

I also added that android foreground service permission in the manifest:

 <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>

I am expecting the notification and service to persist. Can't figure out why OnDestroy is called.

Upvotes: 1

Views: 1338

Answers (1)

fangzhzh
fangzhzh

Reputation: 2252

Once the service has been created, the service must call its startForeground() method within five seconds.

Apps that target Android 9 (API level 28) or higher and use foreground services must request the FOREGROUND_SERVICE permission

foreground service

Edit: I can't tell the problem from your code but from my experience, the notification block should be in override fun onCreate()

Upvotes: 0

Related Questions