對于需要長期運作,例如播放音樂、長期和伺服器的連接配接,即使已不是螢幕目前的activity仍需要運作的情況,采用服務方式。服務将通過API觸發啟動或者通過IPC(Interprocess Communication)連接配接請求觸發啟動。服務将一直運作直至被關閉,或者記憶體不足時由系統關閉。一般而言,為了節省電量,服務應進行優化減少CPU的消耗和大量網絡通信。服務可用于以下的場景:
1、使用者離開activity後,仍需繼續工作,例如從網絡下載下傳檔案,播放音樂
2、無論activity出現(重制)或離開,都需持續工作,例如網絡聊天應用
3、連接配接網絡服務,正在使用一個遠端API提供的服務
4、定時觸發的服務,例如Linux中的cron。
在manifest中聲明服務
和activity、content provider一樣,服務也必須要在AndroidManifest檔案中進行聲明是<application>中的子節點。例如我們下面第一個service的例子ServiceDownloader。
<application … … >
... ...
<service android:name=".ServiceDownloader">
</application >
指令模式:IntentService
編寫自己的Service将繼承Android的Service類,或者Service的子類IntentService。觸發Service的方式有兩種,一種是發送指令,即這次學習的指令模式,一種綁定服務,與服務之間建立雙向的通信管道。指令模式例子為http遠端下載下傳檔案的服務。
服務ServiceDownloader
/* 指令模式的服務由client請求服務,服務進行處理,并在完成後關閉服務,client無需關心是否需要結束服務,适合一次性的處理,如本例 */

public class ServiceDownloader extends IntentService{
private HttpClient client = null;
public ServiceDownloader(){
super("ServiceDownloader");
}
//client通過startService()請求服務時,如果服務沒有開啟,則首先執行onCreate(),我們在此進行服務的初始化工作,請注意,onCreate()是在主線程中運作。
public void onCreate() {
super.onCreate();
client = new DefaultHttpClient();
}
//如果client發出startService()時,如果服務沒有開啟,則先開啟服務onCreate(),在服務開啟後或者如果服務已經開啟,将觸發onStartCommand(),請注意,這也是在主線程中運作,我們不應用将一些時間長的處理放置此處。一般而言,這裡可以根據收到的指令,進行本次服務的初始化處理。原則上,由于是主線程,可進行UI操作,但是好的程式設計風格,service不處理activity的内容。
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
//這是必須override的方法,在收到用戶端指令,處理完onStartCommand()後執行,注意onHandlerIntent是在背景線程中運作,應将主要的處理内容放置此處
protected void onHandleIntent(Intent i) {
/*HTTP的例子之前學習過,首先是采用GET的方法擷取遠端檔案。将傳回的HTTP存放在responseHandler中,我們寫了個私類ByteArrayResponseHandler來處理,檢查HTTP的傳回值,如果不是200 OK,例如3xx-6xx,則說明出現異常,如成功,将擷取的内容存放至檔案中。*/
HttpGet getMethod = new HttpGet(i.getData().toString());
try{
ResponseHandler<byte[]> responseHandler = new ByteArrayResponseHandler();
byte[] responseBody = client.execute(getMethod,responseHandler);
File output = new File(Environment.getExternalStorageDirectory(),
i.getData().getLastPathSegment());
if(output.exists()){
output.delete();
}
FileOutputStream fos = new FileOutputStream(output.getPath());
fos.write(responseBody);
fos.close();
}catch(Exception e){
Log.e(getClass().getName(),"Exception : " + e.toString());
}
//如果client發出stopService()請求停止服務,或者服務本身通過stopSelf()要求停止服務,都會觸發onDestroy(),onDestroy也是在主線程中運作,在此我們應進行停止服務的工作。如果這是正在主線程執行onStartCommand(),則必須要等onStartCommand()的内容執行完,才依次執行onDestroy()的内容。如果這時背景線程onHandleIntent( )正在執行,onDestroy(
)不會自動将背景線程停止,背景線程繼續運作,我們必須在onDestroy()的代碼中終結背景線程的運作。例如狀态檢查,或者本地中直接關閉連接配接,中斷通信
public void onDestroy() {
client.getConnectionManager().shutdown();
super.onDestroy();
//檢查傳回HTTP Response的傳回值,如果是3xx-6xx,不是2xx,則說明出錯,例如404,Not Found。
private class ByteArrayResponseHandler implements ResponseHandler<byte[]>{
public byte[] handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() >= 300){
throw new HttpResponseException (statusLine.getStatusCode(),statusLine.getReasonPhrase());
HttpEntity entity = response.getEntity();
if(entity == null)
return null;
return EntityUtils.toByteArray(entity);
}
指令模式服務的用戶端
/*用戶端采用指令方式觸發服務,由于IntentService在執行完後自動關閉,則隻需通過startService( )指令觸發即可 */
public class ServiceTest1 extends Activity{
… …
//調起服務和調起Activity非常相似,都是通過Intent來出傳遞,通過setData傳遞參數,在本例是直接http的Uri位址。
private void startDownloader(){
Intent intent = new Intent(this,ServiceDownloader.class);
intent.setData(Uri.parse("http://commonsware.com/Android/excerpt.pdf"));
startService(intent);
//一般而言指令模式的服務,不需要考慮終止服務。此處隻做試驗用。注意,終止服務是終止整個服務,會觸發服務中的onDestroy( ),如果隊列中還有其他指令等等服務處理,将由onDestroy()中的代碼停止。是以影響的是所有正在和等待服務處理,而不單是用戶端的請求,此需特别注意!!
private void stopDownloader(){
stopService(new Intent(this,ServiceDownloader.class);
服務和用戶端的通信
用戶端代碼如下
……
intent = new Intent(this,ServiceDownloader.class);
intent.setData(Uri.parse("http://commonsware.com/Android/excerpt.pdf"));
//activity在調起服務時,即startService()或者bindService()都可以攜帶Messenger作為Intent的extra傳遞,這樣在服務和client之間可通過Messenger傳遞
intent.putExtra(ServiceDownloader.EXTRA_MESSAGER, new Messenger(handler));
startService(intent);
//Handler通過handlerMessage()接受消息,運作在主線程,用于處理UI等内容。
private Handler handler = new Handler(){
public void handleMessage(Message msg) {
super.handleMessage(msg);
buttonStart.setEnabled(true);
buttonStop.setEnabled(false);
switch(msg.arg1){
case Activity.RESULT_OK:
Toast.makeText(ServiceTest1.this, "Result : OK " , Toast.LENGTH_LONG).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(ServiceTest1.this, "Result : Cancel " , Toast.LENGTH_LONG).show();
default:
};
服務端代碼如下:
//避免出現命名重複,将類的命名空間加在前面
public static final String EXTRA_MESSAGER="com.wei.android.learning.ServiceDownloader.EXTRA_MESSAGER";
protected void onHandleIntent(Intent i) {
int result = Activity.RESULT_CANCELED
//下載下傳檔案的處理,成功則,設定result = Activity.RESULT_OK;
… …
//步驟1:從Intent的Extras中擷取Messenger
Bundle extras = i.getExtras();
if(extras != null){
Messenger mesenger = (Messenger)extras.get(EXTRA_MESSAGER);
//步驟2:使用Message.obtain()獲得一個空的Message對象
Message msg = Message.obtain( );
//步驟3:填充message的資訊。
msg.arg1 = result;
//步驟4:通過Messenger信使将消息發送出去。
try{
mesenger.send(msg);
}catch(Exception e){
Log.w(getClass().getName(),"Exception Message: " + e.toString());
}
}