有没有人有办法解决吗?
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()上,我也在stopRadio()
mediaSession.setActive(true);
中也设置了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);
如果将true作为参数传递,方法
stopForeground(boolean removeNotification);
将清除通知。您可以从stopForeground阅读有关它的信息。