To update the UI in a child thread using handler

Source: Internet
Author: User
Tags message queue

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 the original A 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:

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 a handler Handler=new handler () that belongs to the main thread;        button_submit= (Button) Findviewbyid (r.id.button_submit);        textview= (TextView) Findviewbyid (R.id.textview);    Button_submit.setonclicklistener (New Submitoncliecklistener ()); }//Add listener for Button class Submitoncliecklistener implements onclicklistener{@Overridepublic void OnClick (View v) {//Local machineDeploy as a server, download the contents of the A.txt file locally on TextView display final downfiles df=new downfiles ("Http://192.168.75.1:8080/downLoadServer/a.txt" ); Textview.settext ("Loading ..."); new Thread () {public void run () {content=df.downloadfiles (); Handler.post ( RUNNABLEUI);        }}.start ();} }//Build Runnable object, update interface in Runnable Runnable runnableui=new Runnable () {@Overridepublic void Run () {//update interface textview.se Ttext ("The Content is:" +content);};}


Reference URL:

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

2. Detailed Thread Detail link http://lavasoft.blog.51cto.com/62575/27069/

To update the UI in a child thread using handler

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.