Service的生命周期

*Service在后台运行,处理需要在后台运行的事件。

*Service的生命周期,只调用三个方法:onCreate(),onStart()onDestroy()

*启动一个Service:context.startService() -->onCreate()-->onStart()

*停止一个Service:context.stopService() -->onDestroy(),如果调用者直接退出而没有调用stopService,则会一直在后台运行。

*bindService后,Service就和调用bindService的进程同生共死了,就是说:当调用bindService的进程死了,那么它bindService也要跟着被结束。 

  期间也可以调用unBindService()结束Service。

*调用startServiceonCreate() --> onStart()(可多次调用) --> onDestroy()

*调用bindServiceonCreate() --> onBind()(一次,不可多次绑定) --> onUnbind() --> onDestory()

*在Service的一个生命周期过程中,可以看到,只有onStart()的方法可以被多次调用,onCreate()onBind()onUnbind()onDestory()在一个生命周期中只能被调用一次。

posted @ 2011-03-21 13:21  PengG  Views(427)  Comments(0)    收藏  举报