Comparison of service and Intentservice usage in Android

Source: Internet
Author: User

I do not know if you have the same as me, before the project or practice has been used to deal with the background time-consuming operation, but very little notice there is a intentservice, the previous period of time to prepare for the interview saw an explanation about Intentservice, Found that it is a lot more convenient than the service, here today a little to summarize my experience.

First of all, Intentservice is inherited from the service, then we look at the official introduction of the service, here is a list of two more important places:

1.A Service is A separate process. The Service object itself does not imply it was running in its own process; Unless otherwise specified, it runs in the same process as the application it was part of.

2.A Service is not A thread. It is a means itself to does work off of the main thread (to avoid application not responding errors).

  1.Service is not a separate process, it is in the same process as the application

  2.Service is not a thread, so we should avoid time-consuming operations within the service

It is important to stress that if there is a time-consuming operation in the service, a separate thread must be opened to handle it, which should be borne in mind.

Intentservice There are several useful advantages relative to service, let's start by looking at the official documentation:

Intentservice is a base class for Services this handle asynchronous requests (expressed as Intents) on demand. Clients send requests through StartService (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.

This "Work Queue processor" pattern was commonly used to offload the tasks from a application ' s main thread. The Intentservice class exists to simplify this pattern and take care of the mechanics. To use it, extend Intentservice and implement Onhandleintent (Intent). Intentservice would receive the Intents, launch a worker thread, and stop the service as appropriate.

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.

The translation comes in the form of a queue that joins the requested intent to the queue and then opens a worker thread to handle the intent in the queue, and for asynchronous StartService requests, Intentservice will process the completion of a second after processing, each request will be processed in a separate worker thread, will not block the application's main thread , here gives us a thought, It is better to use Intentservice to handle time-consuming operations if there is a time-consuming operation than opening a new thread within the service.

  because the service process takes precedence over the background process, it is a good idea to start a service if the activity requires a time-consuming operation. Of course, it is also possible to start a child thread in the activity to complete the time-consuming operation, but the disadvantage of doing so is that Once the activity is no longer visible, the activity's process becomes a background process, and the background process is likely to be killed by the system at any time when there is not enough memory (but the time-consuming operation of starting the service completes the problem of data interaction. For example, the service is not a good choice when the time-consuming operation needs to update the state of the UI control in real time. based on the same considerations, the time-consuming operation should not be performed in Broadcastreceiver, but the service should be started (of course, the Broadcastreceiver life cycle is too short and the time-consuming operation cannot be performed).

Summary: Intentservice is a service that can handle asynchronous requests through Context.startservice (Intent), and when used you only need to inherit Intentservice and override the Onhandleintent The (Intent) method receives a Intent object and stops itself at the appropriate time (usually when the job is completed). All requests are processed in one worker thread, alternating (but not blocking the execution of the main thread), and only one request can be executed at a time.

This is a message-based service, and each time you start the service instead of processing your work right away, you will first create the corresponding Looper,handler and add a Message object with the customer intent in the MessageQueue, When Looper finds a message, it then gets the Intent object by calling your handler in Onhandleintent ((Intent) msg.obj) and stops its service after processing. This means that the life cycle of intent is consistent with the task you are dealing with, so this class is very good with the download task, and the service itself ends the exit after the download task finishes.

The characteristics of summary intentservice are:

(1) A separate worker thread is created to handle all intent requests;

(2) A separate worker thread is created to handle the code implemented by the Onhandleintent () method, eliminating the need to deal with multithreading issues;

(3) After all request processing is complete, Intentservice will stop automatically without calling the Stopself () method to stop the service

Comparison of service and Intentservice usage in Android

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.