服務是Android中實現程序運行后台的解決方案,特別適合不用和用戶進行交互而又需要長期運行的任務。服務不依賴任何用戶界面。
1.創建Servrcie
1)創建一個Service只需要繼承Service類就行。需要重寫onCreate()和onBind()方法。
1 public class MyService extends Service{ 2 3 @Override 4 public void onCreate(){ 5 super.onCreate(); 6 } 7 8 @Override 9 public int onStartCommand(Intent intent,int flags,int startId){ 10 return super.onStartCommand(intent,flags,startId); 11 } 12 13 @Nullable 14 @Override 15 public Ibinder onBind(Intent intent){ 16 return null; 17 } 18 }
2)新創建的Service必須要在AndroidManifest.xml中進行注冊。需要在application添加service節點進行注冊。
<service android:name=".MyService"/>
當一個程序是通過startService啟動的,就會調用onStartCommand方法,因此這個方法可能在Service的生命周期內被執行多次。
Service是在應用程序的主線程中啟動的,這就意味着在onStartCommand中處理的程序邏輯都是在主線程中執行的。
3)onStartCommand方法執行完返回的值決定着Service被終止后,系統如何響應Service的重新啟動。
◊ START_STICKY 標准的重新啟動模式,一般是通過顯式調用startService和stopService來完成對Service的啟動和停止。當重新啟動時,傳入
onStartCommand中的intent為null
◊ START_NOT_STICKY 通常當Service命令執行完成后,Service就會調用stopSelf()終止自己,
◊ START_REDELIVER_INTENT 重新啟動將傳入之前的intent
4)可以通過onStartCommand中的falg參數找出Service的啟動方式。
◊ START_FLAG_REDELIVER 表示系統運行時在顯式調用stopSelf停止Service之前Service被終止
◊ FLAG_RETRY 表示Service在被異常終止后被重新啟動的
2.Service的啟動和停止(還可以通過bind方式啟動服務)
1)啟動Service
//顯式啟動Service Intent intent=new Intent(this,MyService.class); startService(intent); //隱式啟動Service,需要在Service節點內添加intent-filter節點,並設置action Intent intentA=new Intent(ACTION); startService(intentA);
2)停止Service
//顯式停止Servcie stopService(intent); //隱式停止Servcie stopService(intentA);
startService不能夠嵌套調用,因此不管startService被調用了多少次,一次stopService就會終止其所匹配的Servcie。由於Service具有較高的優先級,一
般不會被運行時終止,因此stopSelf可以明顯的改善應用程序中資源占用情況,Service完成操作和處理后,應當調用stopSelf.
3.Service和Activity進行綁定,分為以下幾步。
1)在Service中新建一個擴展自Binder的類,並返回該類的一個實例,擴展類中編寫供Activity調用的方法;
1 public class MyService extends Service{ 2 3 private Ibinder mIBinder= new DownloadBinder(); 4 @Override 5 public void onCreate(){ 6 super.onCreate(); 7 } 8 9 @Override 10 public int onStartCommand(Intent intent,int flags,int startId){ 11 return super.onStartCommand(intent,flags,startId); 12 } 13 @Nullable 14 @Override 15 public Ibinder onBind(Intent intent){ 16 return mIBinder; 17 } 18 19 class DownloadBinder extends Binder{ 20 //添加供Activity調用的方法 21 public void startDownload(){ 22 23 } 24 public int getProgress(){ 25 return0; 26 } 27 } 28 }
2)在Activity中新建一個ServiceConnection匿名類的實例,重寫onServiceConnected和onServiceDisconnect()方法
1 private ServiceConnection connection=new ServiceConnection(){ 2 @Override 3 public void onServiceConnected(ComponentName componentName,Ibinder iBinder){ 4 mDownloadBinder=(MyService.DownloadBinder)iBinder; 5 //Activity調用Service的方法 6 mDownloadBinder.startDownload(); 7 mDownloadBinder.getProgress(); 8 } 9 10 @Override 11 public void onServiceDisconnected(ComponentName componentName){ 12 13 } 14 };
3)利用bindService完成綁定
Intent intent=new Intent(this,MyService.class); //Service和Activity進行綁定 bindService(intent,connection,BIND_AUTO_CREATE); //Service和Activity取消綁定 unbindService(connection);
4.Service的生命周期
如果在項目中調用Context.startService(),相應服務就會啟動起來,若服務已創建,就回調onStartCommand()方法,若還未創建則onCreate()先執
行。服務啟動后會一直運行,直到stopSelf和stopService被調用。通過在項目中調用Context.bindService(),來獲取一個服務的持久連接,此時回調服務中
onBind方法。當項目中調用bindService有綁定到服務的客戶端,除非調用unBindService,否則stopService和stopSelf不會真正的停止服務。服務的生命周
期如下圖所示。
與 Activity 生命周期回調方法不同,你不需要調用這些回調方法的超類實現。即在override方法中不需要調用super方法。
5.前台Service
前台Service是被用戶知道的,並且在系統內存不足時不允許被系統kill掉的服務。前台Service必須在狀態欄提供一個通知,通知只有在前台被主動移除或者服務被終止才能被移除。
(1)啟動前台服務
通過Notification通知消息的構建,在Service的onStartCommand方法中使用startForeground方法讓Android服務運行在前台。
// 參數一:唯一的通知標識;參數二:通知消息。 startForeground(110, notification);// 開始前台服務
(2)停止前台服務
在Service的onDestroy中使用stopFroeground方法來停止正在運行的前台服務。
@Override public void onDestroy() { Log.d(TAG, "onDestroy()"); stopForeground(true);// 停止前台服務--參數:表示是否移除之前的通知 super.onDestroy(); }
6.IntentService的使用
為了可以簡單的創建一個異步的,會自動停止的服務。Android專門提供了一個IntentService類。IntentService會將所有收到的Intent放到隊列中,並
在異步后台線程中逐個的處理它們,當處理完每個Intent后,IntentService就會終止自己。