android關鍵組件service服務(一)

來源:互聯網
上載者:User

android關鍵組件service服務(一)
一、 Service簡介

Service是android 系統中的四大組件之一(Activity、Service、BroadcastReceiver、ContentProvider),它跟Activity的層級差不多,但不能自己運行只能後台運行,並且可以和其他組件進行互動。service可以在很多場合的應用中使用,比如播放多媒體的時候使用者啟動了其他Activity這個時候程式要在後台繼續播放,比如檢測SD卡上檔案的變化,再或者在後台記錄你地理資訊位置的改變等等,總之服務總是藏在背景。

Service的啟動有兩種方式:context.startService() context.bindService()

二、 Service啟動流程

context.startService() 啟動流程:

context.startService() -> onCreate() -> onStart() -> Service running -> context.stopService() -> onDestroy() -> Service stop

startService(new Intent(LoginActivity.this, XXService.class));

如果Service還沒有運行,則android先調用onCreate(),然後調用onStart();

如果Service已經運行,則只調用onStart(),所以一個Service的onStart方法可能會重複調用多次。

如果stopService的時候會直接onDestroy,如果是調用者自己直接退出而沒有調用stopService的話,Service會一直在後台運行,該Service的調用者再啟動起來後可以通過stopService關閉Service。

所以調用startService的生命週期為:onCreate --> onStart (可多次調用) --> onDestroy


context.bindService()啟動流程:

context.bindService() -> onCreate() -> onBind() -> Service running -> onUnbind() -> onDestroy() -> Service stop
在Service每一次的開啟關閉過程中,只有onStart可被多次調用(通過多次startService調用),其他onCreate,onBind,onUnbind,onDestory在一個生命週期中只能被調用一次。

onBind()將返回給用戶端一個IBind介面執行個體,IBind允許用戶端回調服務的方法,比如得到Service的執行個體、運行狀態或其他動作。這個時候把調用者(Context,例如Activity)會和Service綁定在一起,Context退出了,Srevice就會調用onUnbind->onDestroy相應退出。

所以調用bindService的生命週期為:onCreate --> onBind(只一次,不可多次綁定) --> onUnbind --> onDestory。

三、 Service生命週期

Service的生命週期並不像Activity那麼複雜,它只繼承了onCreate()、onStart()、onDestroy()三個方法

當我們第一次啟動Service時,先後調用了onCreate()、onStart()這兩個方法;當停止Service時,則執行onDestroy()方法。

這裡需要注意的是,如果Service已經啟動了,當我們再次啟動Service時,不會在執行onCreate()方法,而是直接執行onStart()方法。

它可以通過Service.stopSelf()方法或者Service.stopSelfResult()方法來停止自己,只要調用一次stopService()方法便可以停止服務,無論調用了多少次的啟動服務方法。



四、 Service樣本

下面我做了一個簡單的音樂播放的應用,分別使用startService和bindService來啟動本地的服務。

Activity

[java] view plaincopyprint?
  1. public class PlayMusicService extends Activity implements OnClickListener {
  2. private Button playBtn;
  3. private Button stopBtn;
  4. private Button pauseBtn;
  5. private Button exitBtn;
  6. private Button closeBtn;
  7. private Intent intent;
  8. @Override
  9. public void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.music_service);
  12. playBtn = (Button) findViewById(R.id.play);
  13. stopBtn = (Button) findViewById(R.id.stop);
  14. pauseBtn = (Button) findViewById(R.id.pause);
  15. exitBtn = (Button) findViewById(R.id.exit);
  16. closeBtn = (Button) findViewById(R.id.close);
  17. playBtn.setOnClickListener(this);
  18. stopBtn.setOnClickListener(this);
  19. pauseBtn.setOnClickListener(this);
  20. exitBtn.setOnClickListener(this);
  21. closeBtn.setOnClickListener(this);
  22. }
  23. @Override
  24. public void onClick(View v) {
  25. int op = -1;
  26. intent = new Intent("com.homer.service.musicService");
  27. switch (v.getId()) {
  28. case R.id.play: // play music
  29. op = 1;
  30. break;
  31. case R.id.stop: // stop music
  32. op = 2;
  33. break;
  34. case R.id.pause: // pause music
  35. op = 3;
  36. break;
  37. case R.id.close: // close activity
  38. this.finish();
  39. break;
  40. case R.id.exit: // stopService
  41. op = 4;
  42. stopService(intent);
  43. this.finish();
  44. break;
  45. }
  46. Bundle bundle = new Bundle();
  47. bundle.putInt("op", op);
  48. intent.putExtras(bundle);
  49. startService(intent); // startService
  50. }
  51. @Override
  52. public void onDestroy(){
  53. super.onDestroy();
  54. if(intent != null){
  55. stopService(intent);
  56. }
  57. }
  58. }


    Service

    [java] view plaincopyprint?
    1. public class MusicService extends Service {
    2. private static final String TAG = "MyService";
    3. private MediaPlayer mediaPlayer;
    4. @Override
    5. public IBinder onBind(Intent arg0) {
    6. return null;
    7. }
    8. @Override
    9. public void onCreate() {
    10. Log.v(TAG, "onCreate");
    11. Toast.makeText(this, "show media player", Toast.LENGTH_SHORT).show();
    12. if (mediaPlayer == null) {
    13. mediaPlayer = MediaPlayer.create(this, R.raw.tmp);
    14. mediaPlayer.setLooping(false);
    15. }
    16. }
    17. @Override
    18. public void onDestroy() {
    19. Log.v(TAG, "onDestroy");
    20. Toast.makeText(this, "stop media player", Toast.LENGTH_SHORT);
    21. if (mediaPlayer != null) {
    22. mediaPlayer.stop();
    23. mediaPlayer.release();
    24. }
    25. }
    26. @Override
    27. public void onStart(Intent intent, int startId) {
    28. Log.v(TAG, "onStart");
    29. if (intent != null) {
    30. Bundle bundle = intent.getExtras();
    31. if (bundle != null) {
    32. int op = bundle.getInt("op");
    33. switch (op) {
    34. case 1:
    35. play();
    36. break;
    37. case 2:
    38. stop();
    39. break;
    40. case 3:
    41. pause();
    42. break;
    43. }
    44. }
    45. }
    46. }
    47. public void play() {
    48. if (!mediaPlayer.isPlaying()) {
    49. mediaPlayer.start();
    50. }
    51. }
    52. public void pause() {
    53. if (mediaPlayer != null && mediaPlayer.isPlaying()) {
    54. mediaPlayer.pause();
    55. }
    56. }
    57. public void stop() {
    58. if (mediaPlayer != null) {
    59. mediaPlayer.stop();
    60. try {
    61. mediaPlayer.prepare(); // 在調用stop後如果需要再次通過start進行播放,需要之前調用prepare函數
    62. } catch (IOException ex) {
    63. ex.printStackTrace();
    64. }
    65. }
    66. }
    67. }

      AndroidManifest.xml

      註冊activity

      [css] view plaincopyprint?
      1. android:name=".service.PlayMusicService"
      2. android:label="@string/app_name" />

        註冊service

        [css] view plaincopyprint?
        1. android:name=".service.MusicService"
        2. android:enabled="true" >


        3. 五、 代碼解析

          1、Activity中,PlayMusicService中通過重寫OnClickListener 介面onClick()方法實現對播放音樂的控制,把音樂各種操作用數字通過Intent傳遞給service

          然後通過構造一個Intent , intent = new Intent("com.homer.service.musicService");

          其中,com.homer.service.musicService是 AndroidManifest.xml 對service的定義,即上面“註冊service”

          2、Activity中,音樂播放的控制,利用Bundle綁定數字op後,通過 startService(intent); 服務後發送出去
          Bundle bundle = new Bundle();
          bundle.putInt("op", op);
          intent.putExtras(bundle);

          startService(intent);

          3、 Service中,會處理Activity啟動的 startService(intent);服務,依次調用service的啟動過程:onCreate --> onStart(可多次調用) --> onDestroy

          onCreate(), 建立mediaPlayer

          onStart(), 通過擷取Bundle bundle = intent.getExtras();,提取int op = bundle.getInt("op");,然後執行響應的音樂播放操作

          onDestroy(),停止並釋放mediaPlayer音樂資源,如果當執行context.stopService()時調用此方法

          4、Activity中,onClick()函數中close與exit是執行含義是不同的:

          close : 只是執行了this.finish(); 關閉了本Activity表單,service並沒有被關掉,音樂依然會繼續在背景播放

          exit : 先調用了stopService(intent); 關閉了service服務,在Service中會調用3中的onDestroy()停止並釋放音樂資源,後才執行this.finish(); 關閉了本Activity表單


          源碼下載


          六、 拓展知識(進程和聲明周期)

          Android作業系統嘗試儘可能長時間的保持應用的進程,但當可用記憶體很低時最終要移走一部分進程。怎樣確定那些程式可以運行,那些要被銷毀,Android讓每一個進程在一個重要級的基礎上運行,重要級低的進程最有可能被淘汰,一共有5級,下面這個列表就是按照重要性排列的:
          1 一個前台進程顯示的是使用者此時需要處理和顯示的。下列的條件有任何一個成立,這個進程都被認為是在前台啟動並執行。
          a 與使用者正發生互動的。
          b 它控制一個與使用者互動的必須的基本的服務。
          c 有一個正在調用生命週期的回呼函數的service(如onCreate()、onStar()、onDestroy())
          d 它有一個正在運行onReceive()方法的廣播接收對象。
          只有少數的前台進程可以在任何給定的時間內運行,銷毀他們是系統萬不得已的、最後的選擇——當記憶體不夠系統繼續運行下去時。通常,在這一點上,裝置已經達到了記憶體分頁狀態,所以殺掉一些前台進程來保證能夠響應使用者的需求。


          2 一個可用進程沒有任何前台組件,但它仍然可以影響到使用者的介面。下面兩種情況發生時,可以稱該進程為可用進程。
          它是一個非前台的activity,但對使用者仍然可用(onPause()方法已經被調用)這是可能發生的,例如:前台的activity是一個允許上一個activity可見的對話方塊,即當前activity半透明,能看到前一個activity的介面,它是一個服務於可用activity的服務。

          3 一個服務進程是一個通過調用startService()方法啟動的服務,並且不屬於前兩種情況。儘管服務進程沒有直接被使用者看到,但他們確實是使用者所關心的,比如後台播放音樂或網路下載資料。所以系統保證他們的運行,直到不能保證所有的前台可見程式都正常運行時才會終止他們。

          4 一個後台進程就是一個非當前正在啟動並執行activity(activity的onStop()方法已經被調用),他們不會對使用者體驗造成直接的影響,當沒有足夠記憶體來運行前台可見程式時,他們將會被終止。通常,後台進程會有很多個在運行,所以他們維護一個LRU最近使用程式列表來保證經常啟動並執行activity能最後一個被終止。如果一個activity正確的實現了生命週期的方法,並且儲存它目前狀態,殺死這些進程將不會影響到使用者體驗。

          5 一個空線程沒有運行任何可用應用程式組,保留他們的唯一原因是為了設立一個緩衝機制,來加快組件啟動的時間。系統經常殺死這些記憶體來平衡系統的整個系統的資源,進程緩衝和基本核心緩衝之間的資源。
          Android把進程裡優先順序最高的activity或服務,作為這個進程的優先順序。例如,一個進程擁有一個服務和一個可見的activity,那麼這個進程將會被定義為可見進程,而不是服務進程。

          此外,如果別的進程依賴某一個進程的話,那麼被依賴的進程會提高優先順序。一個進程服務於另一個進程,那麼提供服務的進程不會低於獲得服務的進程。例如,如果進程A的一個內容供應商服務於進程B的一個用戶端,或者進程A的一個service被進程B的一個組件綁定,那麼進程A至少擁有和進程B一樣的優先順序,或者更高。

          因為一個運行服務的進程的優先順序高於運行後台activity的進程,一個activity會準備一個長時間啟動並執行操作來啟動一個服務,而不是啟動一個線程–尤其是這個操作可能會拖垮這個activity。例如後台播放音樂的同時,通過照相機向伺服器發送一張照片,啟動一個服務會保證這個操作至少運行在service 進程的優先順序下,無論這個activity發生了什麼,廣播接收者應該作為一個空服務而不是簡單的把耗時的操作單獨放在一個線程裡。





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.