When the application starts, it opens a main thread (that is, the UI thread), which manages the UI, listens for user clicks, responds to users and distributes events, and so on. All generally do not perform more time-consuming operations in the main thread, such as delay, download network data, Dead loops, or a ANR error occurs. Therefore, these operations are placed in child threads, but because the Android UI thread is unsafe, all of the UI can only be updated in the main thread. Use thread to create child threads, use message to store data, and use handler to process message data.
Summary :
1, the child thread and the UI main thread pass the data through the message, need to create a new class (MyHandler) and inherit handler, and then overload the Handlemessage (Message msg) method to handle different messages
2. A child thread sends a message to the message queue using the SendMessage (message) of the instantiated object of the new Class (MyHandler) created above
3. Create a message instantiation object in the child thread, use Message.obtain (), create an empty message (the API recommends using Message.obtain () instead of the new Message ()).
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.handler_message "android:versioncode=" 1 "Android:versionnam E= "1.0" > <uses-sdk android:minsdkversion= "8" android:targetsdkversion= "/> <applicatio" n android:allowbackup= "true" android:icon= "@drawable/ic_launcher" android:label= "@string/app_name" Android:theme= "@style/apptheme" > <activity android:name= "com.wxl.handler_message. Mainactivity "android:label=" @string/app_name "> <intent-filter> <action Android:name= "Android.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" tools:context= ". Mainactivity "> <textview android:id=" @+id/textview " android:layout_width=" Wrap_content " android:layout_height= "wrap_content" android:text= "@string/hello_world"/></linearlayout>
Mainactivity.java
Package Com.wxl.handler_message;import Android.os.bundle;import Android.os.handler;import android.os.Message; Import Android.util.log;import Android.widget.textview;import Android.app.activity;public class MainActivity extends Activity {private TextView textview;private myhandler handler;private MyThread thread; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); TextView = (TextView) Findviewbyid (R.id.textview); Handler = new MyHandler (); thread = new MyThread (); Thread.Start (); } public class MyThread extends thread{private int count = 0; @Override public void Run () {//TODO auto-generated Method Stub Super.run (); while (true) {try {thread.sleep (1000); Message message = Message.obtain (); Message.what = 1; if (= = count) {count = 0; } message.obj = ++count; Handler.sendmessage (message); ≪/span>} catch (Interruptedexception e) {//TODO auto-generated catch block E.printstacktrace (); }}}} public class MyHandler extends Handler {@Override public void Handlemessage (Message msg) {//TODO auto-generated Method Stub super.handlemessage (msg); Switch (msg.what) {case 1:{textview.settext ("" +msg.obj);} Break;default:break;} } } }
Android UI Programming (4)--thread, Message, Handler