Handler+looper Change the UI of the activity
Blog for a period of time, has been trying to write some of their own learning experience and understanding, waiting for a long time no longer need to use the time to come directly to get started. Think about it, used to handler before,
Read a few articles feel not very detailed. Talk about handler today.
Android uses handler to change the UI for two purposes: first, from the user experience, the cumbersome operation and computation process to other threads, is the main thread (user interface) more
Added fluency, reducing the occurrence of the phenomenon of Dayton.
Second, to ensure thread safety, only the main thread can change the UI;
With handler, you need to create a Looper object (when the system creates the main thread (activity), a Looper object is encapsulated inside the activity, that is, you cannot see the new
Looper object), the system creates an a message team (Messagequene) for each looper, sending messages to the SendMessage by handler Looper method message
Queue, and then in that thread, handler takes a message from the queue of messages, message.what to determine what action was received by the message (changing the UI of that thread, and so on).
If you want to send a message to the UI thread to change the UI after it has been manipulated in another thread, you must make sure that the message sent by your handler must be sent to the message queue (Messagequene) of that UI thread.
The simplest way to do this is to use the UI thread's handler to send a message (that is, SendMessage), and here are two ways to make the thread of the operation get the handler of the UI thread:
One, in the operating thread: Handler Handler=mainactivity.handler; If you want to change the UI, you're in mainactivity.
Define a handler property and, if static, it is best to put the instantiation of the handler in the Oncreact method
(Of course, handler instantiation can be placed anywhere within the class), overriding Handlemessage (Message msg) in handler instantiation object
method to receive messages and manipulate the UI within the Handlemessage method.
Second, define a class with Handler attribute, add get and set method to get and instantiate handler in class;
Let's look at the code below:
UI thread
Public classMainactivityextendsDroidgap {Public Static Handler Handler; @Override Public voidonCreate (Bundle savedinstancestate) {//Being sure to the super class.Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); handler = new Handler () {@Overridepublic void Handlemessage (Message msg) {super.handlemessage (msg); if (msg.what = = 100) {//This Write code Change UI} else if (Msg.what = = 101) {//This inside write code changes UI} else if (Msg.what = = 102) {}}
Operation class:
Public BooleanAdd (Jsonarray json) {Try{Jsonarray Jsonarray=json.getjsonarray (0); Jsonobject Jsonobject= (Jsonobject) (Jsonarray.get (0));FinalPrintitem printitem=NewPrintitem (jsonobject.getstring ("Product"), Jsonobject.getint ("Count"), Jsonobject.getint ("Price")) ); List.add (Printitem); handler = Mainactivity.handler; Message Message =new message (); message.what=100;message.obj=printitem;handler.sendmessage (message);return true;} Catch(Exception e) {e.printstacktrace ();}return false;}
Essay: Android handler transfer value change UI