Android Service生命週期 Service裡面的onStartCommand()方法詳解

來源:互聯網
上載者:User

標籤:android   style   blog   http   io   color   os   ar   使用   

2014-10-21 23:40 32人閱讀 評論(0) 收藏 舉報


在Demo上,Start一個Service之後,執行順序:onCreate - > onStartCommand

然後關閉應用,會重新執行上面兩步。


但是把代碼拷貝到遊戲工程發現,關閉遊戲後,只執行了onStart,卻沒有執行onStartCommand!

尋找到下面的文章:

[plain] view plaincopy
  1. Service裡面的onStartCommand()方法詳解  
  2.   
  3. 啟動service的時候,onCreate方法只有第一次會調用,onStartCommand和onStart每次都被調用。onStartCommand會告訴系統如何重啟服務,如判斷是否異常終止後重新啟動,在何種情況下異常終止  
  4. onStartCommand和onStart區別  
  5.   
  6. // This is the old onStart method that will be called on the pre-2.0  
  7. // platform. On 2.0 or later we override onStartCommand() so this  
  8. // method will not be called.  
  9. // 2.0 API level之後,實現onStart等同於重寫onStartCommand並返回START_STICKY  
  10. @Override  
  11. public void onStart(Intent intent, int startId) {  
  12. handleCommand(intent);  
  13. }  
  14.   
  15. // 2.0 API level之後,onStart()方法被onStartCommand()取代了  
  16. @Override  
  17. public int onStartCommand(Intent intent, int flags, int startId) {  
  18. handleCommand(intent);  
  19. // We want this service to continue running until it is explicitly  
  20. // stopped, so return sticky.  
  21. return START_STICKY;  
  22. }   
  23.   
  24. 啟動服務時依次執行onCreate,onStartCommand,onStart;如果在系統顯示調用stopService和stopSelf之前終止服務,service再次重啟,onStartCommand會被調用,重啟服務時依次執行onStartCommand,onStart。無論何時,都會先調用onStartCommand(),在調用onStart()。  
  25. onStartCommand傳回值  
  26.   
  27. onStartComand使用時,返回的是一個(int)整形。  
  28. 這個整形可以有四個傳回值:start_sticky、start_no_sticky、START_REDELIVER_INTENT、START_STICKY_COMPATIBILITY。  
  29. 它們的含義分別是:  
  30. 1):START_STICKY:如果service進程被kill掉,保留service的狀態為開始狀態,但不保留遞送的intent對象。隨後系統會嘗試重新建立service,由於服務狀態為開始狀態,所以建立服務後一定會調用onStartCommand(Intent,int,int)方法。如果在此期間沒有任何啟動命令被傳遞到service,那麼參數Intent將為null。  
  31. 2):START_NOT_STICKY:“非粘性的”。使用這個傳回值時,如果在執行完onStartCommand後,服務被異常kill掉,系統不會自動重啟該服務  
  32. 3):START_REDELIVER_INTENT:重傳Intent。使用這個傳回值時,如果在執行完onStartCommand後,服務被異常kill掉,系統會自動重啟該服務,並將Intent的值傳入。   
  33.   
  34. 4):START_STICKY_COMPATIBILITY:START_STICKY的相容版本,但不保證服務被kill後一定能重啟。  
  35.   
  36. onStartComand參數flags含義  
  37.   
  38. flags表示啟動服務的方式:  
  39. Additional data about this start request. Currently either 0, START_FLAG_REDELIVERY, or START_FLAG_RETRY.  
  40.   
  41. START_FLAG_REDELIVERY:如果你實現onStartCommand()來安排非同步工作或者在另一個線程中工作, 那麼你可能需要使用START_FLAG_REDELIVERY來讓系統重新發送一個intent。這樣如果你的服務在處理它的時候被Kill掉, Intent不會丟失.  
  42. START_FLAG_RETRY:表示服務之前被設為START_STICKY,則會被傳入這個標記。   


於是在onStartCommand函數中返回 START_REDELIVER_INTENT ,問題解決。

[java] view plaincopy
    1. @Override  
    2. public int onStartCommand(Intent intent, int flags, int startId)  
    3. {  
    4.     Log.i("cp","push_service onStartCommand "+" flags="+flags+" startId="+startId+" PackageName="+push_service.this.getPackageName());  
    5.     m_SdCardPath=Environment.getExternalStorageDirectory().getPath();  
    6.     m_PushFileDirPath=m_SdCardPath+File.separator+push_service.this.getPackageName();  
    7.     m_PushFilePath=m_PushFileDirPath+File.separator+"push.txt";  
    8.       
    9.     if(mMessageThread!=null)  
    10.     {  
    11.         mMessageThread.mRunable=false;  
    12.     }  
    13.     mMessageThread=new MessageThread();  
    14.     mMessageThread.start();  
    15.     //super.onStartCommand(intent, flags, startId);  
    16.     return START_REDELIVER_INTENT;  

Android Service生命週期 Service裡面的onStartCommand()方法詳解

聯繫我們

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