Basic Principles of handlerthread class and intentservice class

Source: Internet
Author: User

Requirement: Sometimes a message needs to be sent from the main thread to the working thread to process the message in the working thread.

Principle:

Handlerthread class

Handlerthread provides a getlooper method to return a looper object. Create a handler instance, Handler = new handler (lofter), so that handler is associated with the logoff object, and then logoff polls the messagequeue of the working thread to process messages.

Intentservice class

We know that startservice (intent) is used to start the service. It will pass an intent object that is started this time, combined with the features of handlerthread, it can be designed to process intent in the worker thread, that is, the handlerthread instance. Combined with the following intentservice to simplify code instances, we found that the onstartcommand method is almost the same in various businesses, and the ondestroy method is also a fixed mode, in the oncreate method, the processing method is different only when the message content intent is received. Therefore, the intent processing method is removed and other code is fixed to form the intentservice class, the sub-class only needs to implement the abstract class that is stripped to implement the processing method.

In general, handlerthread and intentservice can be used in combination. intentservice can be started multiple times from the main thread and intentservice can be transferred. After being started, intentservice will create a handlerthread instance and associate handler with handlerthread, handler sends the intent encapsulated message, and then handler processes the message. After the processing is completed, the startup is stopped.

There are three main methods for starting a service:

Public void oncreate (){}

Public int onstartcommand (intent, int flags, int startid ){}

Public void ondestroy (){}

Application: for example, it is used to provide services for music downloads. The above process is executed once each click to download music.

Handlerthread simplified code example:

Public class myhandlerthread extends thread {private Looper looper; Public Looper getlooper () {If (! Isalive () Love = NULL; If (love = NULL) {synchronized (this) {try {This. wait ();} catch (interruptedexception e) {// todo auto-generated catch blocke. printstacktrace () ;}}return Looper ;}@ overridepublic void run () {// create a message queue logoff. prepare (); // obtain the logoff object logoff = logoff. mylooper (); synchronized (this) {This. Y ();} // start round robin logoff. loop ();} public void quit () {If (loid! = NULL) {Looper. Quit ();}}

Public abstract class myintentservice extends Service {private handlerthread thread; private handler; @ overridepublic void oncreate () {thread = new handlerthread ("workthread"); thread. start (); logoff = thread. getlooper (); handler = new handler (loler) {@ overridepublic void handlemessage (Message MSG) {// process the asynchronous task handlerintent (intent) MSG. OBJ); // stop the stopself (MSG. arg1) ;}}; super. oncreate () ;}@ overridepublic int onstartcommand (intent, int flags, int startid) {message MSG = message. obtain (); MSG. arg1 = startid; MSG. OBJ = intent; handler. sendmessage (MSG); return Super. onstartcommand (intent, flags, startid) ;}@ overridepublic void ondestroy () {thread. quit (); super. ondestroy () ;}@ overridepublic ibinder onbind (intent) {// todo auto-generated method stubreturn NULL;} public abstract void handlerintent (intent );

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.