调用startForegroundService()方法没有调用startForeground()方法,但它确实已经在后台运行了。

11

我在我的Android服务中使用了Context.startForegroundService()没有然后调用Service.startForeground(),但我无法弄清楚为什么会发生这种情况。

我的应用程序用于媒体流,只有当您从前台通知中暂停(将其变为常规通知),然后向左滑动通知时才会出现此错误,这旨在停止我的服务。

下面是唯一调用startForegroundServicestartForegroundstopForeground方法的函数:

private void configureServiceState(long action) {
        if (action == PlaybackStateCompat.ACTION_PLAY) {
            if (!mServiceInStartedState) {
                ContextCompat.startForegroundService(
                        StreamingService.this,
                        new Intent(
                                StreamingService.this,
                                StreamingService.class));
                mServiceInStartedState = true;
            } startForeground(NOTIFICATION_ID,
                    buildNotification(PlaybackStateCompat.ACTION_PAUSE));
        } else if (action == PlaybackStateCompat.ACTION_PAUSE) {
            stopForeground(false);

            NotificationManager mNotificationManager
                    = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

            assert mNotificationManager != null;
            mNotificationManager
                    .notify(NOTIFICATION_ID,
                            buildNotification(PlaybackStateCompat.ACTION_PLAY));
        } else if (action == PlaybackStateCompat.ACTION_STOP) {
            mServiceInStartedState = false;
            stopForeground(true);
            stopSelf();
        }
    }

这里是我的通知删除意图设置的位置:

.setDeleteIntent(
                        MediaButtonReceiver.buildMediaButtonPendingIntent(
                                this, PlaybackStateCompat.ACTION_STOP));

这个configureServiceState(long action)方法只从我的MediaSession回调函数调用:onPlayonPauseonStop...显然,动作是要执行的预定动作。

当我从 UI 执行 onStop 或者从 UI 调用 onPause 后跟随着 onStop(镜像所需清除通知的操作)时,错误不会发生,仅在从通知中调用时发生。

我能找到关于这个错误的所有信息都是它应该在你调用startForegroundService但5秒内未调用startForeground时发生... 但是startForeground是在唯一一次调用 startForegroundService之后立即调用的。

此外,在onStop方法中,一个onPlaybackStateChange将传输到我的“现在播放”活动,触发那个活动运行finish(),以便服务不会通过这种方式重新启动。

我错过了什么?

额外细节:

  • 服务没有被“正在播放”的活动部分重启,因为代码执行永远不会进入其任何方法。

  • 在产生错误之前,代码执行似乎也不会重新进入configureServiceState

  • 如果我在最后一个可能的点(我的服务中onStartCommand中的MediaButtonReceiver.handleIntent(mMediaSession, intent);)添加断点,在这里暂停执行并尝试调试,将导致调试器在暂停后不久断开连接。

  • 尝试使用不同的通知渠道来区分前台和常规通知也没有任何区别

  • 从锁定屏幕向左划动暂停的通知不会导致错误,仅在手机解锁后发生;无论我的应用程序是否实际打开。

完整异常信息:

android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground()
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1870)
                      at android.os.Handler.dispatchMessage(Handler.java:105)
                      at android.os.Looper.loop(Looper.java:164)
                      at android.app.ActivityThread.main(ActivityThread.java:6809)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

我的测试设备运行的是Android 8.0版本,项目的最小API为21,目标API为27。设备API版本为26。


最近我在创建媒体服务时遇到了这个问题(虽然这可能并不完全相关)。请在您的问题中添加您的Android版本和API级别,我会尽力帮助您。 - Benjamin Scholtz
谢谢。我已经添加了那个,以及在调试期间发现的另一件事情。 - transiti0nary
@transiti0nary 如果 (action == PlaybackStateCompat.ACTION_PLAY) 这个条件在5秒内被执行了吗?我的意思是这个条件是否为“真”,并且 ACTION_PLAY 在5秒内被触发了? - Sagar
服务启动后5秒内?我不确定。为了安全起见,我也可以在onCreate中触发它,但是否有函数来检查前台通知是否存在,以便在播放从暂停转换到播放时(当onCreate不会被调用时)仍然可以在此函数中启动它? - transiti0nary
3个回答

6
您需要在 Android O API 26 及以上版本使用 NotificationChannel,否则您将遇到错误。请参阅来自 Android 文档的以下语句 - 创建和管理通知渠道:

从 Android 8.0 (API 级别 26) 开始,所有通知都必须分配到一个渠道。

下面是我用于构建媒体通知的一种方法摘录(根据您的需求自取)。您可以看到有一个检查 Android O 设备的特定方法来处理该情况:
private fun compileNotification(context: Context, action: NotificationCompat.Action, mediaSession: MediaSessionCompat, controller: MediaControllerCompat, mMetadata: MediaMetadataCompat, art: Bitmap?, mPlaybackState: PlaybackStateCompat) {

    val description = mMetadata.description

    // https://dev59.com/pVcO5IYBdhLWcg3wsTnM
    @TargetApi(26)
    if(Utils.hasO()) {
        val channelA = mNotificationManager?.getNotificationChannel(NotificationChannelID.MEDIA_SERVICE.name)

        if(channelA == null) {
            val channelB = NotificationChannel(NotificationChannelID.MEDIA_SERVICE.name,
                    "MediaService",
                    NotificationManager.IMPORTANCE_DEFAULT)
            channelB.setSound(null, null)

            mNotificationManager?.createNotificationChannel(channelB)
        }
    }

    val notificationBuilder = if(Utils.hasLollipop()) {
        NotificationCompat.Builder(context, NotificationChannelID.MEDIA_SERVICE.name)
    } else {
        NotificationCompat.Builder(context)
    }

    notificationBuilder
            .setStyle(android.support.v4.media.app.NotificationCompat.MediaStyle()
                    // Show actions 0,2,4 in compact view
                    .setShowActionsInCompactView(0,2,4)
                    .setMediaSession(mediaSession.sessionToken))
            .setSmallIcon(R.drawable.logo_icon)
            .setShowWhen(false)
            .setContentIntent(controller.sessionActivity)
            .setContentTitle(description.title)
            .setContentText(description.description)
            .setLargeIcon(art)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setOngoing(mPlaybackState.state == PlaybackStateCompat.STATE_PLAYING)
            .setOnlyAlertOnce(true)

            if(!Utils.hasLollipop()) {
                notificationBuilder
                        .setStyle(android.support.v4.media.app.NotificationCompat.MediaStyle()
                                // Show actions 0,2,4 in compact view
                                .setShowActionsInCompactView(0,2,4)
                                .setMediaSession(mediaSession.sessionToken)
                                .setShowCancelButton(true)
                                .setCancelButtonIntent(MediaButtonReceiver.buildMediaButtonPendingIntent(context,
                                        PlaybackStateCompat.ACTION_STOP)))
                        // Stop the service when the notification is swiped away
                        .setDeleteIntent(MediaButtonReceiver.buildMediaButtonPendingIntent(context,
                                PlaybackStateCompat.ACTION_STOP))
            }

    notificationBuilder.addAction(NotificationCompat.Action(
            R.drawable.exo_controls_previous,
            "Previous",
            MediaButtonReceiver.buildMediaButtonPendingIntent(context,
                    PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS)))
    notificationBuilder.addAction(NotificationCompat.Action(
            R.drawable.ic_replay_10_white_24dp,
            "Rewind",
            MediaButtonReceiver.buildMediaButtonPendingIntent(context,
                    PlaybackStateCompat.ACTION_REWIND)))

    notificationBuilder.addAction(action)

    notificationBuilder.addAction(NotificationCompat.Action(
            R.drawable.ic_forward_10_white_24dp,
            "Fast Foward",
            MediaButtonReceiver.buildMediaButtonPendingIntent(context,
                    PlaybackStateCompat.ACTION_FAST_FORWARD)))
    notificationBuilder.addAction(NotificationCompat.Action(
            R.drawable.exo_controls_next,
            "Next",
            MediaButtonReceiver.buildMediaButtonPendingIntent(context,
                    PlaybackStateCompat.ACTION_SKIP_TO_NEXT)))

    (context as MediaService).startForeground(NOTIFICATION_ID, notificationBuilder.build())
}

在你的onStop()回调中,你需要在服务内调用stopSelf()。然后,当你的服务onDestroy()方法被调用时,你需要清理一些东西(根据你的情况)如下:

override fun onDestroy() {
    super.onDestroy()

    abandonAudioFocus()
    unregisterReceiver(mNoisyReceiver)

    //Deactivate session
    mSession.isActive = false
    mSession.release()

    NotificationManagerCompat.from(this).cancelAll()

    if(mWiFiLock?.isHeld == true) mWiFiLock?.release()

    stopForeground(true)
}

我没有为上面的一些方法提供详细说明,但是方法名称应该是自注释的 - 如果需要更多细节,请告诉我。其中一些可能过于复杂,但在您的情况下可能解决问题。

我非常确定这将解决您的问题。如果不行,我还有一些想法。


谢谢,如果可以的话,我今天稍后会尝试一下。不过有一个问题,我的通知构建函数是否运行这里显示的通知渠道函数:https://developer.android.com/training/notify-user/channels,这样做会产生相同的效果吗?另外,我注意到我的构建器没有额外的“setStyle”,如果!Utils.hasLollipop(),并且始终使用此构造函数NotificationCompat.Builder(context, NOTIFICATION_CHANNEL),而不考虑hasLollipop。这可能与此有关吗? - transiti0nary
在这种情况下,可能是不同的问题 - 我仍然认为如果不太麻烦,尝试上述方法可能是值得的。通常,兼容性库确实处理不同的Android版本检查。 - Benjamin Scholtz
@transiti0nary,我已经添加了关于如何处理我的媒体服务中的onStop()方法的更多细节。请告诉我这是否有所帮助。 - Benjamin Scholtz
在调用stopSelf()之前,你的代码中调用stopForeground(true)可能会出现问题 - stopSelf()不能立即终止服务。 - Benjamin Scholtz
如果这个或任何答案解决了您的问题或在任何方面有所帮助,请考虑通过点击复选标记来接受它。这向更广泛的社区表明您已经找到了解决方案,并为回答者和您自己赢得了一些声誉。没有义务这样做。 - Benjamin Scholtz

5

我在这上面浪费了太多时间。我不确定你是否遇到了相同的问题,但在我的情况下,我一直收到这个异常是因为我的NOTIFICATION_ID0...使用任何其他值似乎都可以解决这个问题。-_-


虽然这里不是这种情况,但我也经历过!花了很长时间才弄清楚。 - Benjamin Scholtz

1
我曾经遇到过同样的问题。问题出在使用PendingIntent.getForegroundService()来更新媒体服务,而应该使用PendingIntent.getService才能解决这个问题。请注意保留HTML标签。

有趣,但我不认为我直接使用那种方法。通知交互会发送MediaButton事件,由我的服务接收并处理。 - transiti0nary
我在Android 8、9上遇到了同样的问题,将PendingIntent.getForegroundService()替换为PendingIntent.getService()解决了它。 - DzungPV

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接