Android Looper update UI in non-UI threads

Source: Internet
Author: User

A toast was used in the test service to print the log.

Prompt cannot call Looper.prepare ()

After adding, the code executes normally.

Looper.prepare (); Toast.maketext (Getapplicationcontext (), "Services Thread", Toast.length_long). Show () Looper.loop ();


But then there was another toast, and then there was an error stating that each thread could have only one looper


caused by:java.lang.RuntimeException:Only one Looper may be created per thread

To view the data, prepared will create a looper.

So the other place no longer needs to be called again.

     //Looper    public static final void  Prepare ()  {        if  (Sthreadlocal.get ()  != null)  {            throw new  RuntimeException ("Only one looper may be created per thread");         }        sthreadlocal.set (New  looper ());   //Create a looper      }     in the current thread Private looper ()  {        mQueue = new  MessageQueue ();   //the key here, the creation of Looper has done nothing.   is actually creating a message queue           mRun = true;         mthread = thread.currentthread ();    &nbsP;}     looper    public static final void prepare ()  {        if  (Sthreadlocal.get ()  != null)  {             throw new runtimeexception (" Only one looper may be created per thread ");         }        sthreadlocal.set (New Looper ());   //Create a Looper    }    private looper () in the current thread  {         mqueue = new messagequeue ();   //the key here, Create what Looper have done.   is actually creating a message queue         mRun = true;         mthread = thread.currentthread ();     }




Android Looper 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.