简体   繁体   English

Android - 在 AsyncTask 执行时显示进度对话框

[英]Android - Show progress dialog while AsyncTask executes

I have the following class, which calls a web service that sends an email, which can take up to 5 seconds, depending on the connection.我有以下课程,它调用发送电子邮件的 Web 服务,这可能需要 5 秒,具体取决于连接。 The idea is to show a message to the user while the mail is being sent, so that it is on hold and can not perform any action这个想法是在发送邮件时向用户显示一条消息,以便它被搁置并且不能执行任何操作

This is my class:这是我的课:

private class TareaEnviarMail extends AsyncTask<String, Void, String>
    {
        ProgressDialog.Builder b;

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

                URL url = new URL("https://example.sendmail.com");
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("POST");

                InputStream in = new java.io.BufferedInputStream(conn.getInputStream());
                String response = org.apache.commons.io.IOUtils.toString(in, "UTF-8");

            }
            catch(Exception ex)
            {
                Log.e("Error",ex.toString());
            }

            return null;

        }

        @Override
        protected void onPostExecute(String result)
        {
            //HERE, I WANT TO DISMISS THE DIALOG!!! LIKE b.dismiss(); ¿?
            Intent intent = new Intent(getApplicationContext(), ActividadPrincipal.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
        }

        @Override
        protected void onPreExecute()
        {
            b = new  ProgressDialog.Builder(ActividadNuevoTurnoSeleccionar.this);
            b.setTitle("Enviando correo. Revise su casilla.");
            b.setMessage("Por favor, espere mientras registramos el turno y le enviamos la confirmación a su casilla.");
            b.create().show();
            //ProgressDialog.show(ActividadNuevoTurnoSeleccionar.this,"Enviando correo. Revise su casilla.", "Por favor, espere mientras registramos el turno y le enviamos la confirmación a su casilla.");
        }

        @Override
        protected void onProgressUpdate(Void... values) {}
    }

The problem is that now I don't know how to close the dialog in onpostexecute method, when the background task is over.问题是现在我不知道如何在后台任务结束时关闭 onpostexecute 方法中的对话框。 Since I don't have the dialog, but the builder.因为我没有对话框,而是构建器。 Could you help me?你可以帮帮我吗? Thank you谢谢

Change ProgressDialog.Builder b;更改ProgressDialog.Builder b; to dialog, like ProgressDialog dialog;对话框,如ProgressDialog dialog; , since you need the reference of the dialog and not it's builder, on onPreExecute you can create the builder and assign the dialog it builds to the dialog declared before. ,因为您需要对话框的引用而不是它的构建器,在 onPreExecute 您可以创建构建器并将它构建的对话框分配给之前声明的对话框。

@Override
        protected void onPreExecute()
        {
            ProgressDialog.Builder b = new  ProgressDialog.Builder(ActividadNuevoTurnoSeleccionar.this);
            b.setTitle("Enviando correo. Revise su casilla.");
            b.setMessage("Por favor, espere mientras registramos el turno y le enviamos la confirmación a su casilla.");
            dialog = b.create();
            b.show();
            //ProgressDialog.show(ActividadNuevoTurnoSeleccionar.this,"Enviando correo. Revise su casilla.", "Por favor, espere mientras registramos el turno y le enviamos la confirmación a su casilla.");
        }

Then dismiss the dialog as usual in onPostExecute();然后像往常一样在 onPostExecute() 中关闭对话框;

    private class TareaEnviarMail extends AsyncTask<String, Void, String> {
    ProgressDialog progressDialog;

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

            URL url = new URL("https://example.sendmail.com");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");

            InputStream in = new java.io.BufferedInputStream(conn.getInputStream());
            String response = org.apache.commons.io.IOUtils.toString(in, "UTF-8");

        } catch (Exception ex) {
            Log.e("Error", ex.toString());
        }

        return null;

    }

    @Override
    protected void onPostExecute(String result) {
        //HERE, I WANT TO DISMISS THE DIALOG!!! LIKE b.dismiss(); ¿?

        if(progressDialog!=null && progressDialog.isShowing())
            progressDialog.dismiss();
        Intent intent = new Intent(getApplicationContext(), ActividadPrincipal.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    }

    @Override
    protected void onPreExecute() {
        progressDialog = new ProgressDialog(ActividadNuevoTurnoSeleccionar.this);
        progressDialog.setMessage("Please wait...");
        progressDialog.setCancelable(false);
        progressDialog.show();


    }

    @Override
    protected void onProgressUpdate(Void... values) {
    }
}

Create an object of progress dialog and replace onPostExecute and onPreExecute with below code创建一个进度对话框对象并用下面的代码替换 onPostExecute 和 onPreExecute

ProgressDialog dialog;

    @Override
    protected void onPostExecute(String result)
    {
        dismiss.show();
        //HERE, I WANT TO DISMISS THE DIALOG!!! LIKE b.dismiss(); ¿?
        Intent intent = new Intent(getApplicationContext(), ActividadPrincipal.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    }

    @Override
    protected void onPreExecute()
    {
       dialog= new  ProgressDialog.Builder(ActividadNuevoTurnoSeleccionar.this)
        .setTitle("Enviando correo. Revise su casilla.")
        .setMessage("Por favor, espere mientras registramos el turno y le enviamos la confirmación a su casilla.")
        .create();
        dialog.show();
        //ProgressDialog.show(ActividadNuevoTurnoSeleccionar.this,"Enviando correo. Revise su casilla.", "Por favor, espere mientras registramos el turno y le enviamos la confirmación a su casilla.");
    }

Another solution would be not using a builder.另一种解决方案是不使用构建器。

ProgressDialog b;

and on preExecute并在执行前

@Override
    protected void onPreExecute()
    {
        b = new  ProgressDialog(ActividadNuevoTurnoSeleccionar.this);
        b.setTitle("Enviando correo. Revise su casilla.");
        b.setMessage("Por favor, espere mientras registramos el turno y le enviamos la confirmación a su casilla.");
        b.create();
        b.show();
    }

Then again, just dismiss it as usual in postExecute.再说一次,只需像往常一样在 postExecute 中关闭它。

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

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