2011-08-12 18 views
0

我有一個應用程序,一旦用戶運行應用程序,它就有一個漫長的過程來運行,比如下載需要的文件並將其全部設置完成。我希望將進程移至後臺,並在進程運行時在狀態欄中顯示通知。Android從狀態欄運行很長的過程

我最好的策略是在下載文件的同時保持進程的活躍狀態,而不是。我已閱讀過有關服務的消息,但也聽說他們很容易被殺害?我應該使用服務還是應該以最高優先級運行一個線程,並在處理結束時僅向其發出通知並關閉它?

什麼是最好的謝謝你的任何幫助。順便說一下,大概十分鐘的時間不再比在ROM管理器中下載ROM時基本上只是想要相同的設置感謝您的任何幫助。

回答

0

這聽起來像一個前臺服務正是你所需要的。當您創建它時,會在狀態欄中放置一條通知,告訴用戶它正在運行。該服務也被賦予高優先級,以便它不會被取消。

從對前景標記的文檔:

public final void startForeground (int id, Notification notification) 

Make this service run in the foreground, supplying the ongoing notification 
to be shown to the user while in this state. By default services are background, 
meaning that if the system needs to kill them to reclaim more memory (such as to 
display a large page in a web browser), they can be killed without too much harm. 
You can set this flag if killing your service would be disruptive to the user, 
such as if your service is performing background music playback, so the user 
would notice if their music stopped playing. 

你可以看一下文檔here的其餘部分。

+0

好的,謝謝你的回答,我要檢查一下,然後接受答案,一旦我玩你提到的一點點 – user577732

0

服務正是你想要的。您可以指定創建時服務的優先級(請參閱@ theisenp的帖子)。

至於將您的Activity移動到後臺並稍後重新打開 - 我建議啓動該服務,然後使用finish()方法實際停止您的應用。然後,一旦服務完成,用Intent重新啓動您的活動,或者廣播可由BroadcastReceiver處理的意圖。