Android time-consuming task _ IntentService, android time-consuming operation
If you see the BackService class mentioned in the previous android time-consuming task _ HandlerThread, you must have an impulse to encapsulate it for ease of use.
Don't worry. The Android SDK has already provided us with the IntentService class that is basically the same as that of the BackService. Because IntentService and BackService are basically the same, I will not post the source code.
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 simple and clear, but you need to pay attention to some things:
1. The class that inherits it must have a constructor without parameters, and call the constructor with parameters of the parent class in this constructor. Otherwise, an error is reported.
2. onHandleIntent is an abstract method that must be implemented.
3. IntentService has created a new thread and enabled the looper-handler mechanism in the thread. onHandleIntent is running in this thread space, so you can directly perform asynchronous tasks.
4. The IntentService implementation class can be started multiple times. its lifecycle is basically the same as that of a normal one. In addition, multiple asynchronous tasks can be distinguished by intent values, multiple asynchronous tasks are queued for processing.
5. You do not need to call stopSelft () to terminate the service. Because after all asynchronous tasks are processed, the system automatically closes the service.
6. Like a common service, you need to register it in manifest, and start the vehicle as well as the normal service.
Its lifecycle is as follows:
--> AsyncService
--> OnCreate
--> OnStartCommand
--> OnStart
--> OnHandleIntent
--> OnStartCommand
--> OnStart
--> OnHandleIntent
--> OnDestroy