简体   繁体   English

进程运行时,在Asnyctask中运行进度对话框

[英]Run progress Dialog in Asnyctask while process running

I am trying to run the following code but I want to show a progress dialog while my process is being run: 我正在尝试运行以下代码,但是我想在运行过程时显示一个进度对话框:

public boolean isOnline() {
    cm = (ConnectivityManager) mContext.getSystemService(mContext.CONNECTIVITY_SERVICE);
    netInfo = cm.getActiveNetworkInfo();

    progress_thread = new progress_thread();
    progress_thread.execute();

    isconnected = false;
    if (netInfo != null && netInfo.isConnected()) {
        try{
            URL url = new URL("http://www.google.com");
            HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
            urlc.setConnectTimeout(3000);
            urlc.connect();
            if (urlc.getResponseCode() == 200)  isconnected = true;

        }catch (MalformedURLException e1) {
            e1.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        }
    }



    progress_thread.cancel(true);
    if (!isconnected) Toast.makeText(mContext,"Internet Connexion Error", Toast.LENGTH_LONG).show();

    return isconnected;


private class progress_thread extends AsyncTask<Void, Integer, Boolean>{

    protected void onPreExecute() { 

        dialog = new ProgressDialog(mContext); 
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        dialog.setMessage("Loading Tracker...");
        dialog.setCancelable(false);
        dialog.show();
    }

    protected Boolean doInBackground(Void... params) {
        try {
            Thread.sleep(60000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return true;
    }

    protected void onCancelled() {
        if (dialog.isShowing()) dialog.dismiss();
    }

    protected void onPostExecute(Boolean result_ex) {
        if (dialog.isShowing()) dialog.dismiss();
    }
}

What I want to do is run the progress dialog while I'm checking if there is internet connection or not. 我想做的是在检查是否有Internet连接时运行进度对话框。 But I am having the problem that the UI is not refreshed and the progress dialog is not shown like i would like. 但是我有一个问题,UI没有刷新,进度对话框没有显示出来,就像我想要的那样。

The way you do it makes the UI thread occupied by checking for internet connection. 您这样做的方式是通过检查Internet连接来占用UI线程。 This makes the window manager unable to process showing of your ProgressDialog . 这使窗口管理器无法处理ProgressDialog显示。

You should move this check, along with showing progress dialog, to the AsyncTask , like so: 您应该将此检查以及显示进度对话框一起移至AsyncTask ,如下所示:

private void startOnlineCheck() {
    ProgressThread progress_thread = new ProgressThread();
    progress_thread.execute();
}

private class ProgressThread extends AsyncTask<Void, Void, Void>{
    ProgressDialog dialog;

    protected void onPreExecute() { 
        dialog = new ProgressDialog(Activity.this); 
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        dialog.setMessage("Loading Tracker...");
        dialog.setCancelable(false);
        dialog.show();
    }

    protected Void doInBackground(Void... params) {
        ConnectivityManager cm = (ConnectivityManager) Activity.this.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        if (netInfo != null && netInfo.isConnected()) {
            try{
                URL url = new URL("http://www.google.com");
                HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
                urlc.setConnectTimeout(3000);
                urlc.connect();
                // CODE to run when we're online
                return null;
            }catch (MalformedURLException e1) {
                e1.printStackTrace();
            }catch (IOException e) {
                e.printStackTrace();
            }
        }
        Toast.makeText(Activity.this, "Internet Connexion Error", Toast.LENGTH_LONG).show();
        // CODE to run when there's no connection
        return null;
    }

    protected void onPostExecute(Void result) {
        if (dialog.isShowing()) dialog.dismiss();
    }
}

You can also pass a Context in constructor to ProgressThread . 您还可以在构造函数中将Context传递给ProgressThread

Remember that if ProgressThread is an inner class of Activity (which is often the case) you can call any method of that activity from any method of ProgressThread . 请记住,如果ProgressThreadActivity的内部类(通常是这种情况),则可以从ProgressThread任何方法调用该活动的任何方法。

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

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