【问题标题】:Foreground service is destroyed after receiving the pending intent前台服务在收到待处理的意图后被销毁
【发布时间】:2019-07-07 21:13:15
【问题描述】:

我正在尝试为 android 编写一个音乐播放器,并正在使用前台服务来运行音乐播放器。我正在从 UI 控件向服务发送一个pendingIntent 来播放歌曲。 收到意图后,立即调用 onStartCommand 和 onDestroy。我不确定如何停止 onDestroy 调用。

我尝试将挂起的 Intent 更改为 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");

我还在manifest中添加了那个android前台服务权限:

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

我希望通知和服务能够持续。无法弄清楚为什么要调用 OnDestroy。

【问题讨论】:

  • 你的PlayerService扩展了什么类?
  • android.app.Service

标签: android android-pendingintent foreground-service


【解决方案1】:

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

面向 Android 9(API 级别 28)或更高版本并使用前台服务的应用必须请求 FOREGROUND_SERVICE 权限

foreground service

编辑:我无法从您的代码中看出问题,但根据我的经验,通知块应该在override fun onCreate()

【讨论】:

  • @user3453575 已编辑,您可以尝试将通知添加到 onCreate。我猜这就是问题所在
猜你喜欢
  • 2021-01-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-19
  • 1970-01-01
  • 1970-01-01
  • 2015-06-28
  • 2019-01-28
相关资源
最近更新 更多