Restart a background service if stopped - android

my question is simply about how do we restart an android service that runs in background in it's own thread from an activity if the service stops itself after completing certain tasks.

Simply call startService() from your activity:
http://developer.android.com/reference/android/content/Context.html#startService(android.content.Intent)

Make sure you are extending Service, not IntentService. And then start service in STICKY mode. It should remain active that way. Even if it dies for some reason (system may kill it etc.), it should restart on its own.
Read this link: START_STICKY and START_NOT_STICKY

Related

How can start Service several times while Service is alive

I want to do this acts with my Service :
1)start App
3)start a Service by App
4)update my UI by Service
5)close App but the Service keep work
6)sart App again
7)connect to Service (not run new Service)
I want in my service public variable during living Service do not change.
If you return START_STICKY from onStartCommand() then your Service will run unstil it is explicitly stopped. You can then decide how you want to stop your Service, either by something calling stopService() or by the Service calling stopSelf() when it no longer wants to run.
Please also note that Android can kill your Service pretty much whenever it wants to. If you have returned START_STICKY then Android will restart your Service after it kills it, but there is nothing you can do to ensure that Android will not kill your Service.

Android: Service started on boot, is not restarting when forced stop

I have a service which is successfully started on boot complete using a BroadcastReceiver. But when I am doing a force stop it is not restarting.
I tried starting the service from activity, and tried force stop, but in this case service restarted.
I am using Service.START_STICKY
In other question on stackoverflow, it is mentioned to user BaseContext not ApplicationContext. How do I get BaseContext from a BroadcaseReceiver
START_STICKY restart service, when OS kill it.
If you kill the app, it's not work.

Foreground service still alive even though its process dies in a crash

I have a Service running in the foreground, and an Activity that interacts with it. If the Activity crashes, Android kills the entire process, including the foreground Service and its associated Threads.
However, the ongoing notification provided by the Service does not go away, and upon closer inspection, Android's task manager reveals that the Service itself is still running.
How can I kill the foreground Service in this circumstance?
Have you override onStartCommand method of the Service? What value is it returning? If not, try to override it and return START_NOT_STICKY from it.
START_STICKY: If this service's process is killed while it is started, then leave it in the started state but don't retain this delivered intent. Later the system will try to re-create the service.
START_NOT_STICKY: If this service's process is killed while it is started, and there are no new start intents to deliver to it, then take the service out of the started state and don't recreate.
Not sure, as I have never worked on foreground services, but this might be the reason.
Are you sure the service is not running it its own process...
Also can you confirm whether the service is getting restarted..If its getting restarted-it is because you are returning START_STICKY from onStartCommand()

Android Background Service That Runs Forever

I need to make an app that will run forever at the background and if needed it opens activity for user UI. I made an Activity that is the main activity which all it does at it's onCreate is to call to startService(new Intent(this, MainService.class));
The problem is that after the onStart command of the service is being called the MainService class becomes null and it is stops running.
Do I need to start the service in a different way? Should I start a new thread for the service?
Thanks,
Nahum
if your want to continue your service running though app gt close.then you need to return STICKY like that. and also need to use BroadcastReciever.and your service will not run continuesly because if system need to release memory then it will kill but yes you can restart your service for sure. So i will suggest you to go through whole documentation and stuff of service Service and
Broadcast
it may helpful for you. and one thing there are preferences which process will kill first by system and so on..check it out.
You need to make a new thread in your service and start this service using command startForeground.
If you want your service to run forever, your code needs to be able to run forever too
onStartCommand {
while (1) {
..
..
//call your activity?
..
..
}
}

how to start a service properly and keep it alive?

I know there are other question with the same topic, but I didn't find an answer to my questions.
my goal is to have a service which works on the background as a location listener, and it won't be stopped when the application is stopped (either by a task killer).
currently, I'm starting the service with startService(Intent) if it the service isn't started already and bind to it using bindService(Intent,ServiceConnection, 0).
now, the first problem is that my application crashes but the service has started, and when I run the application again it works.
the second problem, is that if I kill my application using advanced task killer, it kills my service as well, although in the Service page it says that the service will be stopped when no bounded clients left and if stopService() or stopSelf() have been called.
and it won't be stopped when the application is stopped (either by a task killer).
Fortunately, this is not possible. If your user wishes your service to stop, the user can stop the service via a task killer or the Manage Services screen in Settings.
currently, I'm starting the service with startService(Intent) if it the service isn't started already and bind to it using bindService(Intent,ServiceConnection, 0).
Usually, you only use one or the other, not both.
the second problem, is that if I kill my application using advanced task killer, it kills my service as well, although in the Service page it says that the service will be stopped when no bounded clients left and if stopService() or stopSelf() have been called.
No, because you called startService() in addition to bindService().
The service stops when the application is closed by the task manager. If this could not be possible every app would have its own service running without any user control over them. You could start the service at boot up and then when the user uses task manager to close, you could restart the service.

Categories

Resources