2011-11-15 27 views
2

也許問題很明顯,但我想確定基於我的應用程序。什麼都不做的前臺服務。這是一個很好的候選人被操作系統殺死?

我正在寫一個GPS跟蹤器。我使用靜態類中的位置服務來獲得我的位置。一切正常。不過,我注意到,我的應用程序有時會在運行幾個小時後死亡。爲了解決這個問題,我想把它轉換成前臺服務。

我沒有重寫我的代碼,而是將所有的邏輯放在服務中,我只是想創建一個什麼都不做的前臺服務。當用戶決定開始追蹤時,我會開始這項服務。請記住,我在靜態類中使用位置服務。

這是安全的嗎?或者操作系統會發現這個服務沒有做任何事情,它會有更多的機會被殺害?

這裏是我的服務:

public class SimpleService extends Service 
{ 
    private static final int ONGOING_NOTIFICATION = 1; 

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

     Notification notification = new Notification(R.drawable.icon, "Tracking started", System.currentTimeMillis()); 
     Intent notificationIntent = new Intent(this, MainActivity.class); 
     PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); 
     notification.setLatestEventInfo(this, "notification title", "notification message", pendingIntent);  
     startForeground(ONGOING_NOTIFICATION, notification); 
    } 


    @Override 
    public void onDestroy() 
    { 
      super.onDestroy();   
      stopForeground(true); 
    } 
} 

感謝

回答

1

我爲我創建了一個服務類似的事情定期跑,如果它改變了下載文件,但發現它是由OS喪生幾個小時後。由於使用startForeground,問題已消失。

我不認爲我的服務和簡單​​的工作之間沒有區別。

相關問題