Android Non-ui to UI Thread Communications (Part 2 of 5)

Source: Internet
Author: User

original:http://www.intertech.com/blog/android-non-ui-to-ui-thread-communications-part-2-of-5/

The second part of my series of blogs posts on Android non-ui thread-to-ui thread communications.  See here for the start of the series. As a refresher, this series of posts are about what Non-ui threads can communicate back to the UI thread in an Android appli  cation in order to update the user interface.  Non-ui threads is not a allowed to make updates to the UI. Trying to does too much work (as defined as not allowing the user to interact with the UI for more than 5 seconds) on the UI Thread leads to ANR errors.

The Simple App–review

In my last Post, I provided a simple app (called Simple app) that provided the buttons to start/stop a Non-ui thread. The Non-ui thread ' s job is to simulate long running work were by generating a random number, and the UI to has a TextView Widget Update the display of the random number, and then sleep for a number of seconds.

In the first of this five part series, I showed the "how to" use an Activity's Runonuithread () method in a Non-ui thread to  Post requests to change the user interface via the UI thread ' s event message queue. In this blog, option #2 is explained–using the post () method of a Android View component to request changes to the UI.

The Simpleapp example code for demonstrating option #2 can is found here (in an Eclipse project ZIP file).

OPTION 2–using VIEW ' S POST () METHOD

When a Non-ui thread had access to a View component from the user interface, the NON-UI thread can make a call to that Vie W component ' s post () method to request a UI change.

Just if calling on Runonuithread () of a Activity, calling on the View's post () communicates the desire to request  Work being run on the UI thread is publishing a message in the event queue of the UI thread.  When it is can, the UI thread picks up the action message in the event queue and performs the UI change. So the post () method is a convenience (just as Runonuithread are) for completing this messaging operation.

Here are the Non-ui thread class–dosomethingthread from the simpleapp–that generates a random number and then calls on Its publishprogress () method to get the random number displayed on the UI using post ().

1234567891011121314151617181920212223242526272829303132 public class dosomethingthread extends Thread {   private static final string tag = "Dosomethingthread" ;  private static final int delay = 5000; //5 seconds private static final int random_multiplier = ten; @Override Public void run() { Log. V(TAG, "doing work in Random number Thread"); While (true) {       int Span class= "crayon-v" >randnum = (int) (math. Random () * random_multiplier) publishprogress(randnum); try { Thread. Sleep(DELAY); } catch (interruptedexception e) { Log. V(TAG, "interrupting and stopping the Random number Thread"); return;       }    }  } private void publishprogress(int randnum) {     log. V (tag, "reporting back from the Random number Thread"     final string text = string.format (getstring r. String. Service_msg , Randnum Mainfrag. Resultstextview. Post(new Runnable() { @Override Public void run() { Mainfrag. Getresultstextview(). SetText(text);       }    });  }}

Note in line 3 of the Publishprogress () method above in the one line of code this changes between Option 1 (using Runonuit Hread) and Option 2 here (using POST).

Considerations of OPTION 2–post () METHOD

As indicated, the post () method is using the same event message queue under the covers as Runonuithread ().  So in some regards, many of the pro/cons of the post () is that of Runonuithread (). A StackOverflow Post (here) provides some detailed insight.

Essentially, in order-to-use post (), your non-ui thread must has awareness of a View component (from the UI).  This allows the NON-UI thread to avoid direct connection to the Activity (which is required with runonuithread).  There is times when your non-ui thread could know a View and not an Activity or vice versa. However, both options might more tightly couple the non-ui thread to the UI side given View components and activities is UI "stuff."

Also As with the Runonuithread () option, this post () method option requires you to create and manage your threads more Dir Ectly–and thereby has the more control of the thread and its communication but also require your to has more experience wit H Java concurrency/thread APIs and issues.

Unlike the Runonuithread () method, the post () method does not check whether the current thread is the UI thread.  Therefore the Post () method does not cause the Runnable to execute immediately if it's on the UI thread. Instead, post () always have a event message pushed to the message queue for reaction by the UI thread.

WRAP up

Stay tuned to this blog site for the next 3 options regarding the Android thread communications.  Again, if you need help on the your mobile project or need some mobile training (Android, IOS, other) let's Intertech help. Contact US.



Read more:http://www.intertech.com/blog/android-non-ui-to-ui-thread-communications-part-2-of-5/#ixzz3MyttDTTp
Follow us: @IntertechInc on Twitter | Intertech on Facebook

Android Non-ui to UI Thread Communications (Part 2 of 5)

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.