2012-10-17 74 views
0

2個屏幕出現在一個應用程序中, 在第一個屏幕中只有一個按鈕,第二個屏幕中的1000個列表項目列在單個列表中,如果用戶在第一個屏幕上單擊按鈕,他將指向第二個屏幕,但是在第二個屏幕顯示1000個列表項目用於加載這1000個列表項目需要很長時間,因此用戶點擊菜單按鈕並轉到其他應用程序,在用戶返回主應用程序一段時間後,列表視圖處於之前的狀態,即仍在加載,我想通過在後臺使用服務實現來解決這個問題,在android中可能嗎?如何在加載列表視圖時實現後臺服務?

回答

0

**您可以使用線程,異步任務,服務 但UIupdating UIthread是最好的過程中運行一個後臺進程,因爲

When your app performs intensive work in response to user interaction, this single thread model can yield poor performance unless you implement your application properly. Specifically, if everything is happening in the UI thread, performing long operations such as network access or database queries will block the whole UI. When the thread is blocked, no events can be dispatched, including drawing events. From the user's perspective, the application appears to hang. Even worse, if the UI thread is blocked for more than a few seconds (about 5 seconds currently) the user is presented with the infamous "application not responding" (ANR) dialog. The user might then decide to quit your application and uninstall it if they are unhappy. 

Additionally, the Andoid UI toolkit is not thread-safe. So, you must not manipulate your UI from a worker thread—you must do all manipulation to your user interface from the UI thread. Thus, there are simply two rules to Android's single thread model: 

Do not block the UI thread Do not access the Android UI toolkit from outside the UI thread**