Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發 >> 進階開發

Android Service 詳解:生命週期、啟動與綁定的差別

編輯:進階開發

開放手機聯盟的成立和 Android 的推出是對現狀的重大改變,在帶來初步效益之前,還需要不小的耐心與高昂的投入。本文不談策略,專心把 Service 這個元件講透。

兩個必須先記住的重點

一、Service 預設與呼叫者在同一條執行緒。因此若要在 Service 中執行耗時操作,必須自行開新執行緒,否則會拖住主執行緒的 UI 反應。這一點和 Activity 一樣,是新手最常踩的坑。

二、Service 主要是實作 onCreate、onStart、onDestroy、onBind、onUnBind 這幾個函式。你要的功能就靠這幾個回呼串起來。

簡單的呼叫可以在呼叫者物件中使用 Context.startService 來啟動,以 Intent 作為參數。而 Intent 搜尋、匹配目標的方式,與 Activity 的匹配規則一致——靠 action、category 與 data 去比對所有已註冊的元件。

一個完整的呼叫端範例

  1. package test.pHello;
  2. import android.app.Activity;
  3. import android.content.ComponentName;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.content.ServiceConnection;
  7. import android.os.Bundle;
  8. import android.view.Menu;
  9. import android.view.MenuItem;
  10. public class HelloActivity extends Activity {
  11. ITestService mService = null;
  12. ServiceConnection sconnection = new ServiceConnection()
  13. {
  14. public void onServiceConnected(ComponentName name, IBinder service)
  15. {
  16. mService = (ITestService) service;
  17. if (mService != null) { mService.showName(); }
  18. }
  19. public void onServiceDisconnected(ComponentName name) { }
  20. };
  21. @Override
  22. public boolean onCreateOptionsMenu(Menu menu) {
  23. super.onCreateOptionsMenu(menu);
  24. menu.add(0, Menu.FIRST + 1, 1, "OpenActivity");
  25. menu.add(0, Menu.FIRST + 2, 2, "StartService");
  26. menu.add(0, Menu.FIRST + 3, 3, "StopService");
  27. menu.add(0, Menu.FIRST + 4, 4, "BindService");
  28. return true;
  29. }
  30. @Override
  31. public boolean onOptionsItemSelected(MenuItem item) {
  32. super.onOptionsItemSelected(item);
  33. switch (item.getItemId())
  34. {
  35. case Menu.FIRST + 1:
  36. {
  37. this.setTitle("Switch Activity");
  38. Intent i = new Intent();
  39. i.setAction("test_action");
  40. if (Tools.isIntentAvailable(this, i)) this.startActivity(i);
  41. else this.setTitle("the Intent is unavailable!!!");
  42. break;
  43. }
  44. case Menu.FIRST + 2:
  45. {
  46. this.setTitle("Start Service");
  47. Intent i = new Intent();
  48. i.setAction("start_service");
  49. this.startService(i);
  50. break;
  51. }
  52. case Menu.FIRST + 3:
  53. {
  54. this.setTitle("Stop Service");
  55. Intent i = new Intent(this, TestService.class);
  56. this.stopService(i);
  57. break;
  58. }
  59. case Menu.FIRST + 4:
  60. {
  61. this.setTitle("Bind Service!");
  62. Intent i = new Intent(this, TestService.class);
  63. this.bindService(i, this.sconnection, Context.BIND_AUTO_CREATE);
  64. break;
  65. }
  66. }
  67. return true;
  68. }
  69. @Override
  70. public void onCreate(Bundle savedInstanceState) {
  71. super.onCreate(savedInstanceState);
  72. this.setContentView(R.layout.main);
  73. }
  74. }

編譯執行後你會看到什麼

執行之後你會發現,是先執行 onCreate,然後才執行 onBind。而在呼叫者的 Context.bindService 返回時,ServiceConnection 的 onServiceConnected 並沒有馬上被執行。

這個「不同步」是很多人 debug 時困惑的來源。原因在於 bindService 只是提出綁定請求,真正的連線建立發生在 Service 完成 onCreate 並走到 onBind 之後,此時系統才回頭呼叫 onServiceConnected 把 Binder 交到你手上。所以任何依賴 mService 不為 null 的程式碼,都必須寫在 onServiceConnected 裡,而不能寫在 bindService 後面的那一行。

遠端綁定的不同之處

上述綁定是在呼叫者與 Service 位於同一個應用程式中的情況。如果兩者分處在不同的程式裡,呼叫方式就是另一種情況了——那時跨進程的 Binder 需要透過 AIDL 定義介面,資料也要走 Parcelable 序列化。這是後續文章的主題。

在此之前,先把三組 API 的語意分清楚:startService 是「拜託你做一件事」,stopService 是「你可以收工了」;bindService 是「我想時常跟你保持連線」,unbindService 是「連線可以斷了」。混用這兩組是 Service 生命週期管理出錯最常見的原因。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved