Android time-consuming task _ IntentService, android time-consuming operation

Source: Internet
Author: User

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





Related Article

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.