service.onDestroy() should only be called from the system and not manually

instead use service.stopService() which inturn calls stopSelf() and
triggers hopefully onDestroy() to be called. Eventually we have to make
sure that all ServiceConnections are closed to successfully stop the service
now!

Cleanup within stopService() and not only onDestroy()

So we make sure that all listeners can react to onServiceStopped()
and close their ServiceConnections. Afterwards the android framework
is ready to stop the Service.
This commit is contained in:
evermind 2021-06-22 17:24:14 +02:00
parent 0da8e28651
commit aaa3e20c5a
2 changed files with 12 additions and 4 deletions

View File

@ -178,7 +178,10 @@ public final class MainPlayer extends Service {
if (DEBUG) {
Log.d(TAG, "destroy() called");
}
cleanup();
}
private void cleanup() {
if (player != null) {
// Exit from fullscreen when user closes the player via notification
if (player.isFullscreen()) {
@ -191,9 +194,14 @@ public final class MainPlayer extends Service {
player.stopActivityBinding();
player.removePopupFromView();
player.destroy();
}
player = null;
}
}
public void stopService() {
NotificationUtil.getInstance().cancelNotificationAndStopForeground(this);
cleanup();
stopSelf();
}

View File

@ -857,7 +857,7 @@ public final class Player implements
Log.d(TAG, "onPlaybackShutdown() called");
}
// destroys the service, which in turn will destroy the player
service.onDestroy();
service.stopService();
}
public void smoothStopPlayer() {
@ -1097,7 +1097,7 @@ public final class Player implements
pause();
break;
case ACTION_CLOSE:
service.onDestroy();
service.stopService();
break;
case ACTION_PLAY_PAUSE:
playPause();
@ -1498,7 +1498,7 @@ public final class Player implements
Objects.requireNonNull(windowManager)
.removeView(closeOverlayBinding.getRoot());
closeOverlayBinding = null;
service.onDestroy();
service.stopService();
}
}).start();
}