天天看點

本地Service

通過在content(如activity)中調用startService(Intent )啟動service,service隻有在調用stopSelf(),或者另外component調用stopService()時才會停止。

service啟動時會自動調用

Service類:

This is the base class for all services. When you extend this class, it's important that you create a new thread in which to do all the service's work, because the service uses your application's main thread, by default, which could slow the performance of any activity your application is running

IntentService類:

This is a subclass of <code>Service</code> that uses a worker thread to handle all start requests, one at a time. This is the best option if you don't require that your service handle multiple requests simultaneously. All you need to do is implement <code>onHandleIntent()</code>, which receives the intent for each start request so you can do the background work.

IntentService類做如下工作:

Creates a default worker thread that executes all intents delivered to <code>onStartCommand()</code> separate from your application's main thread.

Creates a work queue that passes one intent at a time to your <code>onHandleIntent()</code> implementation, so you never have to worry about multi-threading.

Stops the service after all start requests have been handled, so you never have to call <code>stopSelf()</code>.

Provides default implementation of <code>onBind()</code> that returns null.

Provides a default implementation of <code>onStartCommand()</code> that sends the intent to the work queue and then to your <code>onHandleIntent()</code> implementation

是以,隻需實作onHandleIntent()函數,和構造函數,不比考慮多線程的問題

當重載其他回調函數時,記住super

繼續閱讀