Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between Thread.run() and Handler.post() and Service in Android?

It's generally suggested to use Handler.post() in Android when need to do some jobs in different threads.
And when I want to do some jobs in the background, I was suggested to start a Service.

But I feel more convenient using new Thread (new Runnable(){...} ); as I used to.

But I am afraid that creating new threads by hand may behave differently in Android, such as maybe automatically stop when memory is low while using Service might not ?

Wishing a clear answer to help me out of this confusion. ^ ^

like image 523
Aloong Avatar asked Dec 28 '22 08:12

Aloong


2 Answers

When performing certain jobs in android it is greatly suggested to use Handler because :

In Android one can only update views in its original thread, i.e., the thread in which they were created, otherwise the app may throw an exception saying

android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

Handlers in Android bind with the thread in which they are created. Each Handler instance is associated with a single thread and that thread's message queue. When you create a new Handler, it is bound to the thread / message queue of the thread that is creating it -- from that point on, it will deliver messages and runnables to that message queue and execute them as they come out of the message queue. So Handlers are the safest to go around in Android.

While Services, Heres a piece of code from http://developer.android.com/reference/android/app/Service.html

What is a Service?

Most confusion about the Service class actually revolves around what it is not:

A Service is not a separate process. The Service object itself does not imply it is running in its own process; unless otherwise specified, it runs in the same process as the application it is part of.

A Service is not a thread. It is not a means itself to do work off of the main thread (to avoid Application Not Responding errors). Thus a Service itself is actually very simple, providing two main features:

A facility for the application to tell the system about something it wants to be doing in the background (even when the user is not directly interacting with the application). This corresponds to calls to Context.startService(), which ask the system to schedule work for the service, to be run until the service or someone else explicitly stop it.

A facility for an application to expose some of its functionality to other applications. This corresponds to calls to Context.bindService(), which allows a long-standing connection to be made to the service in order to interact with it.

And lastly Threads,

threads are used to perform some heavy non-view functions, some heavy computation works like parsing, etc so that it does not block you UI and safely performs all the work ...

like image 190
King RV Avatar answered Jan 02 '23 23:01

King RV


Calling thread from UI violates the single thread model: the Android UI toolkit is not thread-safe and must always be manipulated on the UI thread. thats why the followings arethe alternative instead ofusing thread directly

  • Activity.runOnUiThread(Runnable)
  • View.post(Runnable)
  • View.postDelayed(Runnable, long)
  • Handler

Handler is used to communicate between UI thread and background thread AsyncTask is used to do some little heavy task in background

If you have lightest work in background then use handler for more heavy background work use AsyncTask and to do heaviest work in background use service

See http://android-developers.blogspot.com/2009/05/painless-threading.html

Example

  • To add or remove data from list view you can use by handler
  • To get XML data from server and reflect those in your view use AsynTask. AsyncTask is more preferred than threading
  • To play music file use service
like image 40
Sunil Kumar Sahoo Avatar answered Jan 02 '23 23:01

Sunil Kumar Sahoo