java - startForegroundService() 没有调用 startForeground(),但它调用了

标签 java android android-notifications foreground-service remoteserviceexception

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

这里是设置通知删除 Intent 的地方:

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

configureServiceState(long action) 方法仅从我的 MediaSession 回调调用:onPlayonPauseonStop...显然,操作是要执行的预期操作。

从我的 UI 执行 onStop 时,或者从 UI 调用 onPause 后跟 onStop 时(镜像清除通知所需的操作),仅来自通知。

关于此错误,我能找到的所有信息是,当您调用 startForegroundService 但未在 5 秒内调用 startForeground 时,它可能会发生...但是 startForeground 在唯一一次 startForegroundService 被调用后立即被调用。

此外,onPlaybackStateChange 将转到 onStop 方法中的“正在播放” Activity ,这会触发该 Activity 运行 finish() 这样服务就不会以这种方式重新启动。

我在这里错过了什么?

其他详细信息:

  • 我的“正在播放” Activity 并未部分重启该服务,因为代码执行从未到达其任何方法。

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

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

  • 为前台尝试不同的通知 channel 与常规通知也没有区别

  • 将暂停的通知从锁定屏幕上滑开不会导致错误,只有在手机解锁时才会发生;无论我的应用程序是否真正打开

完全异常:

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 O API 26 及更高版本使用 NotificationChannel,否则您将遇到您遇到的错误。请参阅 Android 文档中的以下声明 - Create and Manage Notification Channels :

Starting in Android 8.0 (API level 26), all notifications must be assigned to a channel.

这是我用于构建媒体通知的方法的摘录(从中获取您需要的内容)。您可以看到有一个针对 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://stackoverflow.com/questions/45395669/notifications-fail-to-display-in-android-oreo-api-26
    @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)
}

我没有包含上面某些方法的详细信息,但方法名称应该是 self 注释 - 如果我可以包含更多详细信息,请告诉我。其中一些可能有些矫枉过正,但在您的情况下可能会解决问题。

我很确定这会解决您的问题。如果没有,我还有一些想法。

关于java - startForegroundService() 没有调用 startForeground(),但它调用了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50347101/

相关文章:

java - 在 JxMap 中为标记设置自定义图标

android - React Native ScrollView 在 Android APK 上崩溃

android - 关于android :textAppearance的问题

android - 如何扩展只有一个附加行的 InboxStyle 通知?

java - 将 int 从 python 套接字发送到 java 套接字

java - Thread.sleep 导致 ExecutorService 循环中发生意外行为

java - 在使用 @SpringBootTest 运行的测试中定义 tomcat 属性

android - touchDragged 在 libgdx 中如何工作?

java - 如何禁用在后台服务中运行的通知

android - 如何在 Android 5 (Lollipop) 中抑制锁定屏幕上的通知但让它在通知区域中?