Android Sub-thread Update main interface

Source: Internet
Author: User
<span id="Label3"></p><p><p>What to learn or to have a real application, there is a need to be in the shortest possible time to firmly grasp a technology.</p></p><p><p>Today is the case, the product of a demand down, ah. Then only slightly touched before, only to bite the Bullet. Finally firmly mastered the simplest handler technology, gossip not much to say!</p></p><p><p>Everyone who does Android knows that Android memory is stored in M deposit! Then we naturally know, and Google in the design of the full consideration of this point, so, our uithread do not usually do time-consuming work, such as requesting the network, such as the implementation of some more time-consuming logic, such as loading pictures, such as the load of images, such as, oh, for the moment only encountered These. Then we Google to provide us with a lot of ways to solve, like another asynchronous thread to request the network ah, like using uithread inside call Sub-thread to update, like the use of Runonuithread method, and so On. And then the other tall features are still temporarily not mastered, today mastered the handler this a relatively civilian small function.</p></p><p><p>As is known to all, handler can update the UI interface with a child thread through the main thread, and then it will not consume too much memory in the System. If you have to understand its internal structure, you will find a lot of online, involving looper, messagequenn,message, before learning, you want to let me give you to say a why I will recite the relevant things back to you listen!!! Work on the settle down of the master function bar!! As for the structure of what to use the time to get a good, and the back of the interview is brought to the!!</p></p><p><p>On the code!!</p></p><p><p>first, we create handler objects in Activity:</p></p><pre><pre><span style="color: #0000ff;">Private</span> <span style="color: #0000ff;">New</span> Handler () {};</pre></pre><p><p>Then create a new thread:</p></p><pre><pre>Thread mythread=<span style="color: #0000ff;">new</span> thread (<span style="color: #0000ff;">new</span> <span style="color: #000000;"> Runnable () { @override run () { } });</span></pre></pre><p><p>Some settings and changes are written to the Thread:</p></p><pre><pre><span style="color: #0000ff;">New</span> <span style="color: #000000;">productlistrequest ();p aram.setorgid</span>("1234"<span style="color: #000000;">);p aram.setorgname (</span>"shanghai pudong"<span style="color: #000000;">);p aram.setpagesize ( </span><span style="color: #000000;">;p Aram.setpageindex (</span>1<span style="color: #000000;"></span>= syncapi.getproductlist (param);</pre></pre><p><p>Then the next step is to create the message object, get it through handler, and assign it an Identity. And then will get to change the Uithread variable assigned to msg, and then send it to handler;</p></p><pre><pre>Message msg=<span style="color: #000000;">handler.obtainmessage (); msg.what</span>=result; <span style="color: #008000;">//</span> <span style="color: #008000;">result is a global constant defined in activity, with a value of 1<br></span>msg.obj=result;<br><em id="__mceDel">Handler.sendmessage (msg);</em></pre></pre><p><p>And then turn on the Thread. finally, I get a message inside the handler and update the UI inside the Handler.</p></p><pre><pre><span style="color: #0000ff;">if</span> (msg.what==shenqingxiangqing)<br>{<br>Getpreapplydetailresponse result=<span style="color: #000000;"><br> Tvresult.settext (result.getbussinesscode ());}</span></pre></pre><p><p>Then the entire code is as Follows:</p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>Result=1<span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span>Handler Handler =<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Handler () {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">handlemessage (Message Msg) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(msg.what==<span style="color: #000000;"><span style="color: #000000;">RESULT) {getpreapplydetailresponse result</span></span>=<span style="color: #000000;"><span style="color: #000000;">(getpreapplydetailresponse) msg.obj; Tvresult.settext (result.getbussinesscode ()); }thread MyThread</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>Thread (<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Runnable () {@Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">run () {Message msg</span></span>=<span style="color: #000000;"><span style="color: #000000;">Handler1.obtainmessage (); Msg.what</span></span>=<span style="color: #000000;"><span style="color: #000000;">RESULT; Msg.obj</span></span>=<span style="color: #000000;"><span style="color: #000000;">result; Handler1.sendmessage (msg); Huankuanqingqiuthread.start (); }</span></span></pre><p><p></p></p><p><p>Android Sub-thread Update main interface</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.