Android Time consuming task _intentservice

Source: Internet
Author: User


If you see the Backservice class mentioned in the previous Android time-consuming task _handlerthread, then you must have an impulse to wrap it up for ease of use.
Don't worry, the Android SDK has already provided us with that Backservice basically the same class--intentservice. Because Intentservice and Backservice are basically the same, so the source I will not post.

Let's take a look at a simple example:
public class Asyncservice extends Intentservice {public asyncservice () {Super ("Asyncservice");} @Overrideprotected void Onhandleintent (Intent arg0) {}}

The use of intentservice is straightforward, but there are some things you need to pay attention to:
1, inheriting its class must have an argument-free constructor, and in this constructor call the parent class's parameter constructor, otherwise it will be an error.
2, Onhandleintent is an abstract method, must be implemented.
3, Intentservice has created a new thread, and the Looper-handler mechanism is opened in the thread, Onhandleintent is running in this space, so you can do the asynchronous task directly.
4, the Intentservice implementation class can be started several times, its life cycle and the normal life cycle is basically the same, and many other asynchronous tasks can be differentiated by the intent value, and multiple asynchronous tasks will be queued processing.
5, do not need to actively call STOPSELFT () to end the service. Because, after all asynchronous tasks are processed, the system shuts down the service automatically.
6, the same as the normal service needs to be registered in the manifest, start the car and the same as the normal service.

The life cycle of its multiple launches is as follows:
-->asyncservice
-->oncreate
-->onstartcommand
-->onstart
-->onhandleintent
-->onstartcommand
-->onstart
-->onhandleintent
-->ondestroy





Android Time consuming task _intentservice

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.