2011-04-28 112 views
1

我正在製作應用程序,用戶可以在其中使用例程服務。爲此,我使用管理員將能夠更新服務的網站。來自android的用戶將能夠通過解析服務器上可用的xml文件來獲取服務列表。刷新活動

我想知道的是,有什麼方法可以讓活動自動刷新自己,並且用戶可以通過管理員在服務中完成更新。

感謝

+0

像[C2DM](http://code.google.com/android/c2dm/index.html)的通知?刷新數據應該很簡單我猜,只需重新查詢服務並根據新數據進行操作即可。 – Audrius 2011-04-28 17:05:30

回答

1

如果您需要做的定期一些工作,你應該有一個看看HandlerAsyncTask。一般方案如下:

//This handler launches the task 
private final Handler handler = new Handler(); 

//This is a task which will be executed 
private class RefreshTask extends AsyncTask<String, Void, Object> { 
    protected Object doInBackground(String... params) { 
     //Do refreshing here 
    } 

    protected void onPostExecute(Object result) { 
     //Update UI here 
    } 
} 

//This is a Runnable, which will launch the task 
private final Runnable refreshRunnable = new Runnable() { 
    public void run() { 
     new RefreshTask(param1, param2).execute(); 
     handler.postDelayed(refreshRunnable, TIME_DELAY); 
    } 
}; 

然後當您想要開始更新時,請致電handler.post(refreshRunnable)。要取消它們,請致電handler.removeCallbacks(refreshRunnable)。當然,我應該注意到,我沒有測試過這個代碼,但它應該給出一個大概的想法。

+0

我還將一個成員mServerTask變量添加到我的活動中,並在開始新的執行之前,我會看到舊的任務是否仍在運行 – Tima 2011-04-28 17:15:24