0

I have read that a service is different from thread because it is not affected by the current activity. But threads are. But Looper is also same like a service which doesn't get affected by the current activity and they both run in background.

So what basically differs a service and a looper... ?

Please help me out. I am totally confused in these topics.

Neha Gupta
  • 847
  • 1
  • 8
  • 15

1 Answers1

1

Looper is a class which is used to execute the Messages(Runnables) in a queue. Normal threads do not have any queue. For example, Simple threads do not have any queue. They are for one time execution and after the end of the code the thread will be stopped/killed and it will not able to run another Message(Runnable).

Usage If you wants to execute multiple messages(Runnables) then you should use the Looper class which is responsible for creating a queue in the thread. For example. If you are writing an application which downloads files from the internet then you can use Looper class to put all the files in the queue to be downloaded.

Service runs in a different process, Say in an application if it gets crashed or the process in which application was running gets killed then Async Task/looper will also be killed but not Service because service was running in a differnt process. Looper run in the same thread as of app unkike Service .Also Android Recommends to use Async task rather than Loopers.

Jitender Dev
  • 6,907
  • 2
  • 24
  • 35
  • Can we use services in this case or AsyncTask class because AsyncTask class is also responsible for handling multiple tasks simultaneously.. – Neha Gupta Sep 26 '13 at 07:38
  • 1
    The main difference is Service runs in a different process, Say in an application if it gets crashed or the process in which application was running gets killed then Async Task will also be killed but not Service because service was running in a differnt process. – Jitender Dev Sep 26 '13 at 11:57
  • And how about Looper. If our app is crashed, will looper also get crashed.. ? – Neha Gupta Sep 26 '13 at 13:10
  • 1
    Looper run in the same thread as of app unkike Service , If the process in which app was running gets killed then looper will also be killed but service will remain running as service runs in a different process. – Jitender Dev Sep 26 '13 at 13:14
  • I am absolutely clear about Services and thank you so much for that. But I still feel that how to decide whether to use looper or asynctask class.. ? do you have any website or blog.. ? Or any example showing working of a looper.. ? – Neha Gupta Sep 26 '13 at 13:31
  • checkout these links http://androidshortnotes.blogspot.in/2013/02/thread-concept-in-android.html – Jitender Dev Sep 26 '13 at 13:33
  • Also Android Recommends to use Async task rather than Loopers. accept my answer if i have helped you clearing your doubt. – Jitender Dev Sep 26 '13 at 13:34
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/38106/discussion-between-neha-gupta-and-brontok) – Neha Gupta Sep 26 '13 at 13:35
  • Yes you have cleared much and the remaining will be cleared soon. – Neha Gupta Sep 26 '13 at 13:41