【Android】Android Service的生命周期
发布日期:2021-06-29 20:55:00 浏览次数:2 分类:技术文章

本文共 2974 字,大约阅读时间需要 9 分钟。

Managing the Lifecycle of a Service

原文来自:  http://www.cnblogs.com/mengdd/archive/2013/03/24/2979944.html

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

A started service

  被开启的service通过其他组件调用 被创建。

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

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

 

A bound service

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

  客户可以通过一个接口和service进行通信。

  客户可以通过 方法来关闭这种连接。

  一个service可以同时和多个客户绑定,当多个客户都解除绑定之后,系统会销毁service。

 

  这两条路径并不是完全分开的。

  即是说,你可以和一个已经调用了 而被开启的service进行绑定。

  比如,一个后台音乐service可能因调用 方法而被开启了,稍后,可能用户想要控制播放器或者得到一些当前歌曲的信息,可以通过将一个activity和service绑定。这种情况下,或 实际上并不能停止这个service,除非所有的客户都解除绑定。

 

Implementing the lifecycle callbacks

  和activity一样,service也有一系列的生命周期回调函数,你可以实现它们来监测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    }    @Override    public void onDestroy()    {        // The service is no longer used and is being destroyed    }}

 

  不像是activity的生命周期回调函数,你不需要调用基类的实现。

 

  这个图说明了service典型的回调方法,尽管这个图中将开启的service和绑定的service分开,但是你需要记住,任何service都潜在地允许绑定。

  所以,一个被开启的service仍然可能被绑定。

  实现这些方法,你可以看到两层嵌套的service的生命周期:

 

The entire lifetime

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

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

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

    和 会被所有的service调用,不论service是通过还是建立。

 

The active lifetime

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

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

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

  注意:尽管一个被开启的service是通过调用  或 来停止的,没有一个对应的回调函数与之对应,即没有onStop()回调方法。所以,当调用了停止的方法,除非这个service和客户组件绑定,否则系统将会直接销毁它,onDestory()方法会被调用,并且是这个时候唯一会被调用的回调方法。

 

Managing the Lifecycle of a Bound Service

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

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

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

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

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

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

  下图展示了这种service(被开启,还允许绑定)的生命周期:

 

参考资料

  API Guides:Services

  

  API Guides:Bound Services

  

 

转载地址:https://dengjin.blog.csdn.net/article/details/41749649 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:【Android】 Android Service生命周期及用法
下一篇:【Android】ContentProvider和Uri详解

发表评论

最新留言

能坚持,总会有不一样的收获!
[***.219.124.196]2024年04月08日 12时12分40秒