Service是一個應用程序組件android
Service沒有圖形化界面ide
Service一般用來處理一些耗時比較長的操做,能夠使用Service更新ContnetProvider,發送Intent以及啓動系統的通知等等。spa
Service不是一個單獨的進程,Service不是一個線程。線程
Service生命週期xml
Android Service的生命週期並不像Android中Activity那麼複雜,由於它只繼承了 onCreate(),onStart(),onDestroy()三個方法,當咱們第一次啓動Service時,前後調用了 onCreate(),onStart()這兩個方法,當中止Service時,則執行onDestroy()方法,這裏須要注意的是,若是 Service已經啓動了,當咱們再次啓動Service時,不會在執行onCreate()方法,而是直接執行onStart()方法。 對象
1.public IBinder onBind(Intent arg0) 將一個Service和一個Activity綁定繼承
2.public void onCreate() 建立一個Service對象時調用生命週期
3.public int onStartCommand(Intent intent, int flags, int startId) 當啓動或者從新啓動對象時調用進程
4.public void onDestroy() 銷燬一個Service對象時調用string
Service須要在AndroidManifest.xml註冊文件中註冊
1. Started Service中使用StartService()方法來進行方法的調用,調用者和服務之間沒有聯繫,即便調用者退出了,服務依然在進行 【onCreate()- >onStartCommand()->startService()->onDestroy()】,注意其中沒有 onStart(),主要是被onStartCommand()方法給取代了,onStart方法不推薦使用了。
2. BindService中使用bindService()方法來綁定服務,調用者和綁定者綁在一塊兒,調用者一旦退出服務也就終止了【onCreate()->onBind()->onUnbind()->onDestroy()】。