Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>Why to use Handler in Android?</h2> <hr> <p><strong>First: Let us know what is thread:</strong></p> <ul> <li>Threads help in multi-tasking</li> <li>Threads can be taught as a mini process running under a main process</li> <li>Threads enable at-least the appearance parallel execution</li> </ul> <p><strong>Second: Let us know about the application thread:-</strong></p> <ul> <li>When the android application is first started the runtime system will create a single main thread, this main thread will take care of execution of all the components in android</li> </ul> <hr> <p><strong>Android UI-Toolkit is not thread safe</strong></p> <ul> <li><em>As stated there are many components in the android main thread, now suppose one of the components takes a long time for execution then this makes the main thread <strong>unresponsive</strong> and it will <strong>show the application unresponsive</em></strong></li> <li>Sub-threads cannot directly manipulate the application (main) thread in the android </li> <li>Handler acts as a interface and collects the messages from the sub-threads and update the main application thread one by one as the messages arrive, Thread handlers are implemented in main thread.</li> </ul> <hr> <p><strong>Handler class:</strong></p> <ul> <li>For the purpose of multithreading we shall use handler class which comes from the package <code>android.os.Handler</code></li> <li>Each thread is handled by one instance of handler class</li> </ul> <p><img src="https://i.stack.imgur.com/7ZPwd.png" alt="Figure"></p> <ul> <li>From the above figure we can see that Each thread is handled by one instance of the Handler class</li> <li>threads communicate between each other with the help of messages</li> <li>This handler class helps to maintain kind of sync-coordination b/w threads by allowing them to run together achieving multithreading</li> </ul> <hr> <p><strong>Instance of handler is made</strong> </p> <pre><code>Handler handlerObject = new Handler(); </code></pre> <hr> <p><strong>Final piece on using handler is to use Runnable Interface:</strong></p> <ul> <li>handler class utilizes runnable interface to implement multithreading</li> <li>We override run method to execute a thread a specified number of time</li> </ul> <hr> <pre><code>Class NameOfClass implements Runnable { Public void run() { //Body of run method } } </code></pre> <hr> <p><strong>Putting all together</strong></p> <pre><code>//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(); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload