Android Service系列(十九)Bound service OverView

    xiaoxiao2023-12-06  179

    A bound service is the server in a client-server interface. It allows components (such as activities) to bind to the service, send requests, receive responses, and perform interprocess communication (IPC). A bound service typically lives only while it serves another application component and does not run in the background indefinitely.

    This document describes how to create a bound service, including how to bind to the service from other application components. For additional information about services in general, such as how to deliver notifications from a service and set the service to run in the foreground, refer to the Services document.

    翻译:主要讲了Bound service 是啥。(生命周期,跨进程)

     

    The basics

    A bound service is an implementation of the Service class that allows other applications to bind to it and interact with it. To provide binding for a service, you must implement the onBind() callback method. This method returns an IBinder object that defines the programming interface that clients can use to interact with the service.

    翻译:bound service 要实现onBind方法,这个方法会返回一个IBinder对象,这个对象定义了和客服端的程序接口

     

    Binding to a started service

    As discussed in the Services document, you can create a service that is both started and bound. That is, you can start a service by calling startService(), which allows the service to run indefinitely, and you can also allow a client to bind to the service by calling bindService().

    翻译: startService或者bindService两种方法

     

    If you do allow your service to be started and bound, then when the service has been started, the system does notdestroy the service when all clients unbind. Instead, you must explicitly stop the service by calling stopSelf() or stopService().

    翻译:如果service已经被started了,则即便all client 都unbind,也不销毁。要手动销毁

     

    Although you usually implement either onBind() or onStartCommand(), it's sometimes necessary to implement both. For example, a music player might find it useful to allow its service to run indefinitely and also provide binding. This way, an activity can start the service to play some music and the music continues to play even if the user leaves the application. Then, when the user returns to the application, the activity can bind to the service to regain control of playback.

    翻译:有时候,要同时实现onBind和onStartCommand

     

    For more information about the service lifecycle when adding binding to a started service, see Managing the lifecycle of a bound Service.

    参考:Managing the lifecycle of a bound Service.

     

    A client binds to a service by calling bindService(). When it does, it must provide an implementation of ServiceConnection, which monitors the connection with the service. The return value of bindService() indicates whether the requested service exists and whether the client is permitted access to it. When the Android system creates the connection between the client and service, it calls onServiceConnected() on the ServiceConnection. TheonServiceConnected() method includes an IBinder argument, which the client then uses to communicate with the bound service.

    翻译:bindService   ServiceConnection  onServiceConnected  iBinder

     

    You can connect multiple clients to a service simultaneously. However, the system caches the IBinder service communication channel. In other words, the system calls the service's onBind() method to generate the IBinder only when the first client binds. The system then delivers that same IBinder to all additional clients that bind to that same service, without calling onBind() again.

    翻译:可以多个客户端绑定一个service ,但是onBind只调用一次

     

    When the last client unbinds from the service, the system destroys the service, unless the service was also started by startService().

    翻译:当所有client unbind,则系统destroy the service,除非这个service是startService

     

    The most important part of your bound service implementation is defining the interface that your onBind() callback method returns. The following section discusses several different ways that you can define your service's IBinderinterface.

    翻译:最重要的部分是onBind方法返回的接口。 下面讨论几种实现方式。

     

     

     

     

     

    最新回复(0)