android 四大元件之service學習總結(二)
1.Started Service 程式碼詳解
Started Service實現方法及步驟: 1、建立一個Started Service類必須繼承Service這個類。 2、在Started Service類中實現如下介面: onStartCommand(Intent intent, int flags, int startId)必須實現!在其中啟動服務提供的功能。例如,若該服務是在後臺下載檔案,則在該函式中啟動一個新的執行緒(Thread),線上程中實現下載功能。當客戶端通過startService()啟動函式時,系統會自動執行服務對應的onStartCommand()函式。
onBind(Intent intent)必須實現!返回null即可。onBind()是"Bound Service"中用到的函式,在"Started Service"服務不會執行onBind();但必須實現它,因為onBind()是Service類的抽象方法。
onCreate()可以不用實現,視使用者需求而定。當服務被建立時,系統會自動呼叫該函式。一般在該函式中進行初始化工作,例如:新建執行緒。
onDestroy()可以不用實現,視使用者需求而定。當服務被銷燬時,系統會自動呼叫該函式。一般在該函式中進行清除工作,例如,終止並回收執行緒。
3、Activity通過startService()啟動服務
4、Activity通過stopService()結束服務
例如:要實現一個activity通過按鈕啟動一個後臺數數服務,再通過按鈕結束該服務。
要想用後臺服務的方式執行上述的執行緒則在onStartCommand方法中來呼叫。private class ShuShu extends Thread { @Override public void run() { for (int i = 0; i < 10; i++) { try { Thread.sleep(1000); Log.i("doSomethingThread", i + ""); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
Log.d("service.onStartCommand()", "onStartCommand");
shuShu.start();
return START_STICKY;
}
結束的話在activity中呼叫stopService,他會執行service中的onDestory()
logshuchu如下:@Override public void onDestroy() { // TODO Auto-generated method stub Log.d("service.onDestroy()", "onDestroy"); if(shuShu!=null){ shuShu.interrupt(); } super.onDestroy(); }
這裡值得一提的是onStartCommand()方法返回的引數:
START_STICKY
如果service程序被kill掉,保留service的狀態為開始狀態,但不保留遞送的intent物件。隨後系統會嘗試重新建立service,由於服務狀態為開始狀態,所以建立服務後一定會呼叫onStartCommand(Intent,int,int)方法。如果在此期間沒有任何啟動命令被傳遞到service,那麼引數Intent將為null。
START_NOT_STICKY
“非粘性的”。使用這個返回值時,如果在執行完onStartCommand後,服務被異常kill掉,系統不會自動重啟該服務。
START_REDELIVER_INTENT
重傳Intent。使用這個返回值時,如果在執行完onStartCommand後,服務被異常kill掉,系統會自動重啟該服務,並將Intent的值傳入。
START_STICKY_COMPATIBILITY
START_STICKY的相容版本,但不保證服務被kill後一定能重啟。
2.Bound Service 程式碼詳解
這種方法常被用在執行程序的某個後臺操作或程序間通訊(IPC)。 Bound Service實現方法及步驟: 1、建立一個Bound Service類必須繼承Service這個類。2、 onCreate():可以不用實現,視使用者需求而定。當服務被建立時,系統會自動呼叫該函式。一般在該函式中進行初始化工作,例如:新建執行緒。
onDestroy():可以不用實現,視使用者需求而定。當服務被銷燬時,系統會自動呼叫該函式。一般在該函式中進行清除工作,例如,終止並回收執行緒。
onBind():必須實現!在onBind()中要返回IBinder物件。IBinder物件的作用是讓客戶端通過IBinder獲取該service的物件,從而呼叫服務提供相關的功能。Anroid傳遞資料的機制是基於IBinder的,我們不能直接傳遞service的物件。總之,我們需要記住的是onBind()中需要返回IBinder物件。下面說說IBinder物件怎麼獲取。
通常,我們通過在"Bound Service"中建立一個繼承於Binder的內部類。在該內部類中新增一個方法,比如getService(),返回“Bound Service”的物件。然後在onBind()中返回該內部類的物件即可。
public class LocalBinder extends Binder {
public MyService getService() {
// Return this instance of LocalService so clients can call public methods
return MyService.this;
}
}
private LocalBinder binder = new LocalBinder();
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return binder;
}
在客戶端中建立一個繼承於ServiceConnection的內部類;實現ServiceConnection中的兩個抽象函式:onServiceConnected() 和 onServiceDisconnected()。 onServiceConnected()在繫結服務成功時會被系統呼叫,在onServiceConnected()的輸入引數包含“onBinder()中返回的IBinder物件”,根據該物件,我們就能獲取到service的物件,之後就可呼叫service提供的服務。 onServiceDisconnected()在解除繫結時會被系統呼叫。
private ServiceConnection mConnection = new ServiceConnection() {
/**
* 繫結服務成功的回撥函式
*/
@Override
public void onServiceConnected(ComponentName className,
IBinder service) {
Log.d("MainActivity", "onServiceConnected");
// 獲取IBinder服務物件
LocalBinder binder = (LocalBinder) service;
mService = binder.getService();
mBound = true;
}
/**
* 解除繫結的回撥函式
*/
@Override
public void onServiceDisconnected(ComponentName arg0) {
Log.d("MainActivity", "onServiceDisconnected");
mBound = false;
}
};
在bindService()中傳入該內部類的物件即可。
在onServiceConnected()中已經獲取到service物件;現在,在我們客戶端的任何地方都可以呼叫到該service提供的服務了。
在Bound Service類中實現以下介面:
3、客戶端通過bindService()來繫結服務。bindService()中傳遞的引數包含一個ServiceConnection物件,下面說說怎麼獲取該物件。
4、 呼叫Bound Service提供的服務函式介面,以執行相關操作。
5、客戶端通過unbindService()結束服務。