My application synchronizes data with a remote database through web service calls. I make these calls in IntentService so that they can run in the background (I call it SyncService).
The code to run my IntentService looks like this:
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);
It usually looks great. However, one of my friends, who is also a user of my application, often informs me that his data is not synchronized and is not displayed on our website. His device, as it turned out, showed symptoms when I was near. I connected his device to a computer, and here is what I found:
- The SyncService startup code was run (i.e.: the code above).
- I had a breakpoint inside the onHandleIntent method of my IntentService and it never hit.
- I checked his list of running services and SyncService was there and it works. Interestingly, it worked for about 20 minutes. I got the impression that the IntentService killed itself when it was all from Intents for processing.
- I made SyncService (and not the application) stop, and suddenly onHandleIntent started to click again and again. It was like all intentions were queued somewhere on the device, and now they were thrown into SyncService.
Does anyone have any ideas on what might be the problem? Do you think this is a problem with my application? With Android?
Again, I pass the Android message: "Launch this IntentService or send a message to an already running IntentService." At this moment, I have no control. A message never hits the IntentService. As soon as I exit the application, messages are sent to the IntentService and it does its job.
UPDATE: I think this code is fine, but I will express it, as many of you may want to see it.
Each intention that is included in the IntentService has an Extra value, indicating that the "type" of the call is used for me (that is, I am calling this web service or this web service, etc.). When an IntentService enters IntentService, I check the "type", and if there is an Intent in the queue for this type, I add Extra to it called "skip", so when it is reached, t search (basically, the IntentService can create a lot intentions, and it makes no sense to call this web service when this web service was called 20 seconds ago). This basically protects the application from website spam.
It is important to note that none of these codes gets in any way (as soon as the problem starts). onStartCommand is not called until the application is killed
@Override public int onStartCommand (Intent intent, int flags, int startId) {