Handler, Loop, MessageQueue works:
Let's take a look at these several components:
Message:handler received and processed message objects
Looper: Reads the message from the MessageQueue and sends the read message to handler for processing
MessageQueue: Message store queue.
The program uses handler to send messages, the messages sent must be assigned to MessageQueue, which means that handler works properly and must ensure that there is a MessageQueue in the current thread. And because MessageQueue is managed by Looper, there must be a Looper object.
How to create a Looper object:
In the main UI thread, the system has initialized a Looper object, so the program can create the handler object directly
In a custom thread, you must create a Looper object yourself and start it. Create method: Call Loop.prepare ().
The Prepare method guarantees that only one Looper object will be in each thread.
Then call the Looper.loop () method to start. The loop method uses a dead loop to continuously remove messages from the MessageQueue until MessageQueue is empty and sends the extracted messages to handler for processing.
To calculate a prime number with a new thread:
Mainactivity.java:
Import Android.app.activity;import android.os.bundle;import Android.os.handler;import Android.os.Looper;import Android.os.message;import Android.view.menu;import Android.view.menuitem;import Android.view.View;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.edittext;import Android.widget.textview;import Android.widget.toast;import Java.lang.*;import Java.util.ArrayList;import Java.util.list;public class Mainactivity extends Activity implements Onclicklistener {private EditText etnum;static Final String upper_num = "UPPER"; Calthread calthread;private TextView textview;class Calthread extends Thread {public Handler mhandler;public void Run () {L Ooper.prepare (); mhandler = new Handler () {@Overridepublic void Handlemessage (Message msg) {//TODO auto-generated method s Tubsuper.handlemessage (msg); if (msg.what = = 0x123) {int upper = Msg.getdata (). GetInt (Upper_num); list<integer> nums = new arraylist<integer> () outer:for (int i = 2; I <=Upper i++) {for (int j = 2, J <= math.sqrt (i), J + +) {if (I! = 2 && I% J = = 0) {continue outer;}} Nums.add (i);} Toast.maketext (Mainactivity.this, Nums.tostring (), 1). Show ();}}; Looper.loop ();}} @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); etnum = (EditText) Findviewbyid (r.id.etnum); Button button = (button) Findviewbyid (R.id.button), TextView = (TextView) Findviewbyid (r.id.textview); calthread = new Calthread (); Calthread.start (); Button.setonclicklistener (this);} @Overridepublic void OnClick (View v) {//TODO auto-generated method Stubmessage msg = new Message (); msg.what = 0x123; Bundle bundle = new bundle (); Bundle.putint (Upper_num, Integer.parseint (Etnum.gettext (). toString ())); Msg.setdata ( bundle); CalThread.mHandler.sendMessage (msg);}}
Activity_main.xml:
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" fill_parent " android:layout_height=" Fill_parent " android:orientation= "vertical" android:paddingbottom= "@dimen/activity_vertical_margin" android: paddingleft= "@dimen/activity_horizontal_margin" android:paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools:context= " Com.example.primedemo.MainActivity "> <edittext android:id=" @+id/etnum " 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=" OK "/></linearlayout>
Android------how Handler, Loop, and MessageQueue work: