Handler Introduction
Android Specifies that only the UI thread is allowed to modify the UI components in a ctivity . When the program starts for the first time, it launches a main thread, often referred to as the UI thread. If the newly-started thread wants to change the value of the attribute in the interface component, use the Handler message-passing mechanism.
Program Examples:
The layout file contains only one Imageview component.
1 Public classMainactivityextendsappcompatactivity {2 3 4 5 6 7 int[] Imageids =New int[]{8 9 R.drawable.java,Ten One r.drawable.ee, A - R.drawable.ajax, - the R.drawable.xml, - - R.drawable.classic - + }; - + intCurrentimageid = 0; A at - - @Override - - protected voidonCreate (Bundle savedinstancestate) { - in Super. OnCreate (savedinstancestate); - to Setcontentview (r.layout.activity_main); + - the * FinalImageView show =(ImageView) Findviewbyid (r.id.show); $ Panax Notoginseng final Handler MyHandler = new Handler () {@Override40//import Android.os.handler;4 2 public void Handlemessage (Message msg) {msg.what = = 0x1233) {46 47 Show.setimageresource (imageids[currentimageid++% imageids.length]); 48 49}50 51}52 5 3}; Wu - About $ NewTimer (). Schedule (NewTimerTask () { - - @Override - A Public voidrun () { + theMyhandler.sendemptymessage (0x1233); - $ } the the}, 0, 1200); the the } - in}
The essence of the TimerTask object is to start a new thread and therefore cannot make changes to the UI component.
The program passes a timer method timed to pass an empty message (0x1233) to MyHandler, and then the main thread handlemessage to handle the elimination ( 0x1233)
Handler Working principle
- message: handler The message objects that are accepted and processed.
- looper: Each thread has only one looper< Span lang= "ZH-CN", which is responsible for managing messagequeuemessagequeue< Span lang= "ZH-CN" to remove the message and divide the message into the corresponding handler processing.
- messagequeue: looper responsible for management. It uses FIFO to manage message
- handler: He could send the message to looper managed messagequeuelooper
steps to use Handler in a thread:
- Calling Looper 's preper () method creates a Looper object for the current thread , and when the Looper object is created, its constructor creates a companion MessageQueue .
- With Looper , create an instance of the Handler subclass, overriding the Handlrmessage () method, which is responsible for processing messages from other threads.
- Call Looper 's Loop () method to start Looper.
Instance:
The layout file contains a key and a text box
where the key contains the property OnClick = "cal" Cal is the method declared in the activity.
1 2 3 Public classMainactivityextendsappcompatactivity {4 5 6 7 Static FinalString upper_num = "UPPER";8 9 EditText Etnum;Ten One Calthread Calthread; A - - the @Override - - protected voidonCreate (Bundle savedinstancestate) { - + Super. OnCreate (savedinstancestate); - + Setcontentview (r.layout.activity_main); A at - -Etnum =(EditText) Findviewbyid (r.id.etnum); - -Calthread =NewCalthread (); - in Calthread.start (); - to } + - the * Public voidcal (View source) { $ Panax NotoginsengMessage msg =NewMessage (); - theMsg.what = 0x1233; + ABundle bundle =NewBundle (); the + Bundle.putint (Upper_num, Integer.parseint (Etnum.gettext (). toString ())); - $ Msg.setdata (bundle); $ - calThread.mHandler.sendMessage (msg); - the } - Wuyi the - Private classCalthreadextendsThread { Wu - About $ PublicHandler Mhandler; - - @Override - A Public voidrun () { + the Looper.prepare (); - $ Mhandler = new Handler (){ the the @Override the the Public void handlemessage (Message msg) { - in if(Msg.what = = 0x1233){ the the intUpper =msg.getdata (). GetInt (upper_num); About theList<integer> nums =NewArraylist<integer>(); the the outer: + - for(inti = 2; I <= Upper; i++){ the Bayi for(intj = 2; J <= Math.sqrt (i); J + +){ the the if(I! = 2 && i% j = 0){ - - Continueouter; the the } the the } - the Nums.add (i); the the }94 theToast.maketext (mainactivity. This, Nums.tostring (), the the Toast.length_long). Show ();98 About } - 101 }102 103 };104 the Looper.loop ();106 107 }108 109 } the 111}
Handler message delivery mechanism