About the Service of one of the four Android Components

Source: Internet
Author: User

About the Service of one of the four Android Components

As one of the four main components of Android, Service plays an important role. The Service has the same level as the Activity, but has no interface. It is a Service running on the background. This running "background" is invisible, not in the background thread. In fact, all four components are running in the UI thread, they cannot perform time-consuming operations or network requests in their respective lifecycle methods. There are still many application scenarios for services, such as playing music in the background, updating changes to the current geographic location in real time, offline caching of content in the news APP background, and inter-process communication.

Services can be divided into Local Service and Remote Service. It is commonly used as a local service and is used within a program. You can start the Service through Context. startService () and end the Service through Context. stopService. You can also bind Context. bindService () to a service itself, or unbind the service through Context. unbindService. No matter how many times the startService is called, you only need one stopService to terminate the service.

Remote Service is used between applications in the android system. You can define an interface and expose it for other applications to perform operations. The client establishes a connection to the service object and calls the service through that connection. Call the Context. bindService () method to establish a connection and start it to call Context. unbindService () to close the connection. Multiple clients can be bound to the same service. If the service is not loaded yet, bindService () will first load it.

There are two ways to start a Service, which correspond to the two lifecycles of the Service. I will not repeat the Official Google image.

Since the start and stop methods are relatively simple to control services, we will not repeat them here. We just need to remind you that in this way, the communication between Activity and Service is not so straightforward and simple, because we cannot get the reference of Service. If you get the reference of the Service in the form of a Singleton, and then obtain the methods and attributes in the form of a Singleton, but the vigilance is serious, we do not recommend this method. BroadCastReceiver is another important component for better communication between activities and services. It will be used in the following instances.

In practice, a simple music player is used. Of course, bind and unbind are used. Bind is the binding Service, unbind is the unbinding Service, and the onDestroy method of the Service is called to destroy the Service. Bind can directly get the reference of the Service and directly control the service. Bind can be used for business processing in the current application or for inter-process communication through AIDL. This article first introduces the first one. A Brief Introduction to the functions of this Demo. It will play a song on the SD card of the mobile phone. You can start and pause the operation on it, the page displays the length of the song and the real-time position of the song. Implementation of interaction between the two: 1. Direct call of class reference 2. Broadcast Mechanism, Service sending, and Activity receiving.

1. Create an internal class MyBinder in MusicService, and a public method returns the Service instance.

2. Use the onBind method in MusicService to return the MyBinder instance for callback after the Activity is successfully bound.

3. Like Activity, you must register in manifest

4. Bind the Activity to obtain the callback Binder, and then obtain the MusicService instance.

5. After obtaining the reference of MusicService, you can operate the service to play the paused music.

6. In the onDestroy method of the Activity, remember to unbind the Service. Otherwise, it will be the same as the registered BroadCastReceiver, causing memory leakage.

Now, today's sharing of dry goods is here, and tomorrow we will provide you with the implementation of inter-process communication based on AIDL and Service.

Address: https://github.com/JackCho/ServiceDemo1

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.