How Android asynchronously updates the UI interface

Source: Internet
Author: User

Under the Android platform, for multithreaded programming, it is often necessary to do some processing in a separate thread outside the main thread, and then update the user interface display. However, the problem with directly updating the page display in a thread other than the main thread is that the system will report this exception, android.view.viewroot$calledfromwrongthreadexception:only the original thread That created a view hierarchy can touch it views. (Only the thread that originally created the view hierachy) can modify its view. )。

This means that the interface must be updated in the main thread of the program (that is, the UI thread). You can use one of the following methods to resolve:

Solution 1: Create an instance of the handler class in Activity.oncreate (Bundle savedinstancestate) and invoke the function shown in the update interface in the Handlemessage callback function of the handler instance. For example:

  

[Java]View Plaincopy
  1. Public class Exampleactivity extends Activity {
  2. Handler h = null;
  3. @override
  4. public void OnCreate (Bundle savedinstancestate) {
  5. h = New Handler () {
  6. @override
  7. public void Handlemessage (Message msg) {
  8. //Call Update GUI method.
  9. }
  10. };
  11. }
  12. }



In other functions, send or mail messages to this h using the Send family or post family functions.

Solution 2: Leverage Activity.runonuithread (runnable)

The code that updates the UI is created in Runnable, and the Runnable object is passed to Activity.runonuithread (runnable) When the UI needs to be updated. This way, the runnable can be called in the UI program.

Note that the runnable in this place is not actually a dynamic update, and if you use this method in OnCreate, the interface will not show up, as it is necessary to wait for the Runnable method call to complete before the display interface can be displayed. So by initializing the interface to get the network data, it is not advisable to use this method directly ...

As follows:

[Java]View Plaincopy
  1. Class Test implements Runnable
  2. {
  3. @Override
  4. public Void Run ()
  5. {
  6. int len = 3;
  7. int i=0;
  8. While (i < len)
  9. {
  10. System.out.println ("LJZ");
  11. i++;
  12. Textview.settext ("Hello |" + System.currenttimemillis ());
  13. Textview.invalidate ();
  14. try {
  15. Thread.Sleep (1000);
  16. } catch (Exception e) {
  17. E.printstacktrace ();
  18. }
  19. }
  20. }
  21. }

Make the following call in the main interface:

MainActivity.this.runOnUiThread (test);

The result is that the method of creating the OnCreate time is getting bigger.

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.