是否可以从AsyncTask内部调用另一个函数?

时间:2011-03-01 03:45:57

标签: android android-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结束的方法?

4 个答案:

答案 0 :(得分:1)

而不是给予

private class DBTask extends AsyncTask<Long, Boolean, Integer>{ 

private class DBTask extends AsyncTask<Long, Boolean, Boolean>{ 

然后在doInBackground()里面创建一个变量:

boolean success = false;

在你的内部ifOnline()make:

success = true;

在该方法的最后给出:

return success;

在onPostExecute()内部,参数为boolean而不是Integer。

给:

if(result) {

showResult();

}

希望这可以帮到你。 onPostExecute()仅在doInBackground()之后调用。

答案 1 :(得分:1)

达到目的的最佳方法是创建Handler并在其中显示对话框。像这样的事情

class RefreshHandler extends Handler {

    @Override

    public void handleMessage(Message msg) {

AlertDialog.Builder alertbox = new AlertDialog.Builder(
                            CardDetails.this);
                    alertbox.setMessage("Please Login First ...");
                    alertbox.setPositiveButton("Ok",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface arg0,
                                        int arg1) {
                                }
                            });
                    alertbox.show();

    }
}

并从您的异步任务

执行此操作

new RefreshHandler().sendEmptyMessage(0)

答案 2 :(得分:1)

简化Mathew建议的内容..只需像这样修改你的代码 首先通过初始化int varable isSuccess然后在isOK if语句中将isSucces的值更改为1来修改doInBackgroud。现在返回isSuccess.like this

 @Override
    protected Integer doInBackground(Long... params) 
    {
        int isSuccess=0;
        publishProgress(isOnline());

            if(isOnline())
            {

            getDBData();
                    if(isOK)
                        {
                        isSuccess=1;
                        }
            }   
        }

        return isSuccess;
    }

现在修改你的PostExecute()以处理在onPostExecute中导致的isSuccess值。

@Override
    protected void onPostExecute(Integer result) {
        if (ServerPD.isShowing())
        {
            ServerPD.dismiss(); 
        }
        if(result==1)
            {
                showResult();
            }

    }//End of onPostExecute

希望这有帮助。

答案 3 :(得分:0)

Shijilal ...当doInBackground方法结束时,您将在onPostExecute中得到通知。 doInBackground中的返回值被发送到onPostExecute。您不应该在doInBackground中触摸UI,该UI在与UI线程分开的线程中运行。

我打算在调用asynchTask.execute之前调用isOnline。我认为doInBackground中的方法会返回Result类型,然后将其发送到onPostExecute。因此,请考虑:AsynchTask<SQLQuery,Void,ResultSet>或其他任何合适的内容。

相关问题