Android sub-thread Update UI Interface Summary

Source: Internet
Author: User

Package Com.jrhcode.morethreadtest;import Java.util.concurrent.executors;import Android.os.asynctask;import Android.os.bundle;import Android.os.handler;import Android.app.activity;import Android.view.Menu;import Android.widget.textview;import android.widget.toast;/** * * Main function is: in the child thread to change the display of TextView * * Android multithreaded programming   1: Through Han   Dler Update UI Component Handler.post (new Runnable ()) in the newly-started thread;    2:handler.postdelayed (new Runnable, delay);  3:activity.runonuithread (); 4:asynctask 5: Thread pool * * @author Administrator * */public class Mainactivity extends Activity {TextView tv;private Handl Er mhandler;private String TAG = MainActivity.class.getSimpleName (); int i = 0; @Overrideprotected void onCreate (Bundle sav Edinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); TV = (TextView) Findviewbyid (r.id.tv); mhandler = new Handler ();//Inithandler ();//InitHandler2 ();//InitHandler3 ();//UpdateTV ();// UPDATETVV (); Threedpool ();} private void Threedpool () {//TODO AuTo-generated method Stubexecutors.newsinglethreadexecutor (). Submit (New Runnable () {@Overridepublic void Run () {//TODO Auto-generated Method StubSystem.out.println (Thread.CurrentThread () getName ()); Tv.settext (TAG); Mhandler.post (new Runnable () {@Overridepublic void Run () {//TODO auto-generated method Stubtv.settext ("This is Handler post"),}});});} /** * */private void Updatetvv () {//TODO auto-generated method Stubnew MyTask (). Execute (); /** * Method 3 */private void UpdateTV () {//TODO auto-generated method stubnew Thread (new Runnable () {@Overridepublic void run  () {//Todo auto-generated method StubMainActivity.this.runOnUiThread (new Runnable () {@Overridepublic void Run () {//TODO Auto-generated method Stubtv.settext (TAG);}}). Start ();} /** * Delay 3 sec */private void InitHandler3 () {//TODO auto-generated method stubnew Thread (new Runnable () {@Overridepublic voi D run () {//Todo auto-generated method stubmhandler.postdelayed (new Runnable () {@Overridepublic void Run () {//Todo auto-g enerated mEthod Stubtv.settext (TAG);}}, 3000);}). Start ();} /** * in child thread method */private void InitHandler2 () {//TODO auto-generated method stubnew Thread (new Runnable () {@Overridepublic void Run () {//TODO auto-generated method Stubmhandler.post (new Runnable () {@Overridepublic void run () {System.out.printl N (Thread.CurrentThread (). GetName ()); Tv.settext (TAG);}}). Start ();} /** * Mothod 1; The method is to manipulate the UI in the note thread; */private void Inithandler () {//TODO auto-generated method Stubmhandler.post (new Runnable () {@Overridepublic void run () { Toast.maketext (Mainactivity.this,thread.currentthread (). GetName (), 0). Show (); Tv.settext (TAG);});} Class MyTask extends Asynctask<string, Void, string> {@Overrideprotected string Doinbackground (String ... params) { Todo auto-generated method stubtry {thread.sleep;} catch (Interruptedexception e) {//TODO auto-generated catch B Locke.printstacktrace ();} return null;} @Overrideprotected void OnPostExecute (String result) {//TODO auto-generated method Stubsuper.onpostexecute (result); Tv.settext (TAG);}}} 

Android sub-thread Update UI Interface Summary

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.