Summary:
Threads are often used in Android development, and many students immediately use the new thread () {...} When thinking about threads. Start () in this way. This way, if the above code is called multiple times in an activity, multiple anonymous threads are created, and the longer the program runs, the slower it may become. Therefore, a handler is required to start a thread, and a thread is removed to ensure that the thread is not created repeatedly.
Body:
1, the general way to create handler
Typically use handler handler = new handler () {...} Create.
This creates a handler that is handler under the main thread, the UI thread, which is bound to the default looper under the UI thread.
The looper is used to implement Message Queuing and message loop mechanisms.
Therefore, if the handler is created by default then it is not possible to create a handler if the thread is doing some time-consuming operations such as network fetch data.
2. Using Handlerthread
Handlerthread is actually a thread, but it's a looper more than a normal thread.
We can create handler using the following example:
New Handlerthread ("Myhandlerthread"New
The start () method is called when the Handlerthread is created.
The Looper object in Handlerthread is then passed in when the handler is created.
Then the Mhandler object is bound to the Handlerthread thread (which is no longer tied to the UI thread, so it does not block the UI when it handles the time-consuming operation).
Finally, the thread that implements the time-consuming operation is post to the Mhandler message queue.
Note that the mbackgroundrunnable thread does not start because there is no call to the start () method.
Full Demo:
Public classMainactivityextendsActivityImplementsonclicklistener{ Public Static FinalString TAG = "Mainactivity"; PrivateHandler Mhandler; Private BooleanMrunning =false; PrivateButton MBtn; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Handlerthread Thread=NewHandlerthread ("Myhandlerthread"); Thread.Start ();//Create a handlerthread and start itMhandler =NewHandler (Thread.getlooper ());//create a handler using the Handlerthread Looper object, which is likely to block the UI thread if you use the default construction methodMhandler.post (mbackgroundrunnable);//post a thread to handlerMBtn=(Button) Findviewbyid (R.id.button); Mbtn.setonclicklistener ( This); } @Overrideprotected voidOnresume () {Super. Onresume (); Mrunning=true; } @Overrideprotected voidOnStop () {Super. OnStop (); Mrunning=false; } @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.main, menu); return true; } //threads that implement time-consuming operationsRunnable mbackgroundrunnable =NewRunnable () {@Override Public voidrun () {//----------simulate time-consuming operations and start--------------- while(mrunning) {log.i (TAG,"Thread running!"); Try{Thread.Sleep (200); } Catch(interruptedexception e) {e.printstacktrace (); } } //----------simulate time-consuming operations and end--------------- } }; @Overrideprotected voidOnDestroy () {Super. OnDestroy (); //Destroying Threadsmhandler.removecallbacks (mbackgroundrunnable); } @Override Public voidOnClick (View v) {toast.maketext (Getapplication (),"Click the button!!!", Toast.length_short). Show (); } }
If you do not use Handlerthread in the OnCreate () method to create handler directly using the default constructor of handler, then Mbackgroundrunnable will block the UI thread.
3. Thread Destruction
Use the above method to create a thread that can be used when destroying
Destroy a thread so that you can avoid creating multiple threads that are running concurrently when you enter the same activity multiple times.
Original:
Creation and destruction of Android threads [2014-07-11] (2013-06-19). http://www.bdqn.cn/news/201306/9538.shtml