Introduction to Android Service Components

Source: Internet
Author: User

Service is one of the four major components provided by the Android system. Its position is parallel with that of activity, but it is not frequently used by activity. As the name suggests, a service is a kind of service program running in the background. Generally, it rarely interacts with users. The biggest feature is that there is no visual interface.

To define a service class, you only need to inherit the service class and implement the methods in its lifecycle. A defined service must be in androidmainifest. in the xml configuration file, you can bind an existing service through <service> or bindservice (). You can also call services between different processes through RPC (Remote process call.

What is a service?

1. Service is an application component;

2. The Service does not have a graphical interface;

3. Services generally process some time-consuming operations;

4. You can use the service to update the content provider, send intent notifications, and start the system.

What is service?

1. The service is not a separate process;

2. The service is not a thread.

Start and Stop a service

Once a service is defined, you can start the service in other components to use it. Start a service using the context. startservice (intent) method. This method is similar to starting an activity and also transmits an intent. When we call the startservice () method, the called service will call its oncreate () method (if the service has not been created), and then call the onstart () method. Once the service is started, it will always run until context. stopservice () or stopself () is called ().

Bind an existing service

You can call startservice () to start a service method, or bindservice () to bind a service. Like calling the startservice () method, the service calls oncreate () to create a service (if not created), but calls onbind () instead of onstart () returns an ibinder interface of the client. Binding a service is generally used for remote service calls.

Binding a service requires three parameters: bindservice (intent, Conn, service. bind_auto_create); the first is intent; the second is serviceconnection object. We create this object to implement onserviceconnected () and onservicedisconnected () to determine whether the connection is successful or disconnected; the third parameter is how to create a service. Generally, it is automatically created when binding.

Remote service call

On the Android platform, each component runs in its own process. They cannot access each other, but some objects must be inevitably transmitted between programs to communicate with each other. To achieve mutual communication between processes, Android uses a lightweight implementation method RPC (Remote Procedure Call, remote process call) to complete inter-process communication, android uses the Interface Definition Language (Android Interface)
Definition Language, aidl) to generate code for mutual access between two processes. For example, if your code in activity needs to access a method in service, you can implement it in this way. The aidl RPC mechanism is implemented through interfaces, similar to the com or CORBA in windows, but it is lightweight, and the client and called implementation are implemented through the proxy mode. The proxy class and the proxy class implement the same interface, that is, the ibinder mentioned above.

 

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.