One of the four core components of Android -----Service (service)

One of the four core components of Android - the basic knowledge of Service (service)

Definition: A service is an application component that represents an application's desire to perform a longer-running operation without interacting with the user or providing functionality used by other applications.

  • Android defines two types of services :
    local services and remote services . Type please click this link


  • Two ways to
    start a service: Reference example link for two ways to start a service
    - using the startService() method and using the bindService() method

  • The difference between the two
  • (1) Use startService() to start the service
    1. The onCreate() and onStartCommand() callback methods of services started with the startService() method are called in turn.
    2. If the startService() method is called repeatedly after the Service is started, the onStartCommand() method will be called multiple times, that is, if the Service is not running, Android will first call the Service's onCreate() method, and then call onStartCommand() . OnStartCommand() will only be called if the Service is already running. Therefore, the onStartCommand() method of a Service may be called multiple times. You can encapsulate the onStartCommand() callback method of the data to be passed with Intent, and then obtain it in the Service.
    3. The function of using the stopService() method is to stop the service and call back the onDestroy() method in the Service class.
    4. The service started with the startService() method is not destroyed when the Activity exits, but is still executed in the memory. That is to say, the service started with the startService() method has only one instance in the memory, and does not follow the Activity. to exit.
  • (2) Use bindService() to start the service and bind the service
    1. A service started with the bindService() method will automatically call the onCreate() and onBind() methods in turn.
    2. When the running Service host object Activity object exits, the current Service is also destroyed, and the onUnbind() method and the onDestroy() method are automatically called.
    3. When the callback method onBind() of the Service returns null, the onServiceConected() method of the ServiceConnection object is not called. When the onBind method returns an IBinder object, the onServiceConected() method in the ServiceConection object is automatically called.
    4. The Intent object passed to the bindService() method is handled in the IBinder onBind(Intent argo) method, while the Intent object passed to the unbindService() method is handled in the onUnbind(Intent intent) method.

Note 1: For a service started with the startService() method, when the host program Activity object exits, the Service is not destroyed, that is, the life cycle of the Service started with the startService() method is not consistent with the host Activity. How to exit the Service, Use BroadcastReceiver to broadcast the receiver object in the Service, and then broadcast through the Activity object to tell the service to exit and destroy.
The Service started with the startService() method can only transmit data through the Intent object, and can only indirectly call the methods in the Service class through broadcast Brodcast

注意2:通过bindService方法启动是和startService()方法一样,都会调用onCreate()方法来创建Service,但它不会调用onStartCommand()方法而是调用onBind()返回客户端的一个IBinder接口,这个IBinder就是在Service的生命周期回调方法onBind()中的返回值,服务运行后,与前者不同的是,不是服务终止,而是使用Context.unbindService()方法之后,Service的生命周期回调onUnbind()会被调用,如果所有bind过Service的组件都调用unbindService()方法,那么Service会被停止,则会回调onDestroy()方法

Service服务的方法介绍

  • StartService()方法:启动服务,在内存中生成服务的实例对象。
  • stopService()方法:停止服务,用来销毁内存中的Service实例对象,如果内存中的Service对象曾经被bindService()方法关联绑定过,那么要想销毁内存中的Service,要先使用unbindService()反绑定服务。
  • bindService()方法:具有创建服务和绑定服务(与服务进行通信)的能力,使得与Service对象取得连接,进而进行数据上的通信,想要通信就得在Service的onBind()方法返回给客户端一个IBind接口实例,IBind接口实例,IBind接口允许客户端调用服务的方法,比如得到Service运行的状态或执行其他业务方法的操作。
  • unbindService方法:断开与Service的通信。
  • 使用bindService()方法启动服务的回调方法onRebind()的调用时机:是在客户端重新绑定Service时进行调用,需要注意的点是:有个限制就是反绑定Service时onUnbind()函数必须返回true.
  • ServiceConnection对象的onServiceDisconnected()方法调用时机:当Service服务被意外销毁时,例如内存的资源不足是这个方法才被自动调用。
  • onCreate()方法:当Service对象第一次被创建是,系统调用该方法。
  • onStartCommand(Intent intent,int flags,int startId)方法:当通过startService方法启动Service时,该方法被调用。
  • onDestroy()方法:当Service不在使用时,系统调用该方法。

  • 方法onStartCommand的返回值:方法onStartCommand的返回值为int类型,主要作用是当Service进程被意外kill掉是,Service服务下一步要做哪些行为,主要有3种值。

    1. START_STICKY: Service被意外终止时不调用onDestroy回调,并且终止后自动重启Service服务,只执行Service对象的onCreate()生命周期方法。
    2. START_NOT_STICKY:Service被意外终止时不调用onDestroy回调,并且不自动重启Service服务
    3. START_REDLIVER_INTENT:Service被意外终止时不调用onDestroy回调,并且终止后自动重启Service服务,还要执行Service对象的onCreate()和onStartCommand()生命周期方法,并且从Intent中能取到值。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324800720&siteId=291194637