How to restart a killed service automatically? - android

When a service has been killed, how to restart it automatically?
sometimes without even calling onDestroy()

I inherited an IntentService, so I had to be gentle.
It worked for me when I overrode onStartCommand() but
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
return START_STICKY;
}
That is, let the parent do what it should and return START_STICKY.

Overrides the onStartCommand() and give START_STICKY or START_REDELIVER_INTENT (depends on your needs) as the return value. The system will then make sure to restart your service until you explicitly stop the service.
http://developer.android.com/reference/android/app/Service.html#START_REDELIVER_INTENT
http://developer.android.com/reference/android/app/Service.html#START_STICKY

If you have your service killed, the system will try to restart it later.
Read more.

Related

Android Service doesnt restart by itself

In my application I have an activity and a service (extends IntentService ). the service's onStartCommand looks like below
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
return START_REDELIVER_INTENT;
}
My onHandleIntent method:
#Override
protected void onHandleIntent(Intent intent) {
while(continueLoop){ //continueLoop is controlled by the Binder
//Do stuff
}
}
I also bind to the service from activity, so I can break the infinite loop. I started the app and it's service, and then started other applications, after a while my Activity got stopped and destroyed, so is my Service. When I close the other applications using task manager , the service doesn't start by itself.
I waited and then launched my app, as soon as activity is launched service also started. I thought the android system will restart the service automatically when memory is available. Am I missing something or should i wait longer.
Thanks in advance
If you read this IntentService you'll see that
onStartCommand(Intent intent, int flags, int startId)
You should not override this method for your IntentService.
Instead
The IntentService class exists to simplify this pattern and take care of the mechanics. To use it, extend IntentService and implement onHandleIntent(Intent).
Per the IntentService documentation:
Clients send requests through startService(Intent) calls; the service is started as needed, handles each Intent in turn using a worker thread, and stops itself when it runs out of work.
If you are binding to the Service and/or controlling the lifecycle of the service yourself, then you should use a Service and not an IntentService.

Can we create an IntentService as STICKY?

The document says intentservice calls stopself implicitly. So was wondering if intentservice can be made Sticky.
Thanks
sticky is like a property (it´s not a property) that you can give to a service, and it gets activated after the system shuts it down because of low memory, when the system has enough memory it will restart the services that return START_STICKY on their onStartCommand, otherwise the will remain off. In IntentServices you cannot return START_STICKY (at least i haven't found the way) but what you can do is:
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
return START_REDELIVER_INTENT;
}
currently i´m using it in my app and it works well

Android keep BroadcastReceiver running in background

I did a service starts a BroadcastReceiver. The BroadcastReceiver is working only if the app is shown i recent apps screen. When I remove the app from recent apps screen the BroadcastReceiver stops.
How can I save the BroadcastReceiver always in background?
Your service might get killed along with your activity
You should add process=":background" in your manifest file within the <service /> node.
Or, alternatively,
In your service, you must return START_STICKY
public int onStartCommand(Intent intent, int flags, int startId)
{
super.onStartCommand(intent, flags, startId);
return START_STICKY;
}
declare the BroadcastReceiver in its own class, and add it to the Android Manifest, instead of registering it with some context.
This should be a simple starting point for you
Whoops I'm wrong. http://developer.android.com/reference/android/content/Intent.html#ACTION_BATTERY_CHANGED

Stopping an IntentService

Does anyone know if there is a way of stopping an IntentService without it finishing its work thread and stopping itself?
Simple question, but I couldn't find the answer in the documentation. Is there a simple way of stopping it?
Thanks
bevor a message to a service is enqueued onStartCommand is called. which forwards the message for queueing. so you could just override onStartCommand, something like that:
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent.getAction().equals("Stop"))
stopSelf();
onStart(intent, startId);
return START_NOT_STICKY;
}
cheers
You should be able to call stopSelf();
http://developer.android.com/reference/android/app/Service.html#stopSelf()
I currently stumble upon this requierement for an app i am working on. I will try using onStartCommand to send a message to the Intent Service to stop working (for example, setup a boolean flag stopWork = true) and evaluate it during the working job or before the next queued task. The IntentService wont stop inmediately but will skip all pending tasks. Hope it helps. Gonna try it myself also.

Restarting my android service?

I want to restart my android service if some preferences have changed, but there's nothing like a restart method in the Service class? Is there any way to restart my service, except creating some kind of reset method that resets all class variables etc. ? Thanks for any hint!
I think that calling it again, will call onStartCommand() again.
This might do the work for you.
*it will run onCreate() if it's not alive.
Call startService(intent) again, that will do the trick
You have to override the onStartCommand() in your service class as follows:
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, START_STICKY, startId);
return START_STICKY;
}
The constant variable START_STICKY indicates the system to restart the service if it get killed.
You should have/implement a listener to detect and inform when preference changes and you should register for that listener inside onStart() method and un-register that listener inside onDestroy().

Categories

Resources