Java IllegalStateException:在广播外部调用finishBroadcast()

Java IllegalStateException:在广播外部调用finishBroadcast(),java,android,android-mediasession,Java,Android,Android Mediasession,有人有解决办法吗 Fatal Exception: java.lang.IllegalStateException: finishBroadcast() called outside of a broadcast at android.os.RemoteCallbackList.finishBroadcast(RemoteCallbackList.java:303) at android.support.v4.media.session.MediaSessionCompat$Medi

有人有解决办法吗

Fatal Exception: java.lang.IllegalStateException: finishBroadcast() called outside of a broadcast
   at android.os.RemoteCallbackList.finishBroadcast(RemoteCallbackList.java:303)
   at android.support.v4.media.session.MediaSessionCompat$MediaSessionImplApi21.setCallback(Unknown Source:28)
   at android.support.v4.media.session.MediaSessionCompat.setCallback(Unknown Source:2)
   at com.test.player.myService.onCurrentPlayerChanged(Unknown Source:196)
   at com.test.player.myService.onCurrentPlayerChanged(Unknown Source:53)
   at com.test.player.myService$$Lambda$3.accept(Unknown Source:6)
   at io.reactivex.internal.operators.observable.ObservableDoOnEach$DoOnEachObserver.onNext(Unknown Source:7)
   at io.reactivex.internal.operators.observable.ObservableDoOnEach$DoOnEachObserver.onNext(Unknown Source:12)
   at io.reactivex.internal.operators.observable.ObservableFlatMap$MergeObserver.onSubscribe(Unknown Source:23)
   at io.reactivex.internal.operators.observable.ObservableFlatMap$MergeObserver.onSubscribe(Unknown Source:6)
   at io.reactivex.internal.operators.observable.ObservableFlatMap$MergeObserver.onNext(Unknown Source:51)
   at io.reactivex.internal.operators.observable.ObservableFlatMap$MergeObserver.onSubscribe(Unknown Source:23)
   at io.reactivex.internal.operators.observable.ObservableFlatMap$MergeObserver.onSubscribe(Unknown Source:6)
   at io.reactivex.internal.operators.observable.ObservableFlatMap$MergeObserver.onNext(Unknown Source:51)
   at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.drainNormal(Unknown Source:47)
   at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.run(Unknown Source:8)
   at io.reactivex.internal.schedulers.ScheduledRunnable.run(Unknown Source:13)
   at io.reactivex.internal.schedulers.ScheduledRunnable.call(Unknown Source)
   at java.util.concurrent.FutureTask.run(FutureTask.java:266)
   at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301)
   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
   at java.lang.Thread.run(Thread.java:764)
此堆栈跟踪来自fabric crashlytics

onCurrentPlayerChanged()
中,只需调用
stopForeground
服务即可

public void onCurrentPlayerChanged(int currentPlayer) {
    if (currentPlayer != LuxePreferences.PLAYER_ONE) {
        stopForeground(true);
        NotificationManagerCompat.from(this).cancel(PLAYER_NOTIFICATION_ID);
    }
}
下面是onCreate()和onStartCommand()

在startRadio()上,我被设置为
mediaSession.setActive(true)
以及stopRadio()中的mediaSession.setActive(false)

在设置通知时,将mediaSession播放状态设置为以下状态

if (isPlaying()) {
        mediaSession.setPlaybackState(stateBuilder
                .setState(PlaybackStateCompat.STATE_PLAYING, 0, 1).build());
        startForeground(PLAYER_NOTIFICATION_ID, notification);
    } else {
        mediaSession.setPlaybackState(stateBuilder
                .setState(PlaybackStateCompat.STATE_PAUSED, 0, 0).build());

        stopForeground(false);
    }
和onDestroy()


知道此异常的原因吗?

您不需要取消通知。只需使用:-

startForeground(NOTIFY_ID, notification.build());
开始和停止

 stopForeground(true);

方法<代码>停止前景(布尔重新通知)将清除通知。您可以从中了解它。

停止服务之前,只需释放所有资源。应用程序在某些设备上崩溃,而不是全部。你知道如何复制吗?你没有提供任何与
MediaSession
相关的代码。我发现了一个类似的问题,它可能会有所帮助。否则,您需要添加更多的信息与代码。
startForeground(NOTIFY_ID, notification.build());
 stopForeground(true);