<span id="Label3"></p><p><p>Intentservice is a class that inherits and processes asynchronous requests, and within Intentservice there is a worker thread that handles time-consuming operations, starting intentservice the same way as a traditional service, and when the task finishes executing, Intentservice will automatically stop without the need for us to manually control or stopself (). In addition, you can start Intentservice multiple times, and each time-consuming operation is performed in the Onhandleintent callback method of Intentservice in the form of a work queue, and only one worker thread is executed at a time, and then the second executes the first one, and so On.</p></p><p><p>In Android development, we may encounter such a business need, a task is divided into several sub-tasks, sub-tasks executed sequentially, after the completion of all subtasks, the task is only Successful. then, It is possible to do this with a few sub-threading sequence execution, but each thread must be manually controlled, and another child thread will be opened after one of the sub-threads has finished executing. or, put them all in one thread and let them execute sequentially. This can be done, however, if this is a background task, you have to put in the service, because the service and activity are peer, so to perform time-consuming tasks, you have to open a sub-thread in the service to Execute. so, is there a simple way to deal with this process, the answer is Intentservice.</p></p><p><p>What is intentservice, first look at the official explanation:<br>Intentservice is a base class Forservices this handle asynchronous requests (expressed Asintents) on Demand. Clients send requests Throughstartservice (Intent) 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</p></p><p><p><br>Simply put, Intentservice is a class that inherits from the service and processes asynchronous requests, and within Intentservice there is a worker thread to handle the time-consuming operation, starting the intentservice in the same way as the traditional service, and When the task is finished, the Intentservice will stop automatically without the need for us to manually control it. In addition, you can start Intentservice multiple times, and each time-consuming operation is performed in the Onhandleintent callback method of Intentservice in the form of a work queue, and only one worker thread is executed at a time, and then the second executes the first one, and so On.<br>There is also a note:<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), but only one request would be processed at a Time.<br>The general meaning is that all requests are in one single thread and do not block the Application's main thread (UI Thread), and only one request is processed at a Time.<br>So what are the benefits of using intentservice? first, we omit the hassle of manual threads in the service, and second, when the operation is complete, we do not have to stop the service manually, third, it's so easy to use!</p></p><pre><span style="color: #0000ff"><span style="color: #0000ff"></span> package</span><span style="color: #000000"><span style="color: #000000">im.weiyuan.com.viewutils;</span></span><span style="color: #0000ff"><span style="color: #0000ff">Import</span></span><span style="color: #000000"><span style="color: #000000">android.app.IntentService;</span></span><span style="color: #0000ff"><span style="color: #0000ff">Import</span></span><span style="color: #000000"><span style="color: #000000">android.content.Intent;</span></span><span style="color: #0000ff"><span style="color: #0000ff">Import</span></span><span style="color: #000000"><span style="color: #000000">android.content.Context;</span></span><span style="color: #0000ff"><span style="color: #0000ff">Import</span></span><span style="color: #000000"><span style="color: #000000">android.os.SystemClock;</span></span><span style="color: #0000ff"><span style="color: #0000ff">Import</span></span><span style="color: #000000"><span style="color: #000000">android.util.Log;</span></span><span style="color: #008000"><span style="color: #008000">/**</span></span><span style="color: #008000"><span style="color: #008000">* An {</span></span><span style="color: #808080"><span style="color: #808080">@link</span></span><span style="color: #008000"><span style="color: #008000">intentservice} subclass for handling asynchronous task requests in * a service on a separate handler Thread. * <p> * todo:customize class-update Intent actions, Extra parameters and static * Helper METHODS. </span></span><span style="color: #008000"><span style="color: #008000">*/</span></span><span style="color: #0000ff"><span style="color: #0000ff"></span> public</span> <span style="color: #0000ff"><span style="color: #0000ff">class</span></span>Myintentservice<span style="color: #0000ff"><span style="color: #0000ff">extends</span></span><span style="color: #000000"><span style="color: #000000">Intentservice {</span></span><span style="color: #008000"><span style="color: #008000">/**</span></span><span style="color: #008000"><span style="color: #008000">* You must write a parameterless constructor, and then call the parent class Super ("myintentservice"); * Where Myintentservice is the name of the thread that executes the onhandleintent corresponding *</span></span><span style="color: #008000"><span style="color: #008000">*/</span></span> <span style="color: #0000ff"><span style="color: #0000ff"></span> public</span><span style="color: #000000"><span style="color: #000000">myintentservice () {</span></span><span style="color: #0000ff"><span style="color: #0000ff">Super</span></span>("myintentservice"<span style="color: #000000"><span style="color: #000000">); } </span></span><span style="color: #008000"><span style="color: #008000">/**</span></span><span style="color: #008000"><span style="color: #008000">* The Onhandleintent function is executed in the sub-thread, so there is no need to open the thread *</span></span><span style="color: #008000"><span style="color: #008000">*/</span></span><span style="color: #000000"><span style="color: #000000">@Override</span></span><span style="color: #0000ff"><span style="color: #0000ff">protected</span></span> <span style="color: #0000ff"><span style="color: #0000ff">void</span></span><span style="color: #000000"><span style="color: #000000">onhandleintent (Intent Intent) {log.d (</span></span>"123456", "onhandleintent is called"<span style="color: #000000"><span style="color: #000000">); </span></span><span style="color: #008000"><span style="color: #008000">/**</span></span><span style="color: #008000"><span style="color: #008000">* Simulated time-consuming operation *</span></span><span style="color: #008000"><span style="color: #008000">*/</span></span><span style="color: #000000"><span style="color: #000000">Systemclock.sleep (</span></span>10000<span style="color: #000000"><span style="color: #000000">); LOG.D (</span></span>"123456", "onhandleintent is out"<span style="color: #000000"><span style="color: #000000">); } </span></span><span style="color: #008000"><span style="color: #008000">/**</span></span><span style="color: #008000"><span style="color: #008000">* After the time-consuming task in the Onhandleintent function is completed, the service will automatically destroy * call OnDestroy function *</span></span><span style="color: #008000"><span style="color: #008000">*/</span></span><span style="color: #000000"><span style="color: #000000">@Override</span></span><span style="color: #0000ff"><span style="color: #0000ff"></span> public</span> <span style="color: #0000ff"><span style="color: #0000ff">void</span></span><span style="color: #000000"><span style="color: #000000">OnDestroy () {</span></span><span style="color: #0000ff"><span style="color: #0000ff">Super</span></span><span style="color: #000000"><span style="color: #000000">. OnDestroy (); LOG.D (</span></span>"123456", "ondestroy is called"<span style="color: #000000"><span style="color: #000000">); }}</span></span></pre><pre><span style="color: #0000ff"><span style="color: #0000ff"><?</span></span><span style="color: #ff00ff"><span style="color: #ff00ff">XML version= "1.0" encoding= "utf-8"</span></span><span style="color: #0000ff"><span style="color: #0000ff">?></span></span><span style="color: #0000ff"><span style="color: #0000ff"><</span></span><span style="color: #800000"><span style="color: #800000">Manifest</span></span><span style="color: #ff0000"><span style="color: #ff0000">xmlns:android</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "http://schemas.android.com/apk/res/android"</span></span><span style="color: #ff0000"><span style="color: #ff0000"></span> package</span><span style="color: #0000ff"><span style="color: #0000ff">= "im.weiyuan.com.viewutils"</span></span><span style="color: #0000ff"><span style="color: #0000ff">></span></span> <span style="color: #0000ff"><span style="color: #0000ff"><</span></span><span style="color: #800000"><span style="color: #800000">Permission</span></span><span style="color: #ff0000"><span style="color: #ff0000">Android:name</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "com.weiyuan.sb"</span></span> <span style="color: #0000ff"><span style="color: #0000ff">/></span></span> <span style="color: #0000ff"><span style="color: #0000ff"><</span></span><span style="color: #800000"><span style="color: #800000">uses-permission</span></span><span style="color: #ff0000"><span style="color: #ff0000">Android:name</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "com.weiyuan.sb"</span></span> <span style="color: #0000ff"><span style="color: #0000ff">/></span></span> <span style="color: #0000ff"><span style="color: #0000ff"><</span></span><span style="color: #800000"><span style="color: #800000">Application</span></span><span style="color: #ff0000"><span style="color: #ff0000">Android:allowbackup</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "true"</span></span><span style="color: #ff0000"><span style="color: #ff0000">Android:icon</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "@mipmap/ic_launcher"</span></span><span style="color: #ff0000"><span style="color: #ff0000">Android:label</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "@string/app_name"</span></span><span style="color: #ff0000"><span style="color: #ff0000">Android:supportsrtl</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "true"</span></span><span style="color: #ff0000"><span style="color: #ff0000">Android:theme</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "@style/apptheme"</span></span><span style="color: #0000ff"><span style="color: #0000ff">></span></span> <span style="color: #0000ff"><span style="color: #0000ff"><</span></span><span style="color: #800000"><span style="color: #800000">Activity</span></span><span style="color: #ff0000"><span style="color: #ff0000">Android:name</span></span><span style="color: #0000ff"><span style="color: #0000ff">=". Mainactivity "</span></span><span style="color: #0000ff"><span style="color: #0000ff">></span></span> <span style="color: #0000ff"><span style="color: #0000ff"><</span></span><span style="color: #800000"><span style="color: #800000">Intent-filter</span></span><span style="color: #0000ff"><span style="color: #0000ff">></span></span> <span style="color: #0000ff"><span style="color: #0000ff"><</span></span><span style="color: #800000"><span style="color: #800000">Action</span></span><span style="color: #ff0000"><span style="color: #ff0000">Android:name</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "android.intent.action.MAIN"</span></span> <span style="color: #0000ff"><span style="color: #0000ff">/></span></span> <span style="color: #0000ff"><span style="color: #0000ff"><</span></span><span style="color: #800000"><span style="color: #800000">category</span></span><span style="color: #ff0000"><span style="color: #ff0000">Android:name</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "android.intent.category.LAUNCHER"</span></span> <span style="color: #0000ff"><span style="color: #0000ff">/></span></span> <span style="color: #0000ff"><span style="color: #0000ff"></</span></span><span style="color: #800000"><span style="color: #800000">Intent-filter</span></span><span style="color: #0000ff"><span style="color: #0000ff">></span></span> <span style="color: #0000ff"><span style="color: #0000ff"></</span></span><span style="color: #800000"><span style="color: #800000">Activity</span></span><span style="color: #0000ff"><span style="color: #0000ff">></span></span> <span style="color: #0000ff"><span style="color: #0000ff"><</span></span><span style="color: #800000"><span style="color: #800000">Service</span></span><span style="color: #ff0000"><span style="color: #ff0000">Android:name</span></span><span style="color: #0000ff"><span style="color: #0000ff">=". Myintentservice "</span></span><span style="color: #ff0000"><span style="color: #ff0000">android:exported</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "false"</span></span><span style="color: #0000ff"><span style="color: #0000ff">></</span></span><span style="color: #800000"><span style="color: #800000">Service</span></span><span style="color: #0000ff"><span style="color: #0000ff">></span></span> <span style="color: #0000ff"><span style="color: #0000ff"></</span></span><span style="color: #800000"><span style="color: #800000">Application</span></span><span style="color: #0000ff"><span style="color: #0000ff">></span></span><span style="color: #0000ff"><span style="color: #0000ff"></</span></span><span style="color: #800000"><span style="color: #800000">Manifest</span></span><span style="color: #0000ff"><span style="color: #0000ff">></span></span></pre><pre><pre>Intent Intent = new Intent (mainactivity.this,myintentservice.class);<br>StartService (intent);<br>Let's take a look at the output of the log print:</pre></pre><p><p>07-23 11:52:18.507 14993-15238/im.weiyuan.com.viewutils D/123456:onhandleintent is called<br>07-23 11:52:28.508 14993-15238/im.weiyuan.com.viewutils D/123456:onhandleintent is out<br>07-23 11:52:28.510 14993-14993/im.weiyuan.com.viewutils D/123456:ondestroy is called</p></p><p><p></p></p><p><p>Don't read the blog clearly:</p></p><p><p>http://blog.csdn.net/ryantang03/article/details/8146154/</p></p><p><p>Pretty Classic.</p></p><p><p></p></p><p><p>Usage scenarios for Intentservice in Android</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.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service