I came across this code in a very basic Handler tutorial. The code is working fine but I do not understand why I have to use Handler for progressDialog.dismiss()
From the documentation of View:
You must always be on the UI thread when calling any method on any view. If you are doing work on other threads and want to update the state of a view from that thread, you should use a
Handler
.
In your example, when you've to call the dismiss()
method on the ProgressDialog
, as per the above documentation, you must do so from the UI thread. The messageHandler
is initialized to an instance of a Handler
when the HandlerThread
class is instantiated (presumably on the UI thread).
From the documentation of Handler:
Each
Handler
instance is associated with a single thread and that thread's message queue. When you create a newHandler
, 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 to communicate with the UI thread from your new thread, just post a message to the Handler
created on the UI thread.
If you call methods on a View
from outside the UI thread, it invokes undefined behaviour, which means, it may appear to work fine. But it's not always guaranteed to work fine.
The easier the better. Instead of using a handler, you could try to use the following code:
runOnUiThread(
new Runnable() {
public void run()
{
//Update user interface here
}
}
);
Don't make your life to complicated ;)
when an application starts, android system launch a process having a main thread, which is responsible to process UI Rendering, and Events. The Android UI isnot thread safe, so we can access android UI only by Event thread. In your program you have defined another thread than event by following code block:
new Thread() {
public void run() {
try {
Thread.sleep(8000);
} catch (InterruptedException e) {
}
messageHandler.sendEmptyMessage(0);
}
}.start();
now if you want to dismiss progress dialog, you can do it only in event thread. Handler is used process/handle messages of a message queue. Handler associates with a thread, in your case its in event thread, as by default it would associate thread, in which it is being created. by messageHandler.sendEmptyMessage() another thread send a message to handler, and handler, process this message, in handleMessage method.
First: Let us know what is thread:
Second: Let us know about the application thread:-
Android UI-Toolkit is not thread safe
Handler class:
android.os.Handler
Instance of handler is made
Handler handlerObject = new Handler();
Final piece on using handler is to use Runnable Interface:
Class NameOfClass implements Runnable
{
Public void run()
{
//Body of run method
}
}
Putting all together
//Create handler in the thread it should be associated with
//in this case the UI thread
final Handler handler = new Handler();
Runnable runnable = new Runnable() {
public void run() {
while(running){
//Do time consuming stuff
//The handler schedules the new runnable on the UI thread
handler.post(new Runnable() {
//Ex.. using progressbar to set the pogress
//Updating the UI is done inside the Handler
});
}
}
};
new Thread(runnable).start();