Служба переднего плана уничтожается после получения намерения - PullRequest
1 голос
/ 08 июля 2019

Я пытаюсь написать музыкальный проигрыватель для 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.

1 Ответ

0 голосов
/ 08 июля 2019

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

...