The use of Android development handler (source sharing)

Source: Internet
Author: User

Handler primarily accepts data sent by a child thread and updates the UIwith this data in conjunction with the main thread.

When the application starts. Android First opens a main thread (that is, the UI thread), and the main thread is the UI control in the admin interface for event distribution.

For example, if you click on a button, Android will distribute the event to the button to respond to your action. If you need a time-consuming operation at this time, such as: network read data or read a large local file, you can not put these operations in the main thread, if you put in the main thread, the interface will appear suspended animation phenomenon, assuming that 5 seconds is not finished,.  will receive an error "forced shutdown" on the Android system. This time we need to put these time-consuming operations in a sub-thread, because the child thread involves UI updates, the Android main thread is not safe , that is to say. The update UI can only be updated in the main thread, and the operations in the child threads are critical. At this time, Handler appeared. To solve this complex problem, because handler executes in the mainline approached (UI thread), it and the child thread can pass the data through the message object, this time. Handler is responsible for receiving the child thread (the child thread uses the Sedmessage () method) Message object, (including data), put these messages into the main thread queue, with the main thread to update the UI.

Let's first 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 ;p rivate 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 Mythrea D ()). Start ();}}); public static class MyHandler extends Handler {@Overridepublic void Handlemessage (Message msg) {//TODO auto-generated Me Thod 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 Stubwhi Le (Count <=) {try {thread.sleep ($);} catch (Exception e) {//Todo:handle exceptione.printstacktrace ();} Get the message from the message pool, assuming there is no message, create a message, and leave it there. The message is taken to carry the data. Sent by handler message 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 PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;}}


The use of Android development handler (source sharing)

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.