Intentservice in-depth analysis of Android service learning
Source: Internet
Author: User
<span id="Label3"></p><p><p></p></p> <!--正文 begin-->What is intentservice? (from Http://blog.csdn.net/gaojie314/archive/2010/11/28/6040701.aspx) The official explanation is that Intentservice is a base class for<span style="color: #0000ff;"><span style="color: #0000ff;">Service</span></span>s that handle asynchronous requests (expressed as<span style="color: #0000ff;"><span style="color: #0000ff;">Intent</span></span>S) on Demand. Clients send requests through<span style="color: #0000ff;"><span style="color: #0000ff;">Android.content.Context.startService (Intent)</span></span>Calls The service is started as needed, handles each Intent in turn using a worker thread, and stops itself when it runs out of Work.<br>This "work queue processor" pattern was commonly used to offload the tasks from a application ' s main thread. The Intentservice class exists to simplify this pattern and take care of the Mechanics. To use it, extend Intentservice and implement<span style="color: #0000ff;"><span style="color: #0000ff;">onhandleintent (Intent)</span></span>. Intentservice would receive the Intents, launch a worker thread, and stop the service as Appropriate.<br>All requests is handled on a single worker Thread--they could take as long as necessary (and would not block the Applicati On ' s main loop), though only one request is processed at a time. meaning: Intentservice is a<span style="color: #0000ff;"><span style="color: #0000ff;">Context.startservice (Intent)</span></span>Start a service that can handle an asynchronous request, and you only need to inherit intentservice and override it.<span style="color: #0000ff;"><span style="color: #0000ff;">onhandleintent (Intent)</span></span>Method receives a intent object and stops itself at the appropriate time (usually when the job is completed). All requests are processed in one worker thread, alternating (but not blocking the execution of the main thread), and only one request can be executed at a Time. (* * I modified some translations of the original Text) below is the source code to Analyze:<span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Abstract</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span>Intentservice<span style="color: #0000ff;"><span style="color: #0000ff;">extends</span></span>Service {<br><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">volatile</span></span>Looper mservicelooper;<span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">volatile</span></span>Servicehandler mservicehandler;<br><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span>String mname;<span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Boolean</span></span>mredelivery;<br> <span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span>Servicehandler<span style="color: #0000ff;"><span style="color: #0000ff;">extends</span></span>Handler {<br><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span>Servicehandler (Looper Looper) {<span style="color: #0000ff;"><span style="color: #0000ff;">Super</span></span>(looper); } @Override<span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>Handlemessage (Message msg) {onhandleintent ((Intent) msg.obj); Stopself (msg.arg1); }<br>From the source can be analyzed: Intentservice is actually the Looper,handler,service collection, he not only has the function of the service, there are processing and cyclic message Function.<br>Below is the source code for OnCreate (): @Override<span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>OnCreate () {<span style="color: #0000ff;"><span style="color: #0000ff;">Super</span></span>. onCreate ();<br>Handlerthread thread =<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>Handlerthread (<span style="color: #800000;"><span style="color: #800000;">"intentservice["</span></span>+ Mname +<span style="color: #800000;"><span style="color: #800000;">"]"</span></span>); Thread.Start ();<br>Mservicelooper = Thread.getlooper (); Mservicehandler =<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>Servicehandler (mservicelooper); } analysis: Intentservice creates a handler thread (handlerthread) and starts it and then gets the Looper object of the current thread to initialize Intentservice mservicelooper, The Mservicehandler object is then Created. Below is the source code for OnStart (): @Override<span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>OnStart (Intent Intent,<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>Startid) {Message msg = Mservicehandler.obtainmessage (); MSG.ARG1 = startid; Msg.obj = intent;<br>Mservicehandler.sendmessage (msg); } analysis: When you start intentservice, you will generate a message with Startid and intent and send it to messagequeue, then Looper find the message in messagequeue, The handler processing message is stopped, and the following code is processed: @Override<span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>Handlemessage (Message msg) {onhandleintent ((Intent) msg.obj); Stopself (msg.arg1); Then call Onhandleintent ((Intent) msg.obj), which is an abstract approach, in fact, we want to rewrite the implementation of the method, we can in this method to deal with our Work. stopself (msg.arg1) is called when the task is completed This method ends the specified Work. when all the work is done: the OnDestroy method will be executed, the source code is as Follows: @Override<span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>OnDestroy () {mservicelooper.quit (); Call this method after the service Mservicelooper.quit () to make Looper stop.<br>Through the analysis of the source code: this is a message-based service, each time you start the service does not immediately deal with your work, but first will create the corresponding Looper,handler and added in MessageQueue with the customer intent message object, When Looper finds a message, it then gets Intent object passed in Onhandleintent ((Intent) Msg.obj) Your handler is called in. The service is stopped after processing. meaning that the life cycle of intent is consistent with the task you are dealing With. so this class is very good with the download task, and the service itself ends the exit after the download task Finishes.<p><p>Intentservice in-depth analysis of Android service learning</p></p></span>
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.