2013-12-12 27 views
0

當我的Activity退出時,我在警報中註冊了一個掛起的意圖,這將在60秒內啓動一個NotificationService。的onCreate()的代碼低於:服務被殺,而不是內存原因

PlayLogSender.getInstance(this).startSend(); 

和startSend():

public void startSend(){ 
new Thread(){ 
    @Override 
    public void run(){ 
     log("[thread]started"); 
     int interval=20; 
     try{ 
      //wait for QTRadioService starting in case of this is a APP launch 
      Thread.sleep(interval*1000); 
     }catch(Exception e){} 
     log("sleep "+s+"s done"); 
     while(some condition){ 
      ...... 
     } 
    } 
}.run() 
} 

而當PlayLogSender的線程完成睡覺,機器人殺死NotificationService;然後重新啓動它並再次殺死它。最後NotificationService再也沒有起來。我試圖將「間隔」設置爲15,然後一切正常。所以我認爲這不是因爲android殺死它的內存。

任何人都可以幫忙嗎?爲什麼Android會殺死我的服務?

回答

0

Thread上調用run()將不會在單獨的線程上運行,而是在調用線程中運行。要在單獨的線程中運行,請致電start()

如果您阻止主線程時間過長,Android會殺死您的服務。

+0

非常感謝! – Guocheng

0

你能顯示你的日誌嗎?可能是ANR。

+0

我不應該直接調用run()。不管怎樣,謝謝你。 – Guocheng

+0

哦,是的,我想跑();當然,你發生ANR。 – venciallee

相關問題