To update the UI in a child thread using handler

Source: Internet
Author: User

Android rules only update the UI in the main thread, and if you update the UI in a child thread, you will be prompted with the following error: Only the original thread, created a view hierachy can touch it view (only Having the original thread creates a view hierarchy that can touch its view).

The reason that the UI can only be updated in the main thread is that the relevant view and controls in Android are not thread-safe and we have to do it alone.

There are times when you need to implement the update UI in a sub-thread, and the following describes the feature of using handler to implement thread communication to update the UI in a child thread.

Handler Use occasions:

1, to schedule messages and runnables to being executed as some point in the future;

Schedule messages and runnables to be executed at a later point in time.

2, to enqueue an action to being performed on a different thread than your own.

The action is queued for execution in a different thread. That is, you can implement inter-thread communication. For example, when you create a child thread, you can then get the handler object created in the parent thread from your child thread, and you can send a message to the parent thread's message queue through that object. Because Android requires the interface to be updated in the UI thread, you can update the interface in other threads with this method.

The child thread updates the UI instance:

Steps:

1. Create the Handler object (this is created in the main thread for easy updating of the UI).

2, constructs the Runnable object, updates the interface in the runnable.

3. Update the UI by post,runnable the object to the UI thread in the child thread's Run method.

The detailed code is as follows:

[Java]View Plaincopyprint?
  1. Package  djx.android;
  2. Import Djx.downLoad.DownFiles;
  3. Import  android.app.Activity;
  4. Import Android.os.Bundle;
  5. Import  Android.os.Handler;
  6. Import Android.view.View;
  7. Import Android.view.View.OnClickListener;
  8. Import Android.widget.Button;
  9. Import  Android.widget.TextView;
  10. Public class downloadpracticeextends Activity {
  11. Private Button button_submit= NULL ; 
  12. Private TextView textview= NULL ; 
  13. Private String content= NULL ; 
  14. Private Handler handler= NULL ; 
  15. /** Called when the activity is first created. * /  
  16. @Override  
  17. Public void onCreate (Bundle savedinstancestate) {
  18. Super . OnCreate (savedinstancestate);
  19. Setcontentview (R.layout.main);
  20. //Create a handler that belongs to the main thread  
  21. handler=New handler ();
  22. button_submit= (Button) Findviewbyid (r.id.button_submit);
  23. textview= (TextView) Findviewbyid (R.id.textview);
  24. Button_submit.setonclicklistener (new Submitoncliecklistener ());
  25. }
  26. //Add listener to button  
  27. class Submitoncliecklistener Implements onclicklistener{
  28. @Override  
  29. Public void OnClick (View v) {
  30. //local machine deployed as server, download a.txt file contents from Local on TextView display  
  31. Final downfiles df= New downfiles ("Http://192.168.75.1:8080/downLoadServer/a.txt");
  32. Textview.settext ("Loading ...");
  33. New Thread () {
  34. Public void run () {
  35. Content=df.downloadfiles ();
  36. Handler.post (RUNNABLEUI);
  37. }
  38. }.start ();
  39. }
  40. }
  41. //Build Runnable object, update interface in runnable  
  42. Runnable runnableui=New Runnable () {
  43. @Override  
  44. Public void run () {
  45. //Update interface  
  46. Textview.settext ("The Content is:"+content);
  47. }
  48. };
  49. }

To update the UI in a child thread using handler

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.