Android Service doesnt restart by itself - android

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.

Related

What if pendingintent call getService() to start a living background service multiple times

What if PendingIntent call getService() to start a background service (which is live) multiple times?
My guess is that the background service will still be there except onStartCommand(Intent intent, int flags, int startId) will be called multiple times, Correct?
In my case I am trying to call requestActivityUpdates() to detect user's activity (moving, still,...). When there are detected activities (callback function called), the example here (https://github.com/googlesamples/android-play-location/tree/master/ActivityRecognition) uses the pendingintent to call intentservice to extract the results of those detected activities then broadcast it.
Since I need to process these detected activities in a background service, I am thinking can I use the pendingintent to start my background service directly instead of calling a intentservice and broadcast it to my service.
My guess is that the background service will still be there except onStartCommand(Intent intent, int flags, int startId) will be called multiple times, Correct?
If the service is still running, onStartCommand() will be called on the running instance.

Service: onCreate called again after restarting app

I pass through the data to be processed via intent:
Intent intent = new Intent(getActivity(), LocationService.class);
intent.putExtra....
...
startService(intent);
onCreate method called once while browsing app, but when I closed app and removed from task list(I checked, service is still runing), then I start app again - Service onCreate called again.
From the doc:
If the service is not already running, the system first calls
onCreate(), then calls onStartCommand().
Update:
What constant are you returning at the end of your onStartCommand
method?
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
Pleasee post the type of service and startmode
LocationService extends Service
Yes the android doc is correct , it will call the service's onCreate() only first time it is created , and then delivers all the intents to onStartCommand() .
But I've have came across these two google groups having the discussion that might be helpful to you
https://groups.google.com/forum/#!topic/android-developers/LtmA9xbrD5A
and https://groups.google.com/forum/#!topic/android-developers/H-DSQ4-tiac
Not much of a help , but might be of some interest .
Enjoy !

IntentService is killed after 30 mins when my app is in background

I've created a custom Background Service, who is extending IntentService. I'm calling this class when my app goes to background via .startService(). Firsly onStartCommand is called where I'm returning START_STICKY (like I've read some other posts to keep my service alive and re-create if needed when is killed due to low memory). After that I'm implementing onHandleIntent(intent) method where I'm getting the extras from the caller intent and here I'm starting a Timer to run every minute and execute simple code. But the problem here is that, when the app is in background, after 30 mins my service is killed and not re-created. Here is part of my code, so any suggestions would be perfect.
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
return START_STICKY;
}
#Override
protected void onHandleIntent(Intent workIntent) {
//here I'm initializing the Timer
}
class UpdateTimeTask2 extends TimerTask {
public void run() {
backgroundTimer();//here I'm doing some simple coding
}
}
So, as Kumar said, i should extend Service not IntentService. Calling of the service was the same as calling of the IntentService, the only difference was the unimplemented methods that were required by the Service class. Also I'm using Handler's postDelayed method instead of Timer and also in onStartComand I'm returning START_STICKY. So the service is not killed at all. Useful code can be found here and here. I hope this helps.
In this case a normal Service and a Handler should be all you need. As Pankaj wrote IntentServices are not designer for longrunning operations. You can then use the Handler's postDelayed method to excecute your code every minute.

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