Android provides a handler Implementation Scheme for asynchronous message processing. Handler has many suitable applications and nuances, so that it achieves good results when used together with thread and service. I. Differences between handler and thread. The handler is in the same thread as the caller. If the handler performs time-consuming operations, the caller thread will be blocked. Android UI operations are not thread-safe and must be executed in the UI thread. Android provides several basic solutions for processing UI operations in other threads, including runonuithread (runnable) of the activity ), both the post of view and the asynctask tool of version 1.5 adopt handler, and the post of handler does not actually start a new thread, instead, it directly calls the thread's run method, which is exactly what Google is trying to do with a handler. Public VoidOncreate (bundle savedinstancestate ){ Super. Oncreate (savedinstancestate );Startthread (); // Sendmessage (); Systemclock.Sleep(2000 ); Setcontentview (R. layout.Main); } Public VoidStartthread (){ Mthread. Start (); } Public VoidSendmessage (){ Mhandler =NewMhandler (); Message MSG = mhandler. obtainmessage (); MSG. sendtotarget (); } ClassMhandlerExtendsHandler { PublicMhandler (){ } PublicMhandler (low.l ){ Super(L ); } @ Override Public VoidHandlemessage (Message MSG ){ IntCount = 0; While(Count ++Max_value){ Log.D(Tag, "Rintail "); } } } Thread mthread =NewThread (){ @ Override Public VoidRun (){ IntCount = 0; While(Count ++Max_value){ Log.D(Tag, "Rintail "); } } }; 2. handler does not process messages concurrently. A logoff reads the next message only after processing a message. Therefore, the message is blocked. However, different logoff methods can achieve the goal of concurrency. In service, onstart execution is also blocked. If a startservice is used again before onstart is completed, it will also be blocked. If you want to execute onstart as soon as possible, you can use handler in onstart, because the message sending is non-blocking. If the processing of different messages is concurrent, You Can instantiate handler with different logoff values. Public VoidOnstart (intent,IntStartid ){ Super. Onstart (intent, startid ); Sendmessagewithlooper (); // Sendmessage (); } Public VoidSendmessage (){ Mhandler =NewMhandler (); Message MSG = mhandler. obtainmessage (); MSG. sendtotarget (); } Public VoidSendmessagewithlooper (){ Handlerthread ht =NewHandlerthread ("rintail "); Ht. Start (); Mhandler =NewMhandler (HT. getloler ()); Message MSG = mhandler. obtainmessage (); MSG. sendtotarget (); } ClassMhandlerExtendsHandler { PublicMhandler (){ } PublicMhandler (low.l ){ Super(L ); } @ Override Public VoidHandlemessage (Message MSG ){ Log.D(Tag, "First "); While(True){ If(False) Break; } Log.D(Tag, "Second "); } }; ClassMythreadExtendsThread { @ Override Public VoidRun (){ Log.D(Tag, "First "); While(True){ If(False) Break; } Log.D(Tag, "Second "); } }; 3. Resource Recycling Sending an empty message similar to new message () to a handler object can clear the message. This method is the same as getlooper (). Quit. If a large amount of resources are used, clean up them in a timely manner. |