gpt4 book ai didi

java - 非法状态异常 : finishBroadcast() called outside of a broadcast

转载 作者:行者123 更新时间:2023-11-29 02:28:00 46 4
gpt4 key购买 nike

有没有人有解决办法?

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

 public void onCreate() {
super.onCreate();

// Create a MediaSessionCompat
mediaSession = new MediaSessionCompat(getApplicationContext(), "RadioService",
new ComponentName(getApplicationContext(), RadioMediaButtonReceiver.class), null);
mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS |
MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);

stateBuilder = new PlaybackStateCompat.Builder()
.setState(PlaybackStateCompat.STATE_STOPPED, 0, 1)
.setActions(
PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_STOP | PlaybackStateCompat.ACTION_PAUSE);
mediaSession.setPlaybackState(stateBuilder.build());
mediaSession.setCallback(mediaSessionCallback);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

if (MediaButtonReceiver.handleIntent(mediaSession, intent) == null) {
int action = intent == null ? ACTION_NONE : intent.getIntExtra(EXTRA_ACTION, ACTION_NONE);

if (action == ACTION_NONE) {
if (preferences.isRadioOn()) {
startRadio();
} else {
boolean removeNotificationOnStop =
intent != null && intent.getBooleanExtra(EXTRA_REMOVE_NOTIFICATION_ON_STOP, false);
stopRadio(removeNotificationOnStop);
}
} else if (action == ACTION_STOP) {
stopRadio(true);
stopSelf();
}
}

return Service.START_STICKY;
}

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

@Override
public void onDestroy() {
super.onDestroy();

//other stuff to release resources

if (mediaSession != null) {
mediaSession.release();
}
}

知道这个异常的原因吗?

最佳答案

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

startForeground(NOTIFY_ID, notification.build());

开始和停止

 stopForeground(true);

方法 stopForeground(boolean removeNotification); 如果您将 true 作为参数传递,将清除通知。您可以从 stopForeground 中了解它.

关于java - 非法状态异常 : finishBroadcast() called outside of a broadcast,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51165851/

46 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com