如果没有互联网应用程序强行停止
webview = (WebView) findViewById(R.id.webView);
final Activity activity = this;
webview.getSettings().setJavaScriptEnabled(true);
webview.canGoBack();
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
public void onLoadResource(WebView view, String url) {
// Check to see if there is a progress dialog
if (progressDialog == null) {
// If no progress dialog, make one and set message
progressDialog = new ProgressDialog(activity);
progressDialog.setMessage("Loading please wait...");
progressDialog.show();
// Hide the webview while loading
webview.setEnabled(false);
}
}
public void onPageFinished(WebView view, String url) {
// Page is done loading;
// hide the progress dialog and show the webview
if (progressDialog.isShowing()) {
progressDialog.dismiss();
progressDialog = null;
webview.setEnabled(true);
}
}
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error){
//Your code to do
Toast.makeText(getApplicationContext(), "Your Internet Connection May not be active Or " + error, Toast.LENGTH_LONG).show();
}
});
// The URL that webview is loading
webview.loadUrl("http://www.google.com");
}
答案 0 :(得分:2)
在您的课程中添加此方法
protected boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnected()) {
return true;
} else {
return false;
}
}
如果您想了解互联网连接状态,请使用以下代码
if(isOnline()==true)
{
//Internet Connected! //do your action here
}
else
{
// "No Internet!
}
希望它会有所帮助!
答案 1 :(得分:0)
使用代码检查互联网连接
ConnectivityManager conMgr = ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
{
NetworkInfo netInfo = conMgr.getActiveNetworkInfo();
if (netInfo == null)
{
Description.setVisibility(View.INVISIBLE);
new AlertDialog.Builder(WelcomePage.this)
.setTitle(getResources().getString(R.string.app_name))
.setMessage(
getResources().getString(
R.string.internet_error))
.setPositiveButton("OK", null).show();
}
else
{
dialog = ProgressDialog.show(WelcomePage.this, "", "Loading...", true,
false);
new Welcome_Page().execute();
}
}