How to Use Handler in Android Development (source code sharing)

Source: Internet
Author: User

Handler mainly accepts the data sent by the sub-thread and uses this data to update the UI with the main thread ..

When an application is started, Android starts a main thread (that is, the UI thread) and the main thread is the UI control in the management interface for event distribution. For example, if you click a Button, Android will distribute the event to the Button to respond to your operation. If you need a time-consuming operation, such as reading data online or reading a large local file, you cannot place these operations in the main thread ,, if you put it in the main thread, the interface will be suspended. If it has not been completed in five seconds, you will receive an error message "Force disable" from the Android system ". at this time, we need to put these time-consuming operations in a sub-thread. Because the sub-thread involves UI update, the main Android thread is thread insecure, that is, updating the UI can only be updated in the main thread. Operations in the Child thread are dangerous. at this time, Handler appears ., because Handler runs in the main thread (in the UI thread), it and the sub-thread can transmit data through the Message object. At this time, handler is responsible for receiving the Message object (the Sub-thread uses the sedMessage () method to send messages) sent by the sub-thread, (which contains data), and putting these messages into the main thread queue, updates the UI with the main thread.

Let's take a look at the simple usage of handler.

Package com. example. android_handler_textview; import android. OS. bundle; import android. OS. handler; import android. OS. message; import android. app. activity; import android. view. menu; import android. view. view; import android. widget. button; import android. widget. textView; public class MainActivity extends Activity {private Button button; private static TextView textView; private MyHandler handler; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); button = (Button) this. findViewById (R. id. button1); textView = (TextView) this. findViewById (R. id. msg); handler = new MyHandler (); button. setOnClickListener (new View. onClickListener () {@ Overridepublic void onClick (View v) {// TODO Auto-generated method stubnew Thread (new MyThread ()). start () ;}});} public static class MyHandler extends Handler {@ Overridepublic void handleMessage (Message msg) {// TODO Auto-generated method stubsuper. handleMessage (msg); int arg1 = msg. arg1; String name = (String) msg. obj; textView. append (name + arg1) ;}} public class MyThread implements Runnable {int count = 0; @ Overridepublic void run () {// TODO Auto-generated method stubwhile (count <= 20) {try {Thread. sleep (500);} catch (Exception e) {// TODO: handle finished tione. printStackTrace ();} // obtain a Message from the message pool. If no Message exists, create a Message. If yes, extract the message carrying data and the handler sends the Message = message. obtain (); message. arg1 = count; message. obj = "jack"; count ++; handler. sendMessage (message) ;}}@ Overridepublic boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.