Keep Service Alive which Starts from Broadcast Receiver - android

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.

Related

Doubts with BroadcastReceiver

I have an alarm that obviously calls a receiver and in the receiver it have to do some tasks and it may take some time to be done. But i heard that the onReceive() method is killed after some seconds.
I made a debug on my code and "stopped" inside the receiver and suddenly the debug stops, it happens because the onReceive() was killed?
So, what should i do?
But i heard that the onReceive() method is killed after some seconds
Correct. onReceive() is called on the main application thread. You want to get off of that thread as soon as possible. If your UI happens to be in the foreground when the broadcast is received, your UI will be frozen. Even if your UI is not in the foreground, you cannot take very long on that thread without your work being terminated.
So, what should i do?
Delegate the work to an IntentService, where you start that service in onReceive(). If the work may take 15+ seconds, I would recommend using WakefulBroadcastReceiver, so that you can ensure the device will stay awake long enough for your work to complete. But even then, "some time to be done" should be measured in seconds, maybe minutes.

Service Stops Logging On Activity Destroy

I am trying to run a service in the background, despite the lifecycle state of the Activity that creates it. To guarantee it's running, the service (in addition to the services it performs) also has a thread that logs once a second. Finally, it also has logs in onStartCommand and onDestroy.
I'm starting the service with startService() and then I bind to it. My understanding is that this should keep the service running, regardless of what happens to its creator Activity. However, if the Activity is destroyed, the service stops logging. If the Activity is simply paused, there is no problem.
The following is also true:
1) onDestroy is never called on the service
2) I never call stopService or stopSelf after calling startService
I'm trying to figure out why the service is dying (there are no exceptions) or at the very least why it's being paused and no longer logging.
A bound service typically lives only while it serves application component and does not run in the background indefinitely.
http://developer.android.com/guide/components/services.html
http://developer.android.com/guide/components/bound-services.html
Use syncadapter instead of service . if you want to transfer data between server and client .
http://developer.android.com/training/sync-adapters/creating-sync-adapter.html

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().

How android service responds when stopped by system

I have a question related to how Android service behaves when stopped by system. As per my understanding, if system (OS) stops the service due to resources needed, it is responsibility of system to start it again. In this scenario, system would not call onDestroy() when stopping service and onCreate() when starting service, rather it would just call onStartCommand().
If I am creating a thread in onStartCommand(), how would I cleanup thread when system stops service. If I dont stop thread, onStartCommand() would create a new thread.
I think, it may come down to onStartCommand() parameters (intent, flags and startId). They might be different when system starts service after stopping it because it needed resources. Could anyone tell me what would be difference in parameters when service started by startService() command or by system itself (after stopping it)
I can create thread in onCreate() but I am not sure if thread still exists when system stops service. What would be the best way to handle this kind of scenario.
Thanks
As per my understanding, if system (OS) stops the service due to resources needed, it is responsibility of system to start it again.
That depends upon what you return from onStartCommand(). START_NOT_STICKY, for example, means the OS does not have to start your service again.
In this scenario, system would not call onDestroy() when stopping service and onCreate() when starting service, rather it would just call onStartCommand().
Whether it calls onDestroy() would depend a bit on how the service is stopped (e.g., directly or via process termination). However, it should still call onCreate() on the new instance, if and when the OS restarts the service.
If I am creating a thread in onStartCommand(), how would I cleanup thread when system stops service.
Ensure that in onDestroy(), something happens that will cause the thread to go away. Either onDestroy() will be called (and your service can do its cleanup), or your process is being terminated (and your thread goes away with it).
Could anyone tell me what would be difference in parameters when service started by startService() command or by system itself (after stopping it)
There is a START_FLAG_REDELIVERY that will be in the flags passed to onStartCommand(), but AFAIK it will only be set if you return START_REDELIVER_INTENT from onStartCommand().

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.

Categories

Resources