Android four component service bound service

Source: Internet
Author: User

<title>Android four component service bound service</title> Date: December 28, 2015 17:10:45
1. Concept           The bound service is a client-server mode service. The bound service allows components (such as activity) to bind, send requests, receive responses, and even communicate interprocess (IPC). Bound services are generally only viable during the service of other application components and do not keep running in the background.
2. When creating a service that supports bindings, you must provide a ibinder that serves as a programming interface for communication between the client and the service. There are three ways to define this type of interface:Extending binder Classes If the service is private to your application and runs in the same process as the client (which is usually the case), you should create your interface by extending the Binder class and returning an instance of it from Onbind (). The client receives the binder object and uses it to access the binder and even the public methods available in the service.
If your service only performs some background work for your own application, this is the preferred technical solution. There is only one reason not to create an interface in this way, that is, the service is being used by another application or is being used across multiple processes.
using MessengerIf you need an interface to work across multiple processes, you can create an interface for the service through Messenger. In this way, the service defines a handler that responds to messages of various types of message objects. This handler is the basis for Messenger to share the same ibinder with the client, which allows the client to send instructions to the service using Message object messages. In addition, the client can define its own message so that the service can send messages back.
This is the easiest way to perform interprocess communication (IPC) because Messenger puts all the requests into a queue in a separate process, so you don't have to design the service as a thread-safe pattern.
using AidlThe Android Interface Definition language aidl (Android Interface definition Language) does all of the following: Resolves objects to primitive patterns recognized by the operating system and serializes them across processes (marshal) to complete the IPC. The previous way of using Messenger is actually based on Aidl, which uses aidl as the underlying structure. As mentioned above, Messenger creates a queue in a separate process that contains all the client requests, so that the service receives only one request at a time. However, if you want your service to be able to handle multiple requests at the same time, you can use Aidl directly. In this case, your service must have multithreading capabilities and are written in a thread-safe manner.
To use Aidl directly, you must create a. aidl file that defines the programming interface. The Android SDK tool uses this file to generate an abstract class that implements the interface and the processing of the IPC, and then you can extend the class in your own service.
Note:The vast majority of applications should not use AIDL to create bound services, as this may require multi-threading capabilities and make the code more complex. Therefore, Aidl does not apply to most applications, and this article does not discuss how to use its content in a service. If you are sure you need to use aidl directly, see the aidl documentation.

From for notes (Wiz)

Android four component service bound service

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.