Android uses multithreading to update UI samples to share _android

Source: Internet
Author: User

The technology involved with Android threads is: Handler; message; MessageQueue; Looper; Handlerthread.

Here's a section of code that updates the UI in a thread:

Copy Code code as follows:

public class Mainactivity extends activity {
Private TextView timelable;
Private Button stopbtn;
Private Thread Mthread;
Private Boolean isrunning = true;
private int timecount = 0;

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Timelable = (TextView) Findviewbyid (r.id.timelable);
STOPBTN = (Button) Findviewbyid (r.id.stop);
Stopbtn.setonclicklistener (New Onclicklistener () {

@Override
public void OnClick (View v) {
IsRunning = false;
}
});

Mthread = new Thread (new Runnable () {

@Override
public void Run () {
while (isrunning) {
try {
Thread.Sleep (1000);
timecount++;
Timelable.settext ("timecount=" + Timecount + "seconds");
catch (Exception e) {
E.printstacktrace ();
}
}

}
});
Mthread.start ();
}
}

This code just updates the display of the TextView in the thread, but does not see the effect after execution, and reports a mistake: Android.view.viewrootimpl$calledfromwrongthreadexception:only the Original thread that created a view hierarchy can touch its views.
Updating UI processing in Android must be updated by the thread that created it, not in other threads. This is the reason for the above error.

Because Timelable is a UI control that is created in the main thread, it is updated in a child thread, and the update operation is implemented in the Mthread thread's Run () method. Such a process violates the Android multithreaded programming rules and throws an exception.

To solve this problem, it is necessary to identify the main thread and child thread responsibilities. The responsibility of the main thread is to create, display, and update UI controls, handle UI events, start child threads, stop child threads, and so on; the responsibility of a child thread is to compute the time and issue an update UI message to the main thread, rather than updating the UI directly. A child thread sends a message to the main thread, which can be implemented with handler. The code is as follows:

Copy Code code as follows:

public class Mainactivity extends activity {

Private TextView timelable;
Private Button stopbtn;
Private Thread Mthread;
Private Boolean isrunning = true;
private int timecount = 0;

Final private Handler Mhandler = new Handler () {
public void Handlemessage (msg) {
Switch (msg.what) {
Case 0:
Timelable.settext ("timecount=" + Timecount + "seconds");
Break
Default:
Break
}
}
};

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Timelable = (TextView) Findviewbyid (r.id.timelable);
STOPBTN = (Button) Findviewbyid (r.id.stop);
Stopbtn.setonclicklistener (New Onclicklistener () {

@Override
public void OnClick (View v) {

IsRunning = false;
}
});

Mthread = new Thread (new Runnable () {

@Override
public void Run () {
while (isrunning) {
try {
Thread.Sleep (1000);
timecount++;
Mhandler.sendemptymessage (0);
catch (Exception e) {
E.printstacktrace ();
}
}

}
});
Mthread.start ();
}
}


After the run does not report before the error, TextView can also normal update content.

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.