2014-05-12 74 views
-1

如何在後臺服務中以2分鐘的睡眠時間間隔休眠10次定期運行AsyncTaskAndroid:如何在後臺服務中定期運行AsyncTask

隨着VM在任何時候停止我的服務,我的AsyncTask也正在關閉。

我試過ScheduledThreadPoolExecutorTimer,TimerTask一旦Service停止,所有設備都會停止。

有沒有一個很好的選擇開始?

任何幫助表示讚賞。

+0

@Nisha,需要的任何信息?我需要定期在後臺運行AsyncTask。我想你知道這個服務會隨時停止虛擬機。 – User12111111

回答

0

您的服務永遠不會停止,但您應該顯示通知。嘗試實施前臺服務。 foreground service

前臺服務顯示通知並且從不停止。

在服務

Notification notification = new Notification(R.drawable.icon, getText(R.string.ticker_text), 
     System.currentTimeMillis()); 
Intent notificationIntent = new Intent(this, ExampleActivity.class); 
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); 
notification.setLatestEventInfo(this, getText(R.string.notification_title), 
     getText(R.string.notification_message), pendingIntent); 
startForeground(ONGOING_NOTIFICATION_ID, notification); 
+0

謝謝,有沒有其他方式沒有顯示通知狀態欄? – User12111111

+0

沒有顯示通知您的服務將被殺死,當android覺得它需要資源時沒有辦法。 –

0

您是否嘗試過的處理函數中實現此代碼段?你可以閱讀更多關於這個在這link

+0

謝謝,快速問題,'Handler'與'Service'一起關閉。 「服務」將被虛擬機殺死。我需要在後臺運行'AsyncTask',即使App沒有啓動。所以基本上我選擇了「服務」。我的實現工作,當'服務'沒有關閉,但不幸的是'服務'被虛擬機關閉。 – User12111111