閱讀目錄
一:Service是什麼
二:布局檔案編寫
三:代碼檔案編寫
四:項目定義檔案編寫
五:運行效果
一:Service是什嗎?
Service是Android系統的組件之一,和Activity,Intent,Conent Provider並稱Android四大天王,Service是不可見的,是沒有介面的,是在後台啟動並執行,Service一般處理比較耗時以及長時間啟動並執行操作。我以前給一個電子商務網站做過一個Windows服務,就是一直審核使用者下達的未審核的訂單,如果符合某種規範則這個訂單審核通過,這個服務是一直在啟動並執行,就跟這個電子商務網站一直線上上跑一樣,它能省掉很多人工的時間,所以說服務一般處理長時間工作的操作。
我之前在看一本書上說到“即便是通過Activity啟動的Service,也不會在相同的process進程當中運行,而是各自屬於不同的進程”,我翻閱了Android的SDK “A Service is not a separate process. The Service object itself does not imply it is running in its own process; unless otherwise specified, it runs in the same process as the application it is part of.”這句話的意思是說,Service不是一個獨立的進程,服務物件本身並不意味著這是運行在其自己的進程內,它與應用程式運行在同一個進程當中。當書本和Android SDK兩者的理論發生矛盾的時候,我們以Android SDK為準,所以我們認為Service和帶起它的應用程式在一個進程當中。因為我們知道了Service和帶起它的應用程式在一個進程當中,如果你的Service裡的操作阻塞住了,就會導致整個應用程式沒有響應,所以我們在Service裡面新開一個線程。
二:布局檔案編寫
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="fill_parent" 5 android:layout_height="fill_parent">
6
7 <Button android:layout_width="wrap_content" 8 android:layout_height="wrap_content" 9 android:text="啟動Service"10 android:id="@+id/start"/>11 <Button android:layout_width="wrap_content"12 android:layout_height="wrap_content"13 android:text="取消Service"14 android:id="@+id/end"/>15 </LinearLayout>
三:代碼檔案編寫
3.1 MainActivity.java
1 package com.menglin.service1; 2 3 import android.app.Activity; 4 import android.content.Intent; 5 import android.content.IntentFilter; 6 import android.os.Bundle; 7 import android.view.View; 8 import android.view.View.OnClickListener; 9 import android.widget.Button;10 11 public class MainActivity extends Activity12 {13 // 聲明兩個Button對象14 private Button btn_start, btn_end;15 16 @Override17 public void onCreate(Bundle savedInstanceState)18 {19 super.onCreate(savedInstanceState);20 //載入布局檔案main.xml21 setContentView(R.layout.main);22 //通過findViewById()方法得到兩個Button對象23 btn_start = (Button)findViewById(R.id.start);24 btn_end = (Button)findViewById(R.id.end);25 //給兩個按鈕綁定監聽單擊事件26 btn_start.setOnClickListener(btn_start_listener);27 btn_end.setOnClickListener(btn_end_listener);28 }29 30 //監聽單擊事件31 private OnClickListener btn_start_listener = new OnClickListener()32 {33 @Override34 public void onClick(View v)35 {36 //建立一個Intent對象37 Intent intent = new Intent();38 //第一個參數是自己的這個類的對象,第二個參數是要調用的Service的對象39 intent.setClass(MainActivity.this, Service1.class);40 //啟動服務41 startService(intent);42 }43 };44 45 //監聽單擊事件46 private OnClickListener btn_end_listener = new OnClickListener()47 {48 @Override49 public void onClick(View v)50 { 51 //建立一個Intent對象52 Intent intent = new Intent();53 //第一個參數是自己的這個類的對象,第二個參數是要調用的Service的對象54 intent.setClass(MainActivity.this, Service1.class);55 //關閉服務56 stopService(intent);57 }58 };59 }
3.2 Service1.java
1 package com.menglin.service1; 2 3 import android.app.Service; 4 import android.content.Intent; 5 import android.os.IBinder; 6 import android.util.Log; 7 8 public class Service1 extends Service 9 {10 11 private String TAG = "service";12 13 @Override14 public IBinder onBind(Intent intent)15 {16 // TODO Auto-generated method stub17 return null;18 }19 20 //當啟動Service的時候會調用這個方法21 @Override22 public void onCreate()23 {24 Log.i(TAG, "onCreate");25 super.onCreate();26 }27 28 //當系統被銷毀的時候會調用這個方法29 @Override30 public void onDestroy()31 {32 Log.i(TAG, "onDestroy");33 super.onDestroy();34 }35 36 //當啟動Service的時候會調用這個方法37 @Override38 public int onStartCommand(Intent intent, int flags, int startId)39 {40 Log.i(TAG, "onStartCommand");41 return super.onStartCommand(intent, flags, startId);42 }43 44 }
四:項目定義檔案編寫
AndroidMainfest.xml
為了讓Android的系統知道有這個我們自訂的服務,所以我們需要加上service節點<service android:name=".Service1"></service>。
五:運行效果
當我們單擊"啟動Service"按鈕後,發現系統輸出了"onCreate"我們要的資訊,我們再次點擊"啟動Service"按鈕發現沒有再次調用onCreate()方法,而是調用了onStartCommand()方法,一個服務啟動起來後,一直在後台運行,當你再想啟動這個服務的話,就不會再調用onCreate()方法了,因為這個服務已被建立了,而是直接調用onStartCommand()方法,一些主要的操作我們都是在onStartCommand()方法中實現的,就是我們在onStartCommand()方法中開啟新的線程,根據Activity傳進來的Inetent對象來完成一些操作,當我們不需要這個Service的時候,我們可以單擊"取消Service"按鈕,在這裡我們單擊"取消Service"按鈕,不僅會停止服務還會輸出"onDestroy"。