Service的生命周期

service生命周期为context.startService() ->onCreate()- >onStartCommand()->Service running ->context.stopService()  ->onDestroy() ->Service stop 

service事实上是存在onStart()方法的,但是并不好用。
如果Service还没有运行,则android先调用onCreate()然后调用onStartCommand(),及onStartCommand()在每次调用service时都会执行;
如果Service已经运行,则只调用onStartCommand()。 

stopService的时候直接onDestroy,
如果是调用者自己直接退出而没有调用stopService的话,Service会一直在后台运行。
该Service的调用者再启动起来后可以通过stopService关闭Service。
Service自己stop调用Service.this.stopSelf(),在非自身Service里面关闭Service调用context.stopService(intent)。

posted @ 2013-02-20 21:09  茉莉花茶  阅读(353)  评论(0编辑  收藏  举报