Android Service的生命週期1

來源:互聯網
上載者:User

標籤:android service的生命週期1

Managing the Lifecycle of a Service

  service的生命週期,從它被建立開始,到它被銷毀為止,可以有兩條不同的路徑:

A started service

  被開啟的service通過其他組件調用 startService()被建立。

  這種service可以無限地運行下去,必須調用stopSelf()方法或者其他組件調用stopService()方法來停止它。

  當service被停止時,系統會銷毀它。

 

A bound service

  被綁定的service是當其他組件(一個客戶)調用bindService()來建立的。

  客戶可以通過一個IBinder介面和service進行通訊。

  客戶可以通過 unbindService()方法來關閉這種串連。

  一個service可以同時和多個客戶綁定,當多個客戶都解除綁定之後,系統會銷毀service。

 

  這兩條路徑並不是完全分開的。

  即是說,你可以和一個已經調用了 startService()而被開啟的service進行綁定。

  比如,一個後台音樂service可能因調用 startService()方法而被開啟了,稍後,可能使用者想要控制播放器或者得到一些當前歌曲的資訊,可以通過bindService()將一個activity和service綁定。這種情況下,stopService()或 stopSelf()實際上並不能停止這個service,除非所有的客戶都解除綁定。

 

Implementing the lifecycle callbacks

  和activity一樣,service也有一系列的生命週期回呼函數,你可以實現它們來監測service狀態的變化,並且在適當的時候執行適當的工作。

  下面的service展示了每一個生命週期的方法:

650) this.width=650;" src="http://common.cnblogs.com/images/copycode.gif" alt="複製代碼" style="margin:0px;border:none;" />

public class ExampleService extends Service{    int mStartMode; // indicates how to behave if the service is killed    IBinder mBinder; // interface for clients that bind    boolean mAllowRebind; // indicates whether onRebind should be used    @Override    public void onCreate()    {        // The service is being created    }    @Override    public int onStartCommand(Intent intent, int flags, int startId)    {        // The service is starting, due to a call to startService()        return mStartMode;    }    @Override    public IBinder onBind(Intent intent)    {        // A client is binding to the service with bindService()        return mBinder;    }    @Override    public boolean onUnbind(Intent intent)    {        // All clients have unbound with unbindService()        return mAllowRebind;    }    @Override    public void onRebind(Intent intent)    {        // A client is binding to the service with bindService(),        // after onUnbind() has already been called    }    @Override    public void onDestroy()    {        // The service is no longer used and is being destroyed    }}

650) this.width=650;" src="http://common.cnblogs.com/images/copycode.gif" alt="複製代碼" style="margin:0px;border:none;" />

 

  不像是activity的生命週期回呼函數,你不需要調用基類的實現。

 650) this.width=650;" src="http://images.cnitblog.com/blog/325852/201303/24233205-ccefbc4a326048d79b111d05d1f8ff03.png" style="border:0px;margin-left:auto;margin-right:auto;" />

  這個圖說明了service典型的回調方法,儘管這個圖中將開啟的service和綁定的service分開,但是你需要記住,任何service都潛在地允許綁定。

  所以,一個被開啟的service仍然可能被綁定。

  實現這些方法,你可以看到兩層嵌套的service的生命週期:

 

The entire lifetime

  service整體的生命時間是從onCreate()被調用開始,到onDestroy()方法返回為止。

  和activity一樣,service在onCreate()中進行它的初始化工作,在onDestroy()中釋放殘留的資源。

  比如,一個音樂播放service可以在onCreate()中建立播放音樂的線程,在onDestory()中停止這個線程。

   onCreate() 和 onDestroy()會被所有的service調用,不論service是通過startService()還是bindService()建立。

 

The active lifetime

  service積極活動的生命時間(active lifetime)是從onStartCommand() onBind()被調用開始,它們各自處理由startService()或 bindService()方法傳過來的Intent對象。

  如果service是被開啟的,那麼它的活動生命週期和整個生命週期一同結束。

  如果service是被綁定的,它們它的活動生命週期是在onUnbind()方法返回後結束。

  注意:儘管一個被開啟的service是通過調用 stopSelf() 或 stopService()來停止的,沒有一個對應的回呼函數與之對應,即沒有onStop()回調方法。所以,當調用了停止的方法,除非這個service和客戶組件綁定,否則系統將會直接銷毀它,onDestory()方法會被調用,並且是這個時候唯一會被調用的回調方法。

 

Managing the Lifecycle of a Bound Service

  當綁定service和所有用戶端解除綁定之後,Android系統將會銷毀它,(除非它同時被onStartCommand()方法開啟)。

  因此,如果你的service是一個純粹的綁定service,那麼你不需要管理它的生命週期。

  然而,如果你選擇實現onStartCommand()回調方法,那麼你必須顯式地停止service,因為service此時被看做是開啟的。

  這種情況下,service會一直運行到它自己調用 stopSelf()或另一個組件調用stopService(),不論它是否和用戶端綁定。

  另外,如果你的service被開啟並且接受綁定,那麼當系統調用你的 onUnbind()方法時,如果你想要在下次用戶端綁定的時候接受一個onRebind()的調用(而不是調用 onBind()),你可以選擇在 onUnbind()中返回true。

  onRebind()的傳回值為void,但是用戶端仍然在它的 onServiceConnected()回調方法中得到 IBinder 對象。

  展示了這種service(被開啟,還允許綁定)的生命週期:

650) this.width=650;" src="http://images.cnitblog.com/blog/325852/201303/24233647-0a03689c14da415ebbcb2913f2932d7d.png" style="border:0px;margin-left:auto;margin-right:auto;" />


Android Service的生命週期1

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.