我有一個從服務器獲取數據的AsyncTask。我需要的是在對話框中顯示重新獲取的數據。我怎樣才能調用函數來在AsyncTask中創建對話框?如果我不能這樣做,那麼我有什麼選擇?是否有可能知道AsyncTask是否已完成?是否可以從AsyncTask中調用另一個組合?
這裏是我的代碼:
//Function calling the AsyncTask
public void getData(int pos)
{
new DBTask().execute();
//Is it possible to know here if the AsyncTask has ended or not??
}
private class DBTask extends AsyncTask<Long, Boolean, Integer>{
ProgressDialog ServerPD = new ProgressDialog(MRRankingActivity.this);
@Override
protected void onPreExecute()
{
ServerPD = ProgressDialog.show (MRRankingActivity.this, "", "Connecting to server...", true, false);
}//End of onPreExecute
@Override
protected Integer doInBackground(Long... params)
{
publishProgress(isOnline());
if(isOnline())
{
getDBData();
if(isOK)
{
//I want to show the result by calling this function..
showResult();
//Calling this function will trow error..So how can i show my result??
Log.d("LIST","DONE..Reached Final Destination");
}
}
}
return null;
}
@Override
protected void onProgressUpdate(Boolean... isConnection) {
// TODO Auto-generated method stub
super.onProgressUpdate(isConnection);
if(isOnline())
{
ServerPD.setMessage("Retreving Data");
}
}
@Override
protected void onPostExecute(Integer result) {
if (ServerPD.isShowing())
{
ServerPD.dismiss();
}
}//End of onPostExecute
}//End of DBTask
public void getDBData()
{
try{
//Code foe connecting to database and retreive data.
//If everything is ok then isOK is set to True..
//else its set to false
isOK=true;
}
catch(Exception e)
{
isOK=false;
}//End if catch
}
public void showResult()
{
//I want to show result here in a new Dialog ...
}
//Checking is connection available
public boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
}
return false;
}
我怎樣才能顯示結果在這種情況呢?有什麼方法可以查找AsyncTask是否結束?
謝謝..你讓它看起來很簡單 – Shijilal