Overview of service in Android

Source: Internet
Author: User

Service is a very important component of Android, and generally has two purposes: to perform long-running operations with the service, and to interact with the user without a UI interface; The service of an application can be called by the components of another application to provide a broader use. To make your own service work, you must register the service via tags in androidmanifest.xml, similar to registering activity with tags. There are two ways to use the service, one is to start the service through the StartService method of the context class, and the other is to start the service through the Bindservice method of the context class.

It is important to note that, like other application components, service is run by default in the main thread of the host process in which it resides. In other words, if we are doing some CPU intensive operations (such as playing MP3) or blocking operations (such as a network request), the main thread will be stuck, and the ARN (application not responding) program is unresponsive, in order to solve this problem, We should create a new thread in the service and then put those time-consuming actions into the newly created thread to execute it, so that it does not block the main thread.

To understand what a service is, we first need to know what service is not:
A. First service is not another independent process. The service does not open a new process by itself unless specifically specified, by default the service is running in the process to which the application belongs.
B. The service is not another new thread. Service operations are performed in the main thread, and in order to avoid blocking the main thread, time-consuming operations should be put into a new thread in the service.

We know from the above description that the service is actually very simple (without initiating new processes and new threads), and the service provides the following two main features:
A. Run in the background as part of the application and do not even need to interact with the user. In this case, we generally invoke the StartService method of the context (such as invoking the Startserivce method in the activity) to start the service.
B. Exposed as a component of an application so that other applications can invoke the service. In this case, we generally call the context of the Bindservice method, such as we have two applications App1 and APP2, in APP1 we have a service called App1service, In APP2 we have a acitivity called app2activity, even if App1 does not start, we can App2 in app2activity by invoking Bindservice in App1, And by calling Bindservice, a connection is established between the service and the caller for inter-program interaction.

The following articles describe the use examples and lifecycles of StartService and Bindservice.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Overview of service 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.