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