android: 服務的基本用法

9.3   服務的基本用法android

 

瞭解了 Android 多線程編程的技術以後,下面就讓咱們進入到本章的正題,開始對服務 的相關內容進行學習。做爲 Android 四大組件之一,服務也少不了有不少很是重要的知識點, 那咱們天然要從最基本的用法開始學習了。編程

 

9.3.1    定義一個服務多線程

 

首先看一下如何在項目中定義一個服務。新建一個 ServiceTest 項目,而後在這個項目中 新增一個名爲 MyService 的類,並讓它繼承自 Service,完成後的代碼以下所示:app

 

public class MyService extends Service {ide

 

@Override佈局

public IBinder onBind(Intent intent) {學習

return null;測試

}this

 

}線程

目前 MyService 中能夠算是空空如也,但有一個 onBind()方法特別醒目。這個方法是 Service 中惟一的一個抽象方法,因此必需要在子類裏實現。咱們會在後面的小節中使用到 onBind()方法,目前能夠暫時將它忽略掉。

既然是定義一個服務,天然應該在服務中去處理一些事情了,那處理事情的邏輯應該寫 在哪裏呢?這時就能夠重寫 Service 中的另一些方法了,以下所示:

 

public class MyService extends Service {

 

@Override

public IBinder onBind(Intent intent) {

return null;

}

 

@Override

public void onCreate() {

super.onCreate();

}

 

@Override

public int onStartCommand(Intent intent, int flags, int startId) {

 

return super.onStartCommand(intent, flags, startId);

}

 

@Override

public void onDestroy() {

super.onDestroy();

}

 

}

能夠看到,這裏咱們又重寫了 onCreate()、onStartCommand()和 onDestroy()這三個方法, 它們是每一個服務中最經常使用到的三個方法了。其中 onCreate()方法會在服務建立的時候調用, onStartCommand()方法會在每次服務啓動的時候調用,onDestroy()方法會在服務銷燬的時候 調用。

一般狀況下,若是咱們但願服務一旦啓動就馬上去執行某個動做,就能夠將邏輯寫在 onStartCommand()方法裏。而當服務銷燬時,咱們又應該在 onDestroy()方法中去回收那些不 再使用的資源。

另外須要注意,每個服務都須要在 AndroidManifest.xml 文件中進行註冊才能生效,不知 道你有沒有發現,這是 Android 四大組件共有的特色。因而咱們還應該修AndroidManifest.xml 文件,代碼以下所示:

 

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.servicetest"

android:versionCode="1" android:versionName="1.0" >

……

<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" >

……

<service android:name=".MyService" >

</service>

</application>

</manifest>

這樣的話,就已經將一個服務徹底定義好了。

9.3.2    啓動和中止服務

 

定義好了服務以後,接下來就應該考慮如何去啓動以及中止這個服務。啓動和中止的方 法固然你也不會陌生,主要是藉助 Intent 來實現的,下面就讓咱們在 ServiceTest 項目中嘗試 去啓動以及中止 MyService 這個服務。

首先修改 activity_main.xml 中的代碼,以下所示:

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"

android:orientation="vertical" >

 

 

<Button android:id="@+id/start_service" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Start Service" />

 

<Button android:id="@+id/stop_service" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Stop Service" />

 

</LinearLayout>

這裏咱們在佈局文件中加入了兩個按鈕,分別是用於啓動服務和中止服務的。 而後修改 MainActivity 中的代碼,以下所示:

 

public class MainActivity extends Activity implements OnClickListener {

 

 

private Button startService;

 

 

private Button stopService;

 

 

@Override

protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);

startService = (Button) findViewById(R.id.start_service);

stopService = (Button) findViewById(R.id.stop_service);

 

 

 

startService.setOnClickListener(this);

stopService.setOnClickListener(this);

}

 

@Override

public void onClick(View v) {

switch (v.getId()) {

case R.id.start_service:

Intent startIntent = new Intent(this, MyService.class);

startService(startIntent); // 啓動服務

break;

case R.id.stop_service:

Intent stopIntent = new Intent(this, MyService.class);

stopService(stopIntent); // 中止服務

break;

default:

break;

}

}

 

}

能夠看到,這裏在 onCreate()方法中分別獲取到了 Start Service 按鈕和 Stop Service 按鈕 的實例,並給它們註冊了點擊事件。而後在 Start Service 按鈕的點擊事件裏,咱們構建出了 一個 Intent 對象,並調用 startService()方法來啓動 MyService 這個服務。在 Stop Serivce 按鈕 的點擊事件裏,咱們一樣構建出了一個 Intent 對象,並調用 stopService()方法來中止 MyService 這個服務。startService()和 stopService()方法都是定義在 Context 類中的,因此咱們在活動裏 能夠直接調用這兩個方法。注意,這裏徹底是由活動來決定服務什麼時候中止的,若是沒有點擊 Stop Service 按鈕,服務就會一直處於運行狀態。那服務有沒有什麼辦法讓自已中止下來呢? 固然能夠,只須要在 MyService 的任何一個位置調用 stopSelf()方法就能讓這個服務中止下 來了。

那麼接下來又有一個問題須要思考了,咱們如何才能證明服務已經成功啓動或者中止了 呢?最簡單的方法就是在 MyService 的幾個方法中加入打印日誌,以下所示:

 

public class MyService extends Service {

 

 

@Override

public IBinder onBind(Intent intent) {

return null;

 

 

 

}

 

 

@Override

public void onCreate() {

super.onCreate();

Log.d("MyService", "onCreate executed");

}

 

 

@Override

public int onStartCommand(Intent intent, int flags, int startId) {

Log.d("MyService", "onStartCommand executed");

return super.onStartCommand(intent, flags, startId);

}

 

 

@Override

public void onDestroy() {

super.onDestroy();

Log.d("MyService", "onDestroy executed");

}

 

 

}

如今能夠運行一下程序來進行測試了,程序的主界面如圖 9.5 所示。

圖   9.5

 

點擊一下 Start Service 按鈕,觀察 LogCat 中的打印日誌如圖 9.6 所示。

 

圖   9.6

 

MyService 中的 onCreate()和 onStartCommand()方法都執行了,說明這個服務確實已經啓 動成功了,而且你還能夠在正在運行的服務列表中找到它,如圖 9.7 所示。

 

圖   9.7

 

而後再點擊一下 Stop Service 按鈕,觀察 LogCat 中的打印日誌如圖 9.8 所示。

 

圖   9.8

 

由此證實,MyService 確實已經成功中止下來了。

話說回來,雖然咱們已經學會了啓動服務以及中止服務的方法,不知道你內心如今有沒有一個疑惑,那就是 onCreate()方法和 onStartCommand()到底有什麼區別呢?由於剛剛點擊

Start Service 按鈕後兩個方法都執行了。

其實 onCreate()方法是在服務第一次建立的時候調用的,而 onStartCommand()方法則在 每次啓動服務的時候都會調用,因爲剛纔咱們是第一次點擊 Start Service 按鈕,服務此時還 未建立過,因此兩個方法都會執行,以後若是你再連續多點擊幾回 Start Service 按鈕,你就 會發現只有 onStartCommand()方法能夠獲得執行了。

相關文章
相關標籤/搜索