3

I am building a music player like Spotify and want to save the last played song when the user clears the app from the "recents list" and as onDestory() is not guaranteed to run when the app is cleared from the recent list and Background service only runs for a minute in API level > 26. So, to me using a Foreground service seems the best possible way to do some chores when the app is cleared from the recent list. And, showing a new notification while the ExoPlayerNotification is shown in the notification area would seem odd. So, is there any way to attach a Foreground service with the ExoPlayerNotification so that I would be notified about the app getting removed in onTaskRemoved(). Please feel free to suggest if there are any better ways to achieve the same.

PS Not talking about what I tried as I already did in this question here

Neeraj Sewani
  • 3,952
  • 6
  • 38
  • 55
  • 2
    Use Android Media framework to separate your player from UI. https://developer.android.com/guide/topics/media-apps/audio-app/building-an-audio-app , once you move the player to the service, just keep last played song in the database and play it again when user opens the activity, you don't need any clean up in activity apart from releasing media framework. Do everything else in the service. Bring service to foreground when `onTaskRemoved` is called. – mallaudin Dec 17 '19 at 14:17

0 Answers0