Use Handler to update the UI in the Child thread and the handler thread to update the ui

Source: Internet
Author: User

Use Handler to update the UI in the Child thread and the handler thread to update the ui

Android requires that you only update the UI in the main thread. If you update the UI in the Child thread, the following error is prompted: only the original thread that created a view hierachy can touch its view (Only the original thread can create a view level to touch its view ).

The reason why the UI can only be updated in the main thread is that the related views and controls in android are NOT thread-safe and must be processed separately.

Sometimes you need to update the UI in the Child thread. The following describes how to use Handler to implement thread communication to update the UI in the Child thread.

Handler usage:

1. to schedule messages and runnables to be executed as some point in the future;

Schedule messages and runnables to be executed at a certain time point in the future.

2. to enqueue an action to be passed med on a different thread than your own.

Bind the action to the queue for execution in a different thread. That is, inter-thread communication can be realized. For example, when you create a sub-thread, you can obtain the Handler object created in the parent thread in your sub-thread to send messages to the Message Queue of the parent thread. Because Android requires updating the interface in the UI thread, you can update the interface in other threads through this method.

The sub-thread updates the UI instance:

Steps:

1. Create a Handler object (this is created in the main thread to facilitate UI updates ).

2. Build a Runnable object and update the interface in Runnable.

3. post to the UI thread in the run method of the sub-thread and update the UI using the runnable object.

The Code is as follows:

Package djx. android; import djx. downLoad. downFiles; import android. app. activity; import android. OS. bundle; import android. OS. handler; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. textView; public class downLoadPractice extends Activity {private Button button_submit = null; private TextView textView = null; private String content = null; private Handler handler = null; /** Called when the activity is first created. * // @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); // create handler = new Handler (); button_submit = (Button) findViewById (R. id. button_submit); textView = (TextView) findViewById (R. id. textView); button_submit.setOnClickListener (new submitOnClieckListener ();} // Add the listener class submitOnClieckListener implements OnClickListener {@ Overridepublic void onClick (View v) for the button) {// the robot is deployed as a server. The final DownFiles df = new DownFiles ("http: // 192.168.75.1: 8080/downLoadServer/a.txt") is displayed in textView as the.txt file. setText ("loading ...... "); new Thread () {public void run () {content = df. downLoadFiles (); handler. post (runnableUi );}}. start () ;}/// construct the Runnable object. In runnable, update the interface Runnable runnableUi = new Runnable () {@ Overridepublic void run () {// update the textView. setText ("the Content is:" + content );}};}


 

Reference URL:

1. Http://blog.csdn.net/djx123456/article/details/6325983

2. Detailed thread details link http://lavasoft.blog.51cto.com/62575/27069/

 

 

 


Android uses Handler to update the UI of a sub-Thread

You have too many problems writing this code, but your requirements are easy to implement. You can upload the textview object to the ServerThread and write the following code in the ServerThread:
Handler handler = new Handler (lofter. getMainLooper ()){
HandleMessage (Message msg ){
// Set text for Textview
}
}
In ServerThread, use the handler above to send messages where you want to set text for textview.

After handler is initialized in the Android sub-thread, why can this sub-thread also update the UI? (IN)

The Android UI cannot be updated in the hanlder of the sub-thread.

To put it simply:

If you create a hander in a child thread, use the following code:
New Handler (Looper. getmainloler ())
The logoff of the main thread is used.


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.