Android ProgressDialog无法从线程中关闭

时间:2019-01-25 13:03:35

标签: android multithreading runnable progressdialog

我正在开发一个android应用。 用户注册过程调用了一个发送电子邮件的服务,因此需要花费几秒钟,例如5或6秒钟,这就是我在线程中执行该任务的原因。问题是,对话框永远不会消失。它保持滚动状态,用户无能为力。这是我的代码:

try
        {
        final ProgressDialog progDailog = new ProgressDialog(ActividadAltaUsuario.this);

        new Thread(new Runnable() 
        {
        @Override
        public void run()
        {
            try
            {
                URL url = new URL("slowWS");
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("POST");

                InputStream in = new BufferedInputStream(conn.getInputStream());
                String response = IOUtils.toString(in, "UTF-8");
                final JSONObject jsonPrincipal = new JSONObject(response);
                Boolean success = jsonPrincipal.get("status").toString() == "true";

                if (success)
                {
                    ActividadAltaUsuario.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            progDailog.show(ActividadAltaUsuario.this, "Sendind email");
                        }
                    });

                    final String idUsuario = jsonPrincipal.get("idUsuario").toString();
                    URL url2 = new URL("anotherSlowWS");
                    HttpURLConnection conn2 = (HttpURLConnection) url2.openConnection();
                    conn2.setRequestMethod("POST");
                    InputStream in2 = new BufferedInputStream(conn2.getInputStream());
                    String response2 = IOUtils.toString(in2, "UTF-8");
                    JSONObject jsonRtaMail = new JSONObject(response2);
                    //finish();
                }
                else
                {
                    //finish();
                    showToast(jsonPrincipal.get("message").toString());
                }

                ActividadAltaUsuario.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        progDailog.dismiss();
                    }
                });
            }
            catch (Exception e)
            {
                e.printStackTrace();
            } 
        }
        }).start();

        }
        catch(Exception e)
        {
            Log.e("log_tag", "Error in http connection" + e.toString());
        }

有人可以帮助我吗? 谢谢!

2 个答案:

答案 0 :(得分:0)

AsyncTask是一种更好的方法,而不是线程,请从线程替换网络调用以使用AsyncTask。您可以使用类似的东西

private class LongOperation extends AsyncTask<Void, Void, Void> {

    @Override
    protected String doInBackground(Void... params) {
        //Main stuff that needs to be done in background

    }

    @Override
    protected void onPostExecute(Void result) {
        //Post Execution this method will be called, handle result accordingly
        //You can dismiss your dialog here
    }

    @Override
    protected void onPreExecute() {
        //Do initialization relative stuff here
        // Initialize your dialog here.
    }
}

onPostExecute()onPreExecute()都在主线程上工作时,您可以使用此方法显示和关闭对话框。

答案 1 :(得分:0)

只能从UI线程访问UI控件。 通常,我在扩展AsyncTask的类中进行此操作

类似: 公共类MyTask扩展了AsyncTask {

$  perl -lne ' if(eof) { /The job has completed.*\[(.*)\].*/ and print "Hex = $1" } ' logfile.txt
Hex = 0x0
$

LE: 有关AsyncTask https://developer.android.com/reference/android/os/AsyncTask的更多细节 特别检查受保护的方法