Android UI Programming (6)--handlerthread

Source: Internet
Author: User
Tags message queue

Introduction :

Handlerthread inherits thread, when the thread is turned on, that is, when it runs, the thread creates a looper that contains the message queue and provides its own get method for this Looper object. This is the only difference between it and the normal thread.

Benefits :

Why to use Handlerthread

1, if the development of multiple uses like new Thread () {}.start (); In this way, a child thread is created, creating multiple anonymous threads, which makes the program run more and more slowly, while the handlerthread comes with looper so that he can reuse the current thread multiple times with messages, saving money

2, the Android system provides the handler class internal looper default binding is the UI thread message queue, for non-UI thread and want to make the message mechanism, then handlerthread internal Looper is the most appropriate, it does not interfere with or block the UI thread

usage :

Handlerthread since the essence is thread, why the previous add a handler? Android in the handler class is essentially from its internal looper in the message, and then trigger its internal callback interface Handlemessage method, let the user to achieve the specific processing of the message. and Handlerthread itself with Looper, as long as it implements the callback interface, then Handlerthread can also be in their own thread to process the message sent out, fully implement the non-UI thread in the lower expenditure of the message processing.

Summarize:

1, handlerthread inherit thread, and thread is different is to provide their own Looper object, but need to start this handlerthread thread

2. Call Handlerthread.getlooper () to get the Looper object, do not call the Looper.prepare () and Looper.loop () methods

to better understand Looper and Handlerthread, first look at Android UI Programming (5)--looper

androidmanifest.xml--did not make any changes to create the project default generated

<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "/http Schemas.android.com/apk/res/android "package=" Com.wxl.handlerthread "android:versioncode=" 1 "android:versionname=        "1.0" > <uses-sdk android:minsdkversion= "8" android:targetsdkversion= "/> <application"        Android:allowbackup= "true" android:icon= "@drawable/ic_launcher" android:label= "@string/app_name"            Android:theme= "@style/apptheme" > <activity android:name= "com.wxl.handlerthread.MainActivity" Android:label= "@string/app_name" > <intent-filter> <action android:name= "a            Ndroid.intent.action.MAIN "/> <category android:name=" Android.intent.category.LAUNCHER "/> </intent-filter> </activity> </application></manifest> 
Activity_main.xml

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "    android:gravity= "center"    android:orientation= "vertical"    tools:context= ". Mainactivity ">    <textview        android:id=" @+id/textview "        android:layout_width=" Wrap_content "        android:layout_height= "wrap_content"/>        <button         android:id= "@+id/button"        android: Layout_width= "Wrap_content"        android:layout_height= "wrap_content"        android:text= "send Hello World"/> </LinearLayout>
Mainactivity.java

Package Com.wxl.handlerthread;import Android.os.bundle;import Android.os.handler;import android.os.HandlerThread; Import Android.os.looper;import android.os.message;import Android.util.log;import Android.view.view;import Android.widget.button;import Android.widget.textview;import Android.app.activity;public class MainActivity extends Activity {private TextView textview;private Button button;private Handler handler;private MyThread mythread;private    Handlerthread Handlerthread;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        TextView = (TextView) This.findviewbyid (R.id.textview);        Button = (button) This.findviewbyid (R.id.button); Button.setonclicklistener (New View.onclicklistener () {@Overridepublic void OnClick (View arg0) {//TODO auto-generated Method Stubmessage message = Message.obtain (); message.what = 1;message.obj = "Hello World"; handler.sendmessage (Message);}});        MyThread = new MyThread ();        Handlerthread = new Handlerthread ("");        Mythread.start ();    Handlerthread.start (); } public class MyThread extends Thread {@Overridepublic void Run () {//TODO auto-generated method Stubsuper.run (); L     Ooper.prepare (); handler = new Handler (Handlerthread.getlooper ()) {@Override public void Handlemessage (Message msg) {    TODO auto-generated Method Stub super.handlemessage (msg);    if (1 = = Msg.what) {//textview.settext ("" +msg.obj); child threads cannot update UI log.i ("", "" +msg.obj);    }    }    };    Looper.loop ();} }}
Click the button





Android UI Programming (6)--handlerthread

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.