本文部分来自android->developer->dev guide->Application Fundamentals
A Service is an application component representing either an application's desire to perform a longer-running operation while not interacting with the user or to supply functionality for other applications to use. Each service class must have a corresponding <service> declaration in its package's AndroidManifest.xml. Services can be started with Context.startService() and Context.bindService().
Using a service in two ways
Context.startService() and Context.bindService(),这两种方法实现不同的接口。
startService()生命周期调用的方法
void onCreate()
void onStart(Intent intent)
void onDestroy()
bindService()生命周期调用的方法
void onCreate()
IBinder onBind(Intent intent)
boolean onUnbind(Intent intent)
void onRebind(Intent intent)
void onDestroy()
- two nested loops of the service's lifecycle
By implementing these methods, you can monitor two nested loops of the service's lifecycle:
* The entire lifetime of a service happens between the time onCreate() is called and the time onDestroy() returns. Like an activity, a service does its initial setup in onCreate(), and releases all remaining resources in onDestroy(). For example, a music playback service could create the thread where the music will be played in onCreate(), and then stop the thread in onDestroy().
* The active lifetime of a service begins with a call to onStart(). This method is handed the Intent object that was passed to startService(). The music service would open the Intent to discover which music to play, and begin the playback.
There's no equivalent callback for when the service stops — no onStop() method.