在Demo上,Start一個Service之後,執行順序:onCreate - > onStartCommand
然後關閉應用,會重新執行上面兩步。
但是把代碼拷貝到遊戲工程發現,關閉遊戲後,只執行了onStart,卻沒有執行onStartCommand!
查找到下面的文章:
- Service裏面的onStartCommand()方法詳解
- 啓動service的時候,onCreate方法只有第一次會調用,onStartCommand和onStart每次都被調用。onStartCommand會告訴系統如何重啓服務,如判斷是否異常終止後重新啓動,在何種情況下異常終止
- onStartCommand和onStart區別
- // This is the old onStart method that will be called on the pre-2.0
- // platform. On 2.0 or later we override onStartCommand() so this
- // method will not be called.
- // 2.0 API level之後,實現onStart等同於重寫onStartCommand並返回START_STICKY
- @Override
- public void onStart(Intent intent, int startId) {
- handleCommand(intent);
- }
- // 2.0 API level之後,onStart()方法被onStartCommand()取代了
- @Override
- public int onStartCommand(Intent intent, int flags, int startId) {
- handleCommand(intent);
- // We want this service to continue running until it is explicitly
- // stopped, so return sticky.
- return START_STICKY;
- }
- 啓動服務時依次執行onCreate,onStartCommand,onStart;如果在系統顯示調用stopService和stopSelf之前終止服務,service再次重啓,onStartCommand會被調用,重啓服務時依次執行onStartCommand,onStart。無論何時,都會先調用onStartCommand(),在調用onStart()。
- onStartCommand返回值
- onStartComand使用時,返回的是一個(int)整形。
- 這個整形可以有四個返回值:start_sticky、start_no_sticky、START_REDELIVER_INTENT、START_STICKY_COMPATIBILITY。
- 它們的含義分別是:
- 1):START_STICKY:如果service進程被kill掉,保留service的狀態爲開始狀態,但不保留遞送的intent對象。隨後系統會嘗試重新創建service,由於服務狀態爲開始狀態,所以創建服務後一定會調用onStartCommand(Intent,int,int)方法。如果在此期間沒有任何啓動命令被傳遞到service,那麼參數Intent將爲null。
- 2):START_NOT_STICKY:“非粘性的”。使用這個返回值時,如果在執行完onStartCommand後,服務被異常kill掉,系統不會自動重啓該服務
- 3):START_REDELIVER_INTENT:重傳Intent。使用這個返回值時,如果在執行完onStartCommand後,服務被異常kill掉,系統會自動重啓該服務,並將Intent的值傳入。
- 4):START_STICKY_COMPATIBILITY:START_STICKY的兼容版本,但不保證服務被kill後一定能重啓。
- onStartComand參數flags含義
- flags表示啓動服務的方式:
- Additional data about this start request. Currently either 0, START_FLAG_REDELIVERY, or START_FLAG_RETRY.
- START_FLAG_REDELIVERY:如果你實現onStartCommand()來安排異步工作或者在另一個線程中工作, 那麼你可能需要使用START_FLAG_REDELIVERY來讓系統重新發送一個intent。這樣如果你的服務在處理它的時候被Kill掉, Intent不會丟失.
- START_FLAG_RETRY:表示服務之前被設爲START_STICKY,則會被傳入這個標記。
Service裏面的onStartCommand()方法詳解
啓動service的時候,onCreate方法只有第一次會調用,onStartCommand和onStart每次都被調用。onStartCommand會告訴系統如何重啓服務,如判斷是否異常終止後重新啓動,在何種情況下異常終止
onStartCommand和onStart區別
// This is the old onStart method that will be called on the pre-2.0
// platform. On 2.0 or later we override onStartCommand() so this
// method will not be called.
// 2.0 API level之後,實現onStart等同於重寫onStartCommand並返回START_STICKY
@Override
public void onStart(Intent intent, int startId) {
handleCommand(intent);
}
// 2.0 API level之後,onStart()方法被onStartCommand()取代了
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
handleCommand(intent);
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
return START_STICKY;
}
啓動服務時依次執行onCreate,onStartCommand,onStart;如果在系統顯示調用stopService和stopSelf之前終止服務,service再次重啓,onStartCommand會被調用,重啓服務時依次執行onStartCommand,onStart。無論何時,都會先調用onStartCommand(),在調用onStart()。
onStartCommand返回值
onStartComand使用時,返回的是一個(int)整形。
這個整形可以有四個返回值:start_sticky、start_no_sticky、START_REDELIVER_INTENT、START_STICKY_COMPATIBILITY。
它們的含義分別是:
1):START_STICKY:如果service進程被kill掉,保留service的狀態爲開始狀態,但不保留遞送的intent對象。隨後系統會嘗試重新創建service,由於服務狀態爲開始狀態,所以創建服務後一定會調用onStartCommand(Intent,int,int)方法。如果在此期間沒有任何啓動命令被傳遞到service,那麼參數Intent將爲null。
2):START_NOT_STICKY:“非粘性的”。使用這個返回值時,如果在執行完onStartCommand後,服務被異常kill掉,系統不會自動重啓該服務
3):START_REDELIVER_INTENT:重傳Intent。使用這個返回值時,如果在執行完onStartCommand後,服務被異常kill掉,系統會自動重啓該服務,並將Intent的值傳入。
4):START_STICKY_COMPATIBILITY:START_STICKY的兼容版本,但不保證服務被kill後一定能重啓。
onStartComand參數flags含義
flags表示啓動服務的方式:
Additional data about this start request. Currently either 0, START_FLAG_REDELIVERY, or START_FLAG_RETRY.
START_FLAG_REDELIVERY:如果你實現onStartCommand()來安排異步工作或者在另一個線程中工作, 那麼你可能需要使用START_FLAG_REDELIVERY來讓系統重新發送一個intent。這樣如果你的服務在處理它的時候被Kill掉, Intent不會丟失.
START_FLAG_RETRY:表示服務之前被設爲START_STICKY,則會被傳入這個標記。
於是在onStartCommand函數中返回 START_REDELIVER_INTENT ,問題解決。
- @Override
- public int onStartCommand(Intent intent, int flags, int startId)
- {
- Log.i("cp","push_service onStartCommand "+" flags="+flags+" startId="+startId+" PackageName="+push_service.this.getPackageName());
- m_SdCardPath=Environment.getExternalStorageDirectory().getPath();
- m_PushFileDirPath=m_SdCardPath+File.separator+push_service.this.getPackageName();
- m_PushFilePath=m_PushFileDirPath+File.separator+"push.txt";
- if(mMessageThread!=null)
- {
- mMessageThread.mRunable=false;
- }
- mMessageThread=new MessageThread();
- mMessageThread.start();
- //super.onStartCommand(intent, flags, startId);
- return START_REDELIVER_INTENT;
- }