Restart service when killed by system automatically

Asked

Viewed 29 times

0

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

1 answer

2


Good night.

So, how does service Starts work.

In case you send a startService(), it will be started with the return setting of the onStartCommand()

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

I will give an example in which I used, to see if understanding is better. I created a Mediaplayer, which should be running even with the dead application, similar to Googleplay Musica.

When selecting a song, I would give a startService() on my player service and right 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 player screen, the player would be an extra application, if the person opened the player, it would give bind, otherwise, would not have any connection with the player, soon, the service would be alive only by startService()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.