四大组件-Service的生命周期

Service的生命周期包括startService和bindService两种启动方式,startService适用于后台任务,stopService停止服务;bindService用于交互,需unbindService释放。onCreate、onStartCommand、onDestroy是startService的主要回调,而onBind、onUnbind、onRebind适用于bindService。当Activity与Service绑定解除后,Service可能调用onRebind,需重写onUnbind返回true以允许再次绑定。Service的生命周期管理需考虑何时停止和解绑,以避免资源泄漏。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

【Service的生命周期】

0. Service 的生命周期及两种启动方法。

  • startService 启动服务: 用于启动一个服务执行后台任务,停止服务使用 stopService().

  • bindService 启动服务: 启动服务进行通信,停止服务使用 unbindService().

x

  • startService / stopService

      onCreate() -> onStartCommand() -> onDestroy()
      第一次调用会触发 onCreate() 和 onStartCommand,之后每次启动服务只触发 onStartCommand()
      无论 startService 多少次,stopService 一次就会停止服务
    
  • bindService/unbindService

      onCreate() -> onBind() -> onUnbind() -> onDestroy()
      第一次 bindService 会触发 onCreate() 和 onBind(),若不unBind() 则以后每次 bindService 都不会调用任何生命周期方法。
      bindService 启动的生命周期依附于启动它的 Context
    

0.1 Service中onRebind方法被调用,必须符合两个必要条件才行

  1. 服务中onUnBind方法返回值为true
  2. 服务对象被解绑后没有被销毁,之后再次被绑定

0.2 需要注意一个问题 :

当Activity退出的时候,Sercvice并不会停止,此时我们可以再进入Activity重新绑定,当这时候 Service就会调用onRebind()方法,但是调用onRebind()方法的前提是先前的onUnbind()方法执行成功,但是使用 super.onUnbind(intent)是执行不成功的,

这时候我们要手动的使其返回true,再次绑定时Rebind()就会执行。否则,如果退出时不显示的指定onUnbind()为成功的话(为false),那么重新启动此Activity来绑定服务时,Service的onBind()方法和onReBind都不会执行,但是ServiceConnection方法确一定会回调了。这说明在Service中的onBind()方法不同于 onStart()方法不能被重复调用。

源码如下:

  /**
     * Called when all clients have disconnected from a particular interface
     * published by the service.  The default implementation does nothing and
     * returns false.
     * 
     * @param intent The Intent that was used to bind to this service,
     * as given to {@link android.content.Context#bindService
     * Context.bindService}.  Note that any extras that were included with
     * the Intent at that point will <em>not</em> be seen here.
     * 
     * @return Return true if you would like to have the service's
     * {@link #onRebind} method later called when new clients bind to it.
     */
    public boolean onUnbind(Intent intent) {
        return false; //默认返回 FALSE ,因此如果想要 onRebind()可以被调用,则子类需要重写此方法。
    }

1. 根据开启方式的不同,分为两种生命周期

Service的生命周期,从它被创建开始,到它被销毁为止,可以有两条不同的路径:

  • onCreate();

    • 服务第一次被开启时调用(当第二次调用startService方法时则直接调用onStartCommand,而不是onCreate方法)
  • onBind(); //第一次调用 bindService(…)方法时,首先会调用onCreate然后调用onBind方法。代表服务已经绑定 —[bind方式开服务特有]

    • 调用bindService方法时,如果Service已经创建了(onCreate),则会直接调用 onBind进行绑定,而不是重新创建 :—混合方式
  • onStartCommand();

    • 或onStart();(已过时),服务开启时就会调用此方法。开启后可以在 设置>>应用管理>>running 中看到
  • onUnbind();

    • 每当有客户端与Service取消绑定时,就会调用此方法。
  • onDestroy() ;

    • 当服务被销毁时调用。(调用stopService()等方法后,或用户手动设置关停,或者所有客户端都与此Service解绑了)

2. 【开启服务有两种方式】:----【且两种开启方式分别对应着两种生命周期】

  • 方式一:[开启]context.startService();----[关闭] context.stopService(). 或stopSelf()

    特点:
    1.【startService:方式开启的服务就会在后台长期运行,直到调用stopService()等方法后,或用户手动设置关停 】

      2.【客户端不能与Service进行通信(即:客户端不能对Service进行控制)】
    
  • 方式二:[开启]context.bindService();-----[关闭] context.unbindService().或stopSelf()

    • 特点:
      1. 【利用bind发生开启的Service, 在客户端(即:组件(如:Activity))退出时必须 手动取消绑定(unbindService),否则会报错(泄漏)】
        -------(类似于动态注册的广播接收者:程序退出前必须取消注册,否则报错)

         因此建议在客户端(组件)的onDestroy方法中 取消绑定(unbindService) ,但若不取消绑定实际上也会销毁服务	
        
      2. 【客户端可以获得一个实现了IBinder接口的实例,用于与Service进行通信。(客户端调用Service里的方法)】

      3. 【一个service可以同时和多个客户绑定,当多个客户都解除绑定之后(unBindService()),才能调用销毁此service】

      4. 【通过bind方式开启的服务:不能在 设置>>应用管理>>服务看到,因此相当于一个隐形的服务 】

  • 【第一种开启方式的Service】-----startService

    1. 被开启的service通过其他组件调用 [startService()被创建]。

    2. 这种service可以无限地运行下去,必须调用stopSelf()方法或者其他组件调用stopService()方法来停止它。

    3. 当service被停止时,系统会销毁它。

  • 【第二种开启方式的Service】----bindService

    1. 被绑定的service是当其他组件(一个客户)调用[bindService()来创建的]。

2. 【客户端可以获得一个实现了IBinder接口的实例,用于与Service进行通信。(客户端调用Service里的方法,对Service进行控制)】

3. 客户可以通过 unbindService()方法来关闭这种连接。

4. 【一个service可以同时和多个客户绑定,当多个客户都解除绑定之后(unBindService())系统才会销毁此service】

  • 【也可以混合方式开启Service】

即是说:【startService方式开启的Service,还可以使用bindService进行绑定】

* 例如:一个后台音乐service可能因调用 startService()方法而被开启了,

		稍后可能用户想要控制播放器或者得到一些当前歌曲的信息,可以通过bindService()将一个activity和service绑定。

* 【注意】:这种情况下,[stopService()或 stopSelf()实际上并不能停止这个service,除非所有的客户都先解除绑定unBindService()]

	* 【因此:混合方式开启的Service,在需要关闭时必须 先解绑(unBindService)所有客户,后停止服务(stopService()或 stopSelf())】	

3.实现 生命周期回调方法(Implementing the lifecycle callbacks)

和activity一样,service也有一系列的生命周期回调函数,你可以实现它们来监测service状态的变化,并且在适当的时候执行适当的工作。

public class ExampleService extends Service{

    int mStartMode; // indicates how to behave if the service is killed
    IBinder mBinder; // interface for clients that bind
    boolean mAllowRebind; // indicates whether onRebind should be used

    @Override
    public void onCreate() {
        // The service is being created
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // The service is starting, due to a call to startService()
        return mStartMode;
    }

    @Override
    public IBinder onBind(Intent intent) {
        // A client is binding to the service with bindService()
        return mBinder;
    }

    @Override
    public boolean onUnbind(Intent intent){
        // All clients have unbound with unbindService()
        return mAllowRebind;
    }

    @Override
    public void onRebind(Intent intent) {
        // A client is binding to the service with bindService(),
        // after onUnbind() has already been called
    }

    /**
     * 强烈建议在Service的onDestroy()方法里去清理掉那些不再使用的资源,防止在Service被销毁后还会有一些不再使用的对象仍占用着内存
     */
    @Override
    public void onDestroy(){
        // The service is no longer used and is being destroyed
    }
}
  • 【不像是activity的生命周期回调函数,你可以不调用基类的实现。也可以调用】

——————————————————————————————————————————————————————————————————————————————————

4:Service的生命周期图:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-iqefyI1R-1677227783916)(Service的生命周期图.png)]

这个图说明了service典型的回调方法,尽管这个图中将开启的service和绑定的service分开,

【但是需要记住,任何service都潜在地允许绑定。所以,一个被开启的service仍然可能被绑定。

  • 实现这些方法,你可以看到两层嵌套的service的生命周期:-----( 整体的生命时间 嵌套 活动的生命时间 )

    The entire lifetime(【整体生命时间】)

    service整体的生命时间是从onCreate()被调用开始,到onDestroy()方法返回为止。

    和activity一样,service在onCreate()中进行它的初始化工作,在onDestroy()中释放残留的资源。

    比如,一个音乐播放service可以在onCreate()中创建播放音乐的线程,在onDestory()中停止这个线程。

    onCreate() 和 onDestroy()会被所有的service调用,不论service是通过startService()还是bindService()建立。

    The active lifetime (【活动生命时间】)

    service积极活动的生命时间(active lifetime)是从onStartCommand() 或onBind()被调用开始,
    它们各自处理由startService()或 bindService()方法传过来的Intent对象。

    如果service是被开启的startService(),那么它的活动生命周期和整个生命周期一同结束。

    如果service是被绑定的bindService(),它们它的活动生命周期是在onUnbind()方法返回后结束。

* 注意:尽管一个被开启的service是通过调用 stopSelf() 或 stopService()来停止的,没有一个对应的回调函数与之对应,即没有onStop()回调方法。

	* 所以,当调用了停止的方法,除非这个service和客户组件绑定,否则系统将会直接销毁它,onDestory()方法会被调用,并且是这个时候唯一会被调用的回调方法。

5.绑定形式开启的Service的 生命周期管理

当绑定service和所有客户端解除绑定之后,Android系统将会销毁它,(除非它同时被onStartCommand()方法开启)。

因此,如果你的service是一个纯粹的绑定service,那么你不需要管理它的生命周期。

然而,如果你选择实现onStartCommand()回调方法,那么你必须显式地停止service,因为service此时被看做是开启的。

这种情况下,service会一直运行到它自己调用 stopSelf()或另一个组件调用stopService(),不论它是否和客户端绑定。

另外,如果你的service被开启并且接受绑定,那么当系统调用你的 onUnbind()方法时,如果你想要在下次客户端绑定的时候接受一个onRebind()的调用(而不是调用 onBind()),你可以选择在 onUnbind()中返回true。

onRebind()的返回值为void,但是客户端仍然在它的 onServiceConnected()回调方法中得到 IBinder 对象。

————————————————————————————————————————————————————————————————————————————————————————

6. 【一个比较标准的Service就可以写成:】

	public class ExampleService extends Service{

		    IBinder mBinder; // interface for clients that bind

		    @Override
		    public void onCreate() {
		        // The service is being created
		    }

			@Override  
			public int onStartCommand(Intent intent, int flags, int startId) {  
			    new Thread(new Runnable() {  
			        @Override  
			        public void run() {  
			            // 开始执行后台任务  
			        }  
			    }).start();  
			    return super.onStartCommand(intent, flags, startId);  
			}  
			  
			class MyBinder extends Binder {  
			  
			    public void startDownload() {  
			        new Thread(new Runnable() {  
			            @Override  
			            public void run() {  
			                // 执行具体的下载任务  
			            }  
			        }).start();  
			    }  
			} 
    }	 		

附:

  • 服务可以多次bind,但是只能unbind一次。

  • 不能unbind未被bind的服务。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值