3

I have a question about services: What would be better, create a thread within a service, or service within a thread?

The question comes because I am implementing an error reporter for my application, so that when the application fails it sends me a report with what has happened. My idea is a notification when an error occurs from here show activity Activity and creates a thread to retry delivery until it succeeds or reaches a minimum number of attempts.

Remaking the question, how is the system least likely to remove my process (if out of memory): With a thread within a service, or service within a thread?

I think the best is the first choice, but I saw a code in the same android people, here:

(line 640) code from google to keep alive a thread??

Where they use the second. What do you think about it?

I don't need an AsyncTask, because I don't need to interact with the UI thread, I'm not showing anything at this point to the user.

casperOne
  • 73,706
  • 19
  • 184
  • 253
user675319
  • 71
  • 10
  • Could you reformulate the part "My idea is a notification when an error occurs from here show activity Activity and creates a thread to retry delivery until it succeeds or reaches a minimum number of attempts."? You have an activity called `Activity`? And it creates the thread to retry? – The Student Mar 24 '11 at 17:08
  • of course, my idea is to send a notification to the user, and when he/she clicks on it, show an activity with two buttons (send and cancel). If the user clicks "send" now i'm starting a thread that tries to send errordata to a server, and in case of fail sleeps 30 minutes and try again, a maximum of 6 retries – user675319 Mar 25 '11 at 17:20
  • You say you don't need an AsyncTask becouse you is not showing anything to the user at that point. So I imagine it's a background task that will not freeze the GUI, right? Just confirming.. – The Student Mar 29 '11 at 13:15
  • As far as I know, Android will handles better a service then a thread. So a thread inside a service would be better.. But I would like someone to confirm that. – The Student Mar 29 '11 at 13:18

1 Answers1

5

Create your worker thread inside the service.

Ryan Reeves
  • 10,209
  • 3
  • 42
  • 26
  • You gave an answer.. But is there any advantage, at all? – The Student Mar 29 '11 at 13:15
  • You usually create a service to perform background work, outside the life cycle of your activity. So the advantage is a service is not bound to the activity life cycle and its execution can span multiple activities. – Ryan Reeves Mar 29 '11 at 15:26
  • Thanks everybody, I solved it by creating a service that creates a notification, and this notification generates an activity (when clicked) that launches a thread. I found this is the most stable combination, a bit embarrasing but works fine that is what i want! thanks again and regards! – user675319 Mar 29 '11 at 18:48