Android to update UI in non-UI threads

Source: Internet
Author: User

The exception that appears when you update the UI (Program interface) in a non-UI thread is as follows:


So how do you get a finer UI in a non-UI thread?

There are many ways to do this, and here are two main types:

The first type: Add looper.prepare () after the line of code that needs to update the UI; With Looper.loop (); Two words can be. Such as:

New Thread () {@Overridepublic void Run () {//TODO auto-generated method Stubtxtrotation.settext ("Update ui! in a non-UI thread "); Looper.prepare (); Looper.loop ();}}. Start ();

The second type: Use the following method:

New Thread () {@Overridepublic void Run () {//TODO auto-generated method Stubshowtoastbyrunnable (Mainactivity.this, "", 3000);}}. Start ();
/** * Use toast in a non-UI thread * @param Context Context * @param text to display the message content * @param the time the duration message was displayed * */private void Showtoastbyru Nnable (Final context context, final charsequence text, final int duration) {    Handler Handler = new Handler (Looper.getm Ainlooper ());    Handler.post (New Runnable () {        @Override public        void Run () {            Toast.maketext (context, text, duration). Show ( );        }    });}




Android to update UI in non-UI threads

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.