Stopping a service once it has finished it's work - android

I have an Android Service that is started by my application and does some things in a threadPool using Executors.newCachedThreadPool()
Once it has finished doing it's work I would like the service to stopSelf(), how can I get the service to determine when it is no longer needed (ie, there are no more Threads executing) so that it can shut itself down automatically?

Do it the other way: the last Runnable should shut the Service upon completion.

Related

Does a Service stops its worker thread when the service itself is stopped?

I have two services in my app.
First Service listens for a broadcast from System.
Based on it the first service makes decision whether to start or stop a Second Service.
Now my Second service performs some network tasks by starting a separate worker thread.
My question is : when I will stop the 2nd service from 1st service, definitely onDestroy() will be called for 2nd Service, but would it also stop its worker thread or the worker thread will keep going until it finishes ?
Regards.
If you created the Thread yourself, then no, it won't be stopped automatically. You're responsible for killing it in onDestroy().

If an Android service is killed, are threads spawned by the service killed as well?

Suppose I implement a service that starts threads performing long-running operations.
If the service gets killed, will that automatically kill off those running threads as well?
Thanks.
A service runs in the same process as your app unless you specify otherwise in the manifest. If it's killed (i.e., crashes) then all threads belonging to the process will disappear, but I don't think that's what you're asking. Threads started by the service will not be terminated automatically if the service is stopped in the normal way, unless the service is running in another process and the process is also terminated. It may not be. Android tends to keep processes around even after the Activity or Service that was running in them has been stopped and destroyed.
I have found this to also be true.
I wanted a thread in a background service (Main UI process). I wanted the service to be killed when the main activity was killed, however I found that my threads would keep on running even when the activity and service (stopSelf) had stopped/been stopped.
I had to explicitly kill the thread. I did this by calling yourThread.interrupt().

Keep Service Alive which Starts from Broadcast Receiver

I have a major Issue,
I make a Broadcast receiver which apply on the Device boot up, so i need to start a new service for performing long-running operation,
So in the onReceive() method of Broadcast Receiver I make a new Intent and by this Start a new service,
Now my problem is that this Service executes only for short time, as soon as the onRecieve() method finish it process is also finished and my Service is also stops with the finishing of Receiver process.
So how I can do this, to keep alive the Process of Service which starts from the BroadcastReceiver.
Now my problem is that this Service executes only for short time, as soon as the onRecieve() method finish it process is also finished and my Service is also stops with the finishing of Receiver process.
That would only occur if you are calling stopService(), or the service is calling stopSelf(). The service has an independent lifecycle from the BroadcastReceiver. It will not even be started before onReceive() ends.
but in the Service I use the Separate Thread, but this thread is also stopped.
That will only occur if you are stopping the thread yourself. Android does not know about threads you create.
Now, eventually, your app's process will be terminated. With a running service, this could take anywhere from 30 minutes to a couple of days, depending on what else is all going on with the device, whether you are using startForeground(), etc. Once the process is terminated, everything goes away, background thread and all.

How to stop a service?

I have a service [IntentService] which runs a 'timer task' scheduled to run after every 5 minutes. I start the service when my application is installed.
Now i want to stop my service [service runs a 'timer task'], but when i use stopService() method i am unable to stop my service. I tried to put a log in the onDestroy() method of my service but stopService() does not reaches there.
Also, since stopService() returns boolean i logged its output, it is returning false.
How should i stop it??
I have a service [IntentService] which runs a 'timer task' scheduled to run after every 5 minutes.
Please do not do this. Users hate developers who do this and will force-stop your application using task killers or the Settings app. Please use AlarmManager, so you can take up less RAM and be more friendly to the user.
I start the service when my application is installed.
This is not supported. In particular, as of Android 3.1, this is impossible, even by the undocumented hack you might be using.
Now i want to stop my service [service runs a 'timer task'], but when i use stopService() method i am unable to stop my service.
Most likely, your service is already stopped. IntentService stops as soon as onHandleIntent() returns. TimerTask forks a thread to maintain its timer -- if you are creating this TimerTask in onHandleIntent(), you are leaking this thread. You have no way of ever stopping this thread. It will randomly go away once Android elects to terminate your process.
If, on the other hand, you get rid of the TimerTask and use AlarmManager, you can do your real periodic work in the IntentService's onHandleIntent() method.

Alternative to using Handler in a service

I am working on an application which needs to do a WiFi scan every 5-6 seconds. WiFiScanner class is implemented as a service and called from the main Activity. In order to repeat tasks every few seconds, I have used Handler with postDelayed with an interval of 5000 msecs. After installing on the device the application runs fine first time. Stopping the WiFi scan process, closing and immediately reopening the application causes it to crash. I suppose its because I haven't stopped the Handler explicitly in the main activity by calling removecallbacks on the runnable, instead clicking stop would simply stop the service. Here's what logcat throws on the error.
06-14 12:30:58.181: ERROR/AndroidRuntime(23534): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.net.wifi.SCAN_RESULTS } in com.test.example.WiFiScanner$1#2b0a3880
I was looking through stackoverflow and found suggestions to use an AlarmManager instead. But wouldn't this require me to implement a BroadcastReciever class for the purpose as it says here? Is there any other alternative to Handler to doing repetitive tasks in a service invoked from the Main Activity?
well you can just bind the service instead of starting it. that means it's a local service which will get stopped when there arent any more activities bound to it. but be carefull because the service runs in the ui thread so move things in a background thread. what i have commonly used in situations like this is a handler that postDelayed a runnable executing an async task where you can do whatever you want to do and then rescheduling it so it runs in some time period. also remove the callbacks when the service is destroyed and start it sticky so that if it is killed it restarts and you can also remove the callbacks on start.
As an alternative you can use an intent service or a simple service with alarms that is started by the alarm on specific time periods, that calls selfStop after it has completed a scan. but if you are running frequent checks then this creates an overhead because the service needs to be created over and over again (so better keep it running then).
there is also the timerTask class but i simply find this 2 solutions better. the timer class introduces a new thread
see: Timer
And This for an implementation
and here is someone that tried to do the same thing as you are:
Timer task and answers

Categories

Resources