Android sub-thread UI Update Summary

Source: Internet
Author: User

Android sub-thread UI Update Summary

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: to change the display content of TextView in a child thread ** android multi-thread programming 1: update the UI component handler in a newly started thread through Handler. post (new Runnable (); 2: handler. postdelayed (new Runnable, delay); 3: Activity. runOnUiThread (); 4: AsyncTask 5: thread pool ** @ author Administrator **/public class MainActivity extends Activity {TextView TV; private Handler mHandler; private String TAG = MainActivity. class. getSimpleName (); int I = 0; @ Overrideprotected void onCreate (Bundle savedInstanceState) {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(cmd.exe cute ();}/*** 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 of 3 seconds */private void initHandler3 () {// TODO Auto-generated method stubnew Thread (new Runnable () {@ Overridepublic void run () {// TODO Auto-generated method stubmHandler. postDelayed (new Runnable () {@ Overridepublic void run () {// TODO Auto-generated method stubtv. setText (TAG) ;}}, 3000 );}}). start ();}/*** method in the Child Thread */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. println (Thread. currentThread (). getName (); TV. setText (TAG );}});}}). start ();}/*** Mothod 1; this method operates the UI in the injection 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
 
  
{@ Overrideprotected String doInBackground (String... params) {// TODO Auto-generated method stubtry {Thread. sleep (3000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke. printStackTrace ();} return null;} @ Overrideprotected void onPostExecute (String result) {// TODO Auto-generated method stubsuper. onPostExecute (result); TV. setText (TAG );}}}
 

Related Article

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.