2014-01-30 114 views
2

的服務這是我的服務如何強制停止後重新啓動應用

public class SService extends Service { 

     public void onCreate() { 
     super.onCreate(); 

     someTask(); 
     } 

     public int onStartCommand(Intent intent, int flags, int startId) { 

     someTask(); 

     return START_STICKY; 
     } 


     public IBinder onBind(Intent intent) { 

     return null; 
     } 

應用的力停止後(設置 - >應用程序 - >強制停止應用程序),我的服務沒有運行。如何解決它?

+0

**在用戶強行阻止您之後,您不應該重新啓動**,因爲這被認爲是用戶意願的表達,超過了您作爲開發者的願望。 –

+0

@Nick請檢查編輯答案。 –

+1

@Nick檢查此博客http://android-developers.blogspot.in/2010/04/multitasking-android-way.html –

回答

2

您確定該服務沒有重新啓動嗎?您在回車中放入的START_STICK應該執行一些操作,直到系統重新啓動爲止,您可以輸入日誌並等待以確保它重新啓動。

+0

之後僅在關閉應用後使用START_STICKY服務重啓後(destop-> menu->全部刪除) – NickUnuchek

+0

試了一下,你是對的,對不起,我以爲你錯過了什麼。 – Braunster

+1

如果有幫助,您可以使用警報管理器重新啓動您的服務。 http://stackoverflow.com/a/20297853/2568492 – Braunster

2

按了Android document

Starting from Android 3.1, the system's package manager keeps track of applications that 
are in a stopped state and provides a means of controlling their launch from background 
processes and other applications. 

Note that an application's stopped state is not the same as an Activity's stopped 
state. The system manages those two stopped states separately. 

Note that the system adds FLAG_EXCLUDE_STOPPED_PACKAGES to all broadcast intents. It 
does this to prevent broadcasts from background services from inadvertently or unnecessarily 
launching components of stoppped applications. A background service or application can 
override this behavior by adding the FLAG_INCLUDE_STOPPED_PACKAGES flag to broadcast intents 
that should be allowed to activate stopped applications. 

在應用程序的強制停止,Android的只是殺死進程ID。沒有警告,回調服務/活動。根據Android document,應用程序被終止時,有可能調用onPause()。

當我在我的應用程序中嘗試,即使onPause()沒有被調用。 我認爲唯一的方法是使用該意圖標誌並從Chris建議的另一個應用程序發送它。

+1

這對最近的Android版本無效。一旦用戶強制停止應用程序,廣播接收器將被禁用,直到用戶選擇手動重新啓動它。 –

+0

@Chris請檢查我編輯的答案。感謝克里斯糾正我。 –

+0

如果海報在引用應用程序的強制停止時是準確的,那麼這將不起作用,除非您有其他應用程序發送廣播。 –

相關問題