Android intentservice Analysis

Source: Internet
Author: User

What is intentservice?

The official explanation is:
Intentservice is a base class for services that handle asynchronous requests (expressed as intents) on demand. clients send requests through android. content. context. startservice (intent) CILS; the service is started as needed, handles each intent in turn using a worker thread, and stops itself when it runs out of work.

This "work queue processor" pattern is commonly used to offload tasks from an application's main thread. the intentservice class exists to simplify this pattern and take care of the mechanic. to use it, extend intentservice and implement onhandleintent (intent ). intentservice will receive the intents, launch a worker thread, and stop the service as appropriate.

All requests are handled on a single worker thread -- they may take as long as necessary (and will not block the application's main loop ), but only one request will be processed at a time.
 

Intentservice is. startservice (intent) starts a service that can process asynchronous requests. When using this service, you only need to inherit the intentservice and override the onhandleintent (intent) method to receive an intent object, stop yourself when appropriate (usually when the work is completed ). all requests are processed in one working thread, and they will be executed alternately (but will not block the execution of the main thread). Only one request can be executed at a time. (** I have modified some original translations)
The source code to be analyzed is as follows:
Public abstract class intentservice extends Service {

Private volatile low.mserviceloaders;
Private volatile servicehandler mservicehandler;

Private string mname;
Private Boolean mredelivery;

Private final class servicehandler extends handler {

Public servicehandler (low.logoff ){
Super (logoff );
}

@ Override
Public void handlemessage (Message MSG ){
Onhandleintent (intent) msg. OBJ );
Stopself (msg. arg1 );
}

}

From the source code, we can analyze the following:
Intentservice is actually a collection of logoff, handler, and service. It not only provides service functions, but also supports processing and circulating messages.

The source code of oncreate () is as follows:
@ Override
Public void oncreate (){
Super. oncreate ();

Handlerthread thread = new handlerthread ("intentservice [" + mname + "]");
Thread. Start ();

Mserviceloaders = thread. getlooper ();
Mservicehandler = new servicehandler (mserviceloader );
}

Analysis: When intentservice is created, Handler thread will be created and started, and then the logoff object of the current thread will be obtained to initialize mserviceloading of intentservice, and then the mservicehandler object will be created.
The following is the source code of onstart (): @ override
Public void onstart (intent, int startid ){
Message MSG = mservicehandler. obtainmessage ();
MSG. arg1 = startid;
MSG. OBJ = intent;

Mservicehandler. sendmessage (MSG );
}

Analysis: When you start intentservice, a message with startid and intent will be generated and sent to messagequeue. When logoff finds a message in messagequeue, handler will stop processing the message, the code to be processed is as follows:

@ Override
Public void handlemessage (Message MSG ){
Onhandleintent (intent) msg. OBJ );
Stopself (msg. arg1 );
}

Next, call onhandleintent (intent) MSG. OBJ). This is an abstract method. In fact, we need to rewrite the implementation method. We can process our work in this method. when the task is completed, stopself (MSG. arg1) to end the specified job.

After all the work is completed: The ondestroy method will be executed. The source code is as follows:

@ Override
Public void ondestroy (){
Mserviceloading. Quit ();
}

Call the mserviceloading. Quit () method after the service ends to stop the logoff.

The source code is analyzed as follows:
This is a message-based service. Every time you start this service, it does not process your work immediately, but first creates the corresponding logoff, handler and added the message object with the client intent in messagequeue. When logoff finds a message, it obtains the intent object through the onhandleintent (intent) MSG. OBJ) to call your processing program. after processing, your services will be stopped. this means that the life cycle of intent is consistent with that of the task you process. therefore, this class is usually used for download tasks. After the download task is completed, the Service stops and exits.

This article is from the "android" blog, please be sure to keep this source http://1622511.blog.51cto.com/1612511/568990

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.