我正在尝试使用Asynctask首先检查网站是否可访问,然后打开它。问题是在网站完全加载之前关闭了进度对话框。
public class MainActivity extends Activity {
private final static String TAG = "MusicFXActivityBrowser";
/** Called when the activity is first created. */
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void readWebpage(View v){
final Intent intent = getIntent();
final String urlString = intent.getDataString();
try {
final WebView webView = new WebView(this);
final URL url = new URL("http://india-aviation.in/users/userregister/Registration.html");
new LoadURLTask(webView).execute(url);
} catch (final MalformedURLException e) {
Log.e(TAG, "MalformedURLException " + urlString);
}
}
/**
* ASync Task that checks if the site is reachable and then loads the URL.
*/
private class LoadURLTask extends AsyncTask<URL, Void, Boolean> {
final WebView mWebView;
String mURLString;
// Experiment STARTS ///////////////////////////////////////////////////////////////////////////////////////////
ProgressDialog dialog = ProgressDialog.show(MainActivity.this, "", "Please Wait...");
// Experiment ENDS ///////////////////////////////////////////////////////////////////////////////////////////
public LoadURLTask(final WebView webView) {
mWebView = webView;
}
/*
* (non-Javadoc)
*
* @see android.os.AsyncTask#doInBackground(Params[])
*/
@Override
protected Boolean doInBackground(final URL... urls) {
final URL url = urls[0];
mURLString = url.toString();
boolean isSiteReachable = false;
final HttpURLConnection httpURLConnection;
try {
httpURLConnection = (HttpURLConnection) url.openConnection();
final int response = httpURLConnection.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
isSiteReachable = true;
}
} catch (final IOException e) {
Log.e(TAG, "Error connecting to " + mURLString);
}
return isSiteReachable;
}
/*
* (non-Javadoc)
*
* @see android.os.AsyncTask#onPostExecute(java.lang.Object)
*/
@Override
protected void onPostExecute(final Boolean isReachable) {
if (isReachable) {
dialog.dismiss();
setContentView(mWebView);
mWebView.loadUrl(mURLString);
} else {
createAndShowNetworkDialog();
}
}
}
/**
* Shows the network dialog alerting the user that the net is down.
*/
private void createAndShowNetworkDialog() {
new AlertDialog.Builder(this).setTitle(R.string.browser_error_dialog_title)
.setMessage(R.string.browser_error_dialog_text)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(final DialogInterface dialog, final int which) {
finish();
}
}).show();
}
}
答案 0 :(得分:-1)
在加载网站之前,对话框正在关闭,因为您在3.xml
UPDATE TableB
SET recordOwner = (SELECT TOP(1) [name]
FROM TableC
ORDER BY NEWID())
WHERE recordOwner IS NULL
dismiss()
我建议备用方式您可以先显示网站,当网站在屏幕上启动时,等待2秒钟然后关闭对话框。如果网站启动延迟且您的应用程序 UI将提供正确的行为
,此方法不会失败