我正在尝试运行以下代码,但我想在我的进程运行时显示一个进度对话框:
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 没有刷新,并且进度对话框没有像我想要的那样显示。