0

I am designing an android music player app. The problem that I am encountering is regarding the dynamics of services and activities.

So this is what I have done.

I have a MainActivity and MusicService class.

The MusicService class has access to notification ( which would be shown if user exits of app while the music is playing ) and MediaPlayer

The MainActivity has the main UI and it binds to service.

Now the problem that I am encountering is:

  1. When the music is playing I have to move my activity to back by moveTaskToBack method, so that the service does not get killed if my activity is destroyed.

But as user swipes the activity/app from open apps list the service gets destroyed and the music gets stopped.

So my question is that, How can I make my service independent of MainActivity, so that even if the app is closed my service is running and I can control it from my ongoing Notification.

I don't want my app to be sitting in the background.

edit: Regarding foreground notification I am already doing that, when I am exiting my app I am showing the notification which is shown as MusicService.statForeground(..).

Rahul Verma
  • 25
  • 1
  • 8
  • Possible duplicate of [How can we prevent a Service from being killed by OS?](http://stackoverflow.com/questions/9696861/how-can-we-prevent-a-service-from-being-killed-by-os) – Ziem Mar 22 '16 at 10:14

1 Answers1

0

Make your service foreground by help of startForeground() method from the service itself.

For more info, visit the following link:

http://developer.android.com/guide/components/services.html#Foreground

Varun Kumar
  • 1,241
  • 1
  • 9
  • 18