从 Activity 调用时未显示另一个 AsyncTask class 中的 ProgressDialog

ProgressDialog in another AsyncTask class not showing when called from Activity

我有一个名为 RestClient 的 class,它从我的 webService 获取一些信息,然后 return 我正在尝试制作一个进度对话框 运行正在访问互联网。由于我在不止一个地方使用这个 class,所以我不会在 Activity 本身中使用它。这是我的 RestClient class:

public class RestClient extends AsyncTask<URL, String, String> {

    private Context context;
    private String string;
public RestClient(Context context, String string)
{
    this.context = context;
    this.string = string;
}

@Override
protected void onPreExecute() {
    dialog = ProgressDialog.show(context, "Buscando seu Produto","Por favor, espere um momento...",true ,false);
    //I've already tried:
    /*ProgressDialog dialog = new ProgressDialog(context);
    dialog.setTitle("Buscando seu Produto");
    dialog.setMessage("Por favor, espere um momento...");
    dialog.setIndeterminate(true);
    dialog.setCancelable(false);*/

    dialog.show();
    super.onPreExecute();
}

@Override
protected String doInBackground(URL... params) {
    try {

        //Some WebService gets and Json conversions using my string variable
        //and some Thread.sleep that counts 2000 miliseconds to do all the queries

        dialog.dismiss();

    } catch (IOException | InterruptedException |JSONException e) {
        e.printStackTrace();

        dialog.dismiss();

        return e.getMessage();
    }
    return null;
}

@Override
protected void onPostExecute(String s) {
    super.onPostExecute(s);


   }
}

在我的 activity 中,当我点击这样的按钮时,我调用 class RestClient

--- 编辑:我忘了提到我在同一个 activity 中有一个 AlertDialog,它有时可以在 ProgressDialog 之前和之后显示 ---

private Button buttonConfirm;
     private EditView evString;

     private String theString;
     private String returnFromExecute;

     private RestClient restClient;


     private AlertDialog.Builder dialog;

     @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_access_webservice);

            evString = (EditText) findViewById(R.id.editViewMyString);
            buttonConfirm = (Button) findViewById(R.id.buttonConfirm);

            dialog = new ProgressDialog(IdentificacaoDeProdutoActivity.this);
            dialog.setTitle("Error");
            dialog.setMessage("Please try again");
            dialog.setIndeterminate(true);
            dialog.setCancelable(false);


            buttonConfirmar.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v)  {                  
                    theString = evString.getText().toString();
                    if(!(theString!=null && theString.trim().length()>0)) //To check if theString is not null
                    {
                          dialog.show();
                    }

                    restClient = new RestClient(AccessWebserviceActivity.this, theString);

                    //Then I call execute and put a Thread.sleep a bit longer to compensate the ones I have in my doInBackground
                    restClient.execute();
                    try {
                        Thread.sleep(2050);

                    } catch (Exception e) {
                        dialog.show();
                        return;
                    }
                }
            }
        }

问题是我的 ProgressDialog 从未显示。我已经尝试过 getParent()getApplication()getApplicationContext() 而不是 AccessWebserviceActivity.this,但是 none 已经奏效了。有人知道发生了什么事,我该怎么做?

您尚未创建进度对话框试试这个。

ProgressDialog dialog;

 @Override
    protected void onPreExecute() {
        dialog= new ProgressDialog(context);
        dialog.setMessage("on Progress");
        dialog.show();
        super.onPreExecute();
    }

你需要打电话给

dialog = ProgressDialog.show(context, "Buscando seu Produto","Por favor, espere um momento...",true ,false);

并删除

 dialog.show();

也把你的dialog.dismiss()onPostExecute() 中的方法。这个 dialog.dismiss() 方法在 catch 块中很好用,但是如果你在 try 块中调用这个方法,它的目的是什么。一旦您调用此 AsyncTask.

,它将立即删除进度对话框
returnFromExecute = restClient.get();

删除该声明。您已经:

restClient.execute();

应该可以。

您应该在 onPostExecute() 中处理 doInBackground() 的结果。无法在 onCreate().

中处理或检索

经过大量关于线程和进程的研究后,我发现我必须封装我在我之后的所有代码 RestClient.execute

new Thread(new Runnable() { public void run() { // My code } });

以便代码和 WebService 查询在后台执行。

编辑:

即使创建新的Thread有效,也不推荐!正确的做法是创建 另一个 class 扩展 AsyncTask 来完成工作。