Service: onCreate called again after restarting app - android

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 !

Related

Start Service on Multiple Activities While Passing Data on Each Activity [duplicate]

This question already has an answer here:
Multiple activities binding to a service
(1 answer)
Closed 7 years ago.
I want to create this service to work with multiple activities. I mean each activity will be able to send a data to this service and get a data from it.
Basically, i would suggest that you start the service in application class and send broadcast to service whenever its needed to send any data to service.But make sure the service is running.
When you create a Service you should override the onStartCommand() method so if you closely look at the signature below, this is where you receive the intent object which is passed to it:
public int onStartCommand(Intent intent, int flags, int startId)
So from an activity you will create the intent object to start service and then you place your data inside the intent object for example you want to pass a userID from Activity to Service:
Intent serviceIntent = new Intent(YourService.class.getName())
serviceIntent.putExtra("UserID", "123456");
context.startService(serviceIntent);
When the service is started its onStartCommand() method will be called so in this method you can retrieve the value (UserID) from the intent object for example
public int onStartCommand (Intent intent, int flags, int startId){
String userID = intent.getStringExtra("UserID");
return START_STICKY;
}
Note: the above answer specifies to get an Intent with getIntent() method which is not correct in context of a service
In this scenario, you should consider using IntentService. IntentService is a special kind, which handles a queue of work, sent through Intents.
When the first activity calls startService(), the service is started and begins its work. Consequent calls to startService, will queue them and results in the works being processed one after another, until the last sent intent is done, and then service will shutdown it self. It's pretty straightforward to use and takes care of the heavy lifting and all the boilerplate code you should write.
For further study, you can take a look at this .

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.

Android service being stopped, how to notify applications?

I have an application which works like a framework - other apps can register a PendingIntent with my app which triggers them at some point in the future. I am keeping the PendingIntents in an array list in a service and then iterating over them to trigger. To register an intent the application calls "startService()" with a PendingIntent set within the intent bundle.
At some point, my service is being closed - or more specifically my array list is being emptied (which can only happen by Android killing the service) and as such I loose my PendingIntents for the registered apps.
How can I keep my service alive, or more specifically keep my PendingItents from being lost? I don't want to go against the Android system mechanisms and create a hacky effort, I would like to know the way in which Google think this should be done.
When a service is killed, it's onDestroy() method is called. You could probably over-ride the onDestroy() method and do something like:
public void onDestroy(){
Intent intent = new Intent();
intent.setAction("SERVICESTOPPED");
sendBroadcast(intent);
super.onDestroy();
}
Have a BroadcastReceiver that listens to this intent and handle the situation as and when it happens.
Alternatively, in the onstartCommand(), if you specify the return value as START_STICKY, the OS will restart your service if it gets killed by OS. Generally this happens with long running serices
Have you tried with START_STICKY?
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Put your code here
return START_STICKY;
}
You can try START_REDELIVER_INTENT

IntentService not firing

My application synchronizes data with a remote database via web service calls. I make these calls in an IntentService so they can run in the background (I call it SyncService).
The code to launch my IntentService looks like so:
Intent intent = new Intent();
intent.setClass(appContext, SyncService.class);
// place additional values in intent
intent.putExtra("data_type", SyncService.ITEM_TRACKING);
intent.putExtra("user_id", intUserId);
// call SyncService
appContext.startService(intent);
This, normally, looks great. However, one of my friends, who is also a user of my app, often tells me his data doesn't sync and get displayed on our website. His device happened to be displaying the symptoms while I was around. I plugged his device into my computer and here is what I found:
The code to launch SyncService (ie: the code above) was hit.
I had a breakpoint inside the onHandleIntent method of my IntentService and it never gets hit.
I checked his device's list of running services and SyncService was there and running. Interestingly, it had been running for about 20 minutes. I was under the impression that IntentService killed itself when it was all out of Intents to process.
I force stopped the SyncService (not the app) and, all of the sudden, onHandleIntent started getting hit over and over. It was like all the Intents were queued up somewhere on the device and were just now getting thrown at the SyncService.
Does anyone have any ideas as to what may be the problem? Do you think it's an issue with my app? With Android?
Again, I am handing a message to Android saying, "Start this IntentService or send the message to the already running IntentService." At that point, I have no control. The message never gets to the IntentService. Once I force quit the app, the messages get sent to the IntentService and it does its job.
UPDATE: I think this code is fine, but I'll put it up since a lot of you may want to see it.
Every Intent that comes in to the IntentService has an Extra denoting what "type" of call is to me made (ie: do I call this web service or that web service, etc). When an Intent comes in to the IntentService, I check the "type" and, if there is already an Intent in the queue for that type, I add an Extra to it called "skip" so, when it is reached, I don't execute the search (basically the IntentService can build up lots of Intents and it makes no sense to call this web service when this webservice was called 20 seconds ago). It basically protects the app from spamming the website.
It is important to note that none of this code is hit anyway (once the problem starts occurring). onStartCommand does not get called until the app is killed
#Override
public int onStartCommand (Intent intent, int flags, int startId) {
// here be dragons
// overriding this method and adding your own code is dangerous. i've wrapped
// my code in a try/catch because it is essential that the super method be called
// every time this method is entered. any errors in my code should not prevent this
// or the app will explode.
try {
if (flags == 0 && intent != null && intent.hasExtra("data_type")) {
Integer intDataType = intent.getExtras().getInt("data_type");
if (!mCurrentTypes.containsKey(intDataType)
|| !mCurrentTypes.get(intDataType)) {
mCurrentTypes.put(intDataType, true); // put this type in the list and move on
}
else {
intent.putExtra("skip", true); // mark this Intent to be skipped
}
}
}
catch (Exception e) {
// Log.e("Error onStartCommand", "error: " + e);
}
return super.onStartCommand(intent, flags, startId);
}
private void processIntent(Intent intent) {
// do stuff if no "skip" Extra
mCurrentTypes.put(intDataType, false);
}
There is definitly something that keeps your service running on your friend's device. If so all subsequent call to this intent service are queued until the current one finishes. If it doesn't finish then you will get what you have : next services won't start.
You should double check that :
you give proper timeouts to nework operations
you give proper timeouts to nework connections operations
there is no race condition between threads.
you log any exception that can occur inside the service, you don't wanna loose that kind of information.
Afterwards, if you think everything is green : just log what the service does and use some bug reporting mechanism to get it automatically sent from your friends device. A simple solution could be to use bugsense or equivalent.
Next, put in place some kind of watchdog : a thread that will go on running until your service stops (you just tell your thread to stop when service is stopped). The thread will have to stop your service after some time limit has been passed.
This watchdog thread could be put inside the service itself, or outside, although this may be more complex to put in place.
This answer suggests a solution that worked for me in similar situations. It doesn't fix your current code but suggests another, perhaps simpler (and easier to debug) option:
Add a BroadcastReceiver to your calling Activity that listens for SUCCESS Intents from the IntentService.
In your calling Activity, include the logic for when to start the IntentService (and don't include it in the IntentService). The logic is:
Call startService() and set a flag in the calling Activity to CANNOT_CALL.
If the Activity's BroadcastReceiver has not received a SUCCESS broadcast from the IntentService, then startService() can not be called again.
When the Activity does receive a SUCCESS intent, set the flag to CAN_CALL, and startService() can be called when the timer hits again.
In your IntentService, write your onStartCommand() like so:
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
return START_STICKY;
}
In you IntentService, when you've received, parsed and stores the web service response, call sendBroadcast() with an Intent with custom action SUCCESS.
This logic is just an outline and has to be fine-tuned for error messages from the web service that have to be broadcast from IntentService to the listening Activity.
Hope this helps.
It seems to me that setting a set of flags to your Intent may solve the problem.
Intent intent = new Intent();
intent.setClass(appContext, SyncService.class);
// This way
intent.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK|Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
You can make your Service start as fresh using the above flag in a fresh task.
One more comment. It's not an answer for your question. However, it may affect overall behavior of a service.
You do following:
return super.onStartCommand(intent, flags, startId);
Internally Service.onStartCommand() looks like following
public int onStartCommand(Intent intent, int flags, int startId) {
onStart(intent, startId);
return mStartCompatibility ? START_STICKY_COMPATIBILITY : START_STICKY;
}
mStartCompatibility is false if your app target SDK API 7 or later (which is most likely a case).
So, as result your service will be started as START_STICKY.
Here is piece from documentation:
For started services, there are two additional major modes of operation they can decide to run in, depending on the value they return from onStartCommand(): START_STICKY is used for services that are explicitly started and stopped as needed, while START_NOT_STICKY or START_REDELIVER_INTENT are used for services that should only remain running while processing any commands sent to them. See the linked documentation for more detail on the semantics.
Base on what you have described, I recommend to replace "return super.onStartCommand(intent, flags, startId);" to "return START_NOT_STICKY;"

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.

Categories

Resources