0

I have an IntentService which is started from a BroadcastReceiver with startService(service). When I get new informations in the BroadcastReceiver the new infos are pushed through an intent with startService(service) again to the IntentService but then the service is restarted. Can I prevent this? I want to push new informations to the IntentService without restating it.

Cilenco
  • 6,951
  • 17
  • 72
  • 152
  • So you want an `IntentService` to not work like an `IntentService`? – 323go Mar 17 '13 at 16:45
  • I tried with a normal Service but then I had a problem with ANR so I switched to an IntentService. A normal Service was perfect only the ANR was the problem... so what can I do? The main difference between IntentService and a normal Servic is that the IntentService has a WorkerThread, right? Can I do this in a normal Service, too? How should I start the WorkerThread then if a new Intent comes to the Service? – Cilenco Mar 17 '13 at 17:30
  • Did you implement your code in `onHandleIntent` or in `onStartCommand`? – 323go Mar 17 '13 at 17:45
  • When I use an IntentService I implement my Code for receiving new informations in onHandleIntent. With a normal Service I get new informations over the onStartCommand function. – Cilenco Mar 17 '13 at 18:01

2 Answers2

1

Intent service are intended to be started with an intent, execute their job and finish. They are more like an asynctask from this point of view. This is the reason why your intentservice is restarting.

onHandleIntent should do some work and finish. You could do some tricks to make it blocking but that would go against the nature of intent services.

What you should do is to have a classic Service. If you are getting ANR errors, you should perform all the time intensive operations inside a thread or an asynctask hosted inside the service itself.

fedepaol
  • 6,834
  • 3
  • 27
  • 34
  • If I use a normal Service the `onStart` method is only called at the first time and wehen I call `context.startService(service)` a second time only the `onStartCommand` method is called, right? In the `onStartCommand` method I only have this code `final Bundle extras = intent.getExtras(); String string1 = extras.getString("string1"); String string2 = extras.getString("string2"); Notification nf = new Notification(string1, string2); data.add(nf); adapter.notifyDataSetChanged();` It puts a new item to my listView. Does this need so much time? – Cilenco Mar 18 '13 at 22:07
  • I'd say you can always rely on onStartCommand getting called (the first time as well). If you are getting ANR error you are performing some time intensive operation there, you need to identify them and place inside another thread. If you are not sure about which they are you can enable strictusermode – fedepaol Mar 18 '13 at 22:17
  • The StrictMode only says `03-18 23:58:28.494: D/StrictMode(15451): StrictMode policy violation; ~duration=1481 ms: android.os.StrictMode$StrictModeDiskReadViolation: policy=31 violation=2 03-18 23:58:28.494:D/StrictMode(15451):at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:1107) 03-19 00:01:50.234: D/StrictMode(15576): StrictMode policy violation; ~duration=413 ms: android.os.StrictMode$StrictModeDiskReadViolation: policy=31 violation=2 03-19 00:01:50.234:D/StrictMode(15576):at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:1107)` – Cilenco Mar 18 '13 at 23:04
  • which means that you are reading something from the storage. You should identify that and move it in an asynctask (or something more complex) – fedepaol Mar 18 '13 at 23:05
  • I don't read anything from the storage... I have only a PreferenceActivity but otherwise I don't read anything. – Cilenco Mar 18 '13 at 23:14
0

I assume you need to share some state for handling subsequent Intents inside your Service. I see two solutions:

  1. Use IntentService and save and restore this state inside onHandleIntent.
  2. Use started Service, and hold this state as a field inside your Service class. To prevent ANRs, process the Intents outside of UI thread, just like the IntentService does. To keep the Service running, just remove the stopSelf call from handleMessage.

If you can get away with restoring and saving state for each processed Intent, the first solution is safer, because your Service may be killed by the OS in case of running out of memory.

chalup
  • 8,358
  • 3
  • 33
  • 38
  • `process the Intents outside of UI thread` how should I do this? – Cilenco Mar 18 '13 at 23:33
  • See the IntentService implementation (link in my answer) to see how it can be done. Since you don't want your service to stop after all the Intents are processed, do not call stopSelf() (also mentioned in my answer). – chalup Mar 19 '13 at 21:15