0

我正在尝试运行以下代码,但我想在我的进程运行时显示一个进度对话框:

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();
    }
}

我想要做的是在检查是否有互联网连接时运行进度对话框。但我遇到的问题是 UI 没有刷新,并且进度对话框没有像我想要的那样显示。

4

1 回答 1

0

你这样做的方式通过检查互联网连接来占用 UI 线程。这使得窗口管理器无法处理您的ProgressDialog.

您应该将此检查连同显示进度对话框一起移至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();
    }
}

您还可以将Contextin 构造函数传递给ProgressThread.

请记住,如果ProgressThread是 的内部类Activity(通常是这种情况),您可以从 的任何方法调用该活动的任何方法ProgressThread

于 2012-12-23T00:58:15.130 回答