Служба переднего плана уничтожается после получения ожидающего намерения

Я пытаюсь написать музыкальный проигрыватель для Android и использую службу переднего плана для запуска музыкального проигрывателя. Я отправляю pendingIntent из элементов управления пользовательского интерфейса в службу для воспроизведения песен. После получения намерения немедленно вызываются onStartCommand и onDestroy. Я не уверен, как остановить вызов onDestroy.

Я попытался изменить ожидающее намерение на startService / ContextCompat.startForegroundService, но проблема все еще сохраняется.

Услуга:

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());
  }
}

В действии:

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");

Я также добавил это разрешение службы переднего плана Android в манифест:

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

Я ожидаю, что уведомление и обслуживание останутся. Не могу понять, почему вызывается OnDestroy.


person user3453575    schedule 07.07.2019    source источник
comment
К какому классу относится ваш PlayerService?   -  person ianhanniballake    schedule 08.07.2019
comment
android.app.Service   -  person user3453575    schedule 08.07.2019


Ответы (1)


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

Приложения, предназначенные для Android 9 (уровень API 28) или выше и использующие службы переднего плана, должны запрашивать разрешение FOREGROUND_SERVICE.

служба переднего плана

Изменить: я не могу определить проблему из вашего кода, но по моему опыту блок уведомлений должен быть в override fun onCreate()

person fangzhzh    schedule 07.07.2019
comment
@ user3453575 отредактировал, можете попробовать добавить уведомление в onCreate. я предполагаю, что это проблема - person fangzhzh; 08.07.2019