Restart service when killed by system automatically

0

I'm using a Service to perform a background task, when I start it by startService() and put it in onStartCommand() return START_STICKY; it restarts whenever the system forces the application to close. But when I start this same service using BindService() it does not automatically restarta the service.

    
asked by anonymous 09.04.2017 / 00:46

1 answer

2

Good evening.

So how does service starts?

If you send a startService() , it will start with the onStartCommand() setting

However, if you start via BindService() it will be a service attached to that class, when it ceases to exist, it will be terminated. so it does not pass through a onStartCommand()

I will give an example in which I used it, to see if it gets better understanding. I created a MediaPlayer, which should be running even with the dead application, similar to GooglePlay Music.

When selecting a song, I gave startService() to my player service and soon after a bindService() to be able to update the service through the application, since I would need to be able to play, pause, etc ... however not always I would be on the screen of the player, the player would be an extra of the application, if the person opened the player, it would bind, otherwise it would not have any connection with the player, so the service would be alive only by startService() / p>     

09.04.2017 / 06:15