Android Four ways to update the UI asynchronously _android

Source: Internet
Author: User
Tags stub

Everyone knows that because of performance requirements, Android requires that the UI be updated only in the UI thread, and there are roughly 4 ways to update the UI in other threads, using four different ways to update a textview.
1. Use the handler message passing mechanism

Package com.example.runonuithreadtest;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.os.Handler;
Import Android.widget.TextView;
public class Mainactivity extends activity {
 private TextView TV;
 Handler Handler = new Handler ()
 {public
 void Handlemessage (Android.os.Message msg) {
  if (msg.what==0x123)
  {
  Tv.settext ("Updated TextView");
 }}; @Override
 protected void onCreate (Bundle savedinstancestate) {
 super.oncreate (savedinstancestate);
 Setcontentview (r.layout.activity_main);
 TV = (TextView) Findviewbyid (r.id.tv);
 New Mythread (). Start ();
 Class Mythread extends Thread
 {
 @Override public
 void Run () {
  //delay two seconds to update
  try {
  Thread.Sleep ();
  } catch (Interruptedexception e) {
  //TODO auto-generated catch block
  e.printstacktrace ();
  }
  Handler.sendemptymessage (0x123);}}

2. Using the Asynctask asynchronous task (updating the UI can only be done in the OnPostExecute (String result) method)

Package com.example.runonuithreadtest;
Import android.app.Activity;
Import Android.os.AsyncTask;
Import Android.os.Bundle;
Import Android.widget.TextView;
public class Mainactivity extends activity {
 private TextView TV;
 @Override
 protected void onCreate (Bundle savedinstancestate) {
 super.oncreate (savedinstancestate);
 Setcontentview (r.layout.activity_main);
 TV = (TextView) Findviewbyid (r.id.tv);
 New Yibu (). Execute ();
 Class Yibu extends Asynctask<string, string, string>
 {
 @Override
 protected string Doinbackground (String ... params) {
  try {
  thread.sleep;
  } catch (Interruptedexception e) {
  //TODO auto-generated catch block< C22/>e.printstacktrace ();
  }
  return null;
 }
 @Override
 protected void OnPostExecute (String result) {
  //TODO auto-generated the method stub
  Tv.settext (" Updated TextView ");}}
 }

3. Using the Runonuithread (action) method

Package com.example.runonuithreadtest;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.widget.TextView;
public class Mainactivity extends activity {
 private TextView TV;
 @Override
 protected void onCreate (Bundle savedinstancestate) {
 super.oncreate (savedinstancestate);
 Setcontentview (r.layout.activity_main);
 TV = (TextView) Findviewbyid (r.id.tv);
 New Mythread (). Start ();
 Class Mythread extends Thread
 {
 @Override public
 void Run () {
  runonuithread (new Runnable ()
  { @Override public
  Void Run () {
   //TODO auto-generated method stub
   try {
    //delay two seconds update
    Thread.Sleep ( );
   } catch (Interruptedexception e) {
    e.printstacktrace ();
   }
   Tv.settext ("Updated TextView");}}
  }

4. Using the handler post (Runnabel R) method

Package com.example.runonuithreadtest;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.os.Handler;
Import Android.widget.TextView;
public class Mainactivity extends activity {
 private TextView TV;
 @Override
 protected void onCreate (Bundle savedinstancestate) {
 super.oncreate (savedinstancestate);
 Setcontentview (r.layout.activity_main);
 TV = (TextView) Findviewbyid (r.id.tv);
 Handler Handler = new Handler ();
 Handler.post (New Runnable () {
  @Override public
  void Run () {
  try {
   //delay two seconds update
   Thread.Sleep ( );
  } catch (Interruptedexception e) {
   e.printstacktrace ();
  }
  Tv.settext ("Updated TextView");}}

The above is the four kinds of Android asynchronous update UI way, hope to help everyone's learning.

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.