Android Time consuming task _handlerthread

Source: Internet
Author: User

HandlerthreadIn the last Android time-consuming task _handler introduced the handler mechanism, and introduced a simple example of a common thread that generates Looper and communicates using handler mechanisms. We know that there is no looper in the normal thread, and it is not good to use the handler mechanism in normal thread space, and it would be a little cumbersome to do it each time as in the previous example. In fact, Android has encapsulated a thread that has its own looper handlerthread, its implementation and the example given in the previous article basic one, just a little more professional. The following is the detailed code for this class.
public class Handlerthread extends Thread {private int mpriority;     private int Mtid =-1;      Private Looper Mlooper;        Publichandlerthread (String name) {super (name);    Mpriority =process.thread_priority_default;        } publichandlerthread (String name, int priority) {super (name);    Mpriority =priority;        } protected void Onlooperprepared () {} public void Run () {Mtid =process.mytid ();        Looper.prepare ();            Synchronized (this) {Mlooper =looper.mylooper ();           Notifyall ();       } process.setthreadpriority (mpriority);        Onlooperprepared ();          Looper.loop ();    Mtid =-1;        Public Looper Getlooper () {if (!isalive ()) {return null;        }//If The Threadhas been started, wait until the Looper has been created. Synchronized (this) {while (isAlive () && mlooper = = null) {try {wait (      );          } catch (Interruptedexception e) {}}} return mlooper;        } public Boolean quit () {Looper Looper =getlooper ();            if (Looper!=null) {looper.quit ();        return true;    } return false;    } public Intgetthreadid () {return mtid; }}
This class is inherited from the thread class, when using this class must be aware of the need to start (), otherwise the run () method is not called, the handler mechanism is not built up.
Classic ApplicationsA classic application for Handlerthread is the application in the service, we know that the General Service is running in the main thread, the Android time consuming task _ In ANR I also recommend that you start the service in Broadcastreceiver and start the thread processing time-consuming task in the service. So how to start a thread, here's a classic code:

public class Backservice extends Service {private Servicehandler servicehandler; @Overridepublic ibinder Onbind (Intent ARG0) {return null;} Private Final class Servicehandler extends Handler {public Servicehandler (Looper Looper) {super (Looper);} @Overridepublic void Handlemessage (Message msg) {super.handlemessage (msg); Onhandleintent ((Intent) msg.obj);// The stop service is executed only when its parameter startid is equal to the ID generated when the service was last started. Stopself (MSG.ARG1);}} @Overridepublic void OnCreate () {super.oncreate (); Handlerthread thread = new Handlerthread ("Backservice"); Thread.Start (); Looper servicelooper = Thread.getlooper (); servicehandler = new Servicehandler (servicelooper);} @Overridepublic void OnStart (Intent Intent, int startid) {Message msg = Servicehandler.obtainmessage (); msg.arg1 = Startid ; msg.obj = Intent;servicehandler.sendmessage (msg);} protected void Onhandleintent (Intent Intent) {               //Do your asynchronous task}}





Android Time consuming task _handlerthread

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.