简体   繁体   English

在递归AsyncTask中仅显示ProgressDialog

[英]Showing ProgressDialog only once in a recursive AsyncTask

In the Java code I have executed an AsyncTask class and with the returned result I have done a recursive call to itself in the onPostexecute method. 在Java代码中,我执行了AsyncTask类,并使用返回的结果在onPostexecute方法中对其自身进行了递归调用。 Example: 例:

MainActivity.java MainActivity.java

public void button_clicked(){
  UploadAsync send_data = new UploadAsync(MainActivity.this);
  send_data.execute("send first data", user_data, file_path);
}

UploadAsync.java UploadAsync.java

@Override
protected String doInBackground(String... params) {
  String task = params[0];
  if(task.equals("send first data"){
    String user_data = params[1];
    String file_path = params[2];

    //send in the user_data to a php file

  }else if(task.equals("send file"){
    String file_path = params[1];

    //send the file_path to another php file

  }
}

@Override
protected void onPreExecute() {
 ProgressDialog pd = ProgressDialog.show(context, "Sending", "Please wait");
}

@Override
protected void onPostExecute(String result) {
  if(result.equals("data sent"){
    UploadAsync send_data = new UploadAsync(context);
    send_data.execute("send file", file_path);
  }else{
    //show error
  }

  pd.dismiss();
}

The code above is only an example made. 上面的代码只是一个例子。 Now the thing is, implementing this example will run the progress dialog twice. 现在,实现此示例将运行进度对话框两次。 I have tried many ways to only show the progress dialog once while the AsyncTask is sending the user data and the file path but I'm not succeeding. 我尝试了许多方法,仅在AsyncTask发送用户数据和文件路径时只显示一次进度对话框,但我没有成功。 Is there any suggestions on how to implement this correctly? 关于如何正确实施此建议?

你能不能叫pd.dismiss()和解雇ProgressDialog当前UploadAsync执行新的前UploadAsync

In onPostExecute first, dismiss the dialog and then start new asynctask like this: 首先在onPostExecute中,关闭对话框,然后启动新的asynctask,如下所示:

@Override
protected void onPostExecute(String result) {

  pd.dismiss();

  if(result.equals("data sent"){
    UploadAsync send_data = new UploadAsync(context);
    send_data.execute("send file", file_path);
  }else{
    //show error
  }

}

I hope this will help you. 我希望这能帮到您。

Let ProgressDialog as global data . ProgressDialog作为全局数据。

private ProgressDialog pd;

@Override
protected void onPreExecute() {
    // show the ProgressDialog
    if (pd == null) {
        pd = ProgressDialog.show(context, "Sending", "Please wait");
    }
}

@Override
protected void onPostExecute(String result) {
    if (result.equals("data sent") {
        UploadAsync send_data = new UploadAsync(context);
        send_data.execute("send file", file_path);
    }else{
        //show error
    }
    // edited here ,make the ProgressDialog dismiss
    if (pd.isShowing() && pd != null) {
        pd.dismiss();
        pd = null;
    }
}

In that case use callback mechanism between the async task and the caller(here, MainActivity ). 在这种情况下,请在异步任务和调用者之间使用回调机制(此处为MainActivity )。

Implement as I explained below. 如我在下面解释的那样实施。

  • Define an interface with showProgressDiaglog() and DismissProgressDialog() methods. 使用showProgressDiaglog()DismissProgressDialog()方法定义一个interface

  • MainActivity implements this interface and implements the two methods to show and dismiss progress dialog respectively. MainActivity实现此接口,并实现两种方法分别显示和关闭进度对话框。

  • Define setter method in your AsyncTask class and call this setter method from the MainActivity by passing this (this refers to MainActivity) after creating the AsyncTask instance and before calling execute method. 定义setter method在你AsyncTask类并调用从这个setter方法MainActivity通过传递this之后(这是指MainActivity) creating the AsyncTask instance and before calling execute method. store the passed interface implementation in the AsyncTask class as an instance variable. 将传递的接口实现作为实例变量存储在AsyncTask类中。

  • Modify AsyncTask constructor to pass the task type instead of passing in the execute method and store this task type in the AsyncTask as an instance variable. 修改AsyncTask构造函数以传递任务类型,而不是传递execute方法,然后将此任务类型作为实例变量存储在AsyncTask中。

  • Now from within the AsyncTask constructor, in onPreExecute() method, call showProgressDialog() method based on the task type. 现在,在AsyncTask构造函数中的onPreExecute()方法中,根据任务类型调用showProgressDialog()方法。 and in onPostEecute() method call dismissProgressDialog() depending on the task type. 然后在onPostEecute()方法中根据任务​​类型调用dismissProgressDialog()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM