机器人 - 如何prevent的WebView加载的时候没有互联网连接 [英] android - how to prevent webview to load when no internet connection

查看:122
本文介绍了机器人 - 如何prevent的WebView加载的时候没有互联网连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Android应用程序有一个web视图。如果没有互联网连接,web视图会显示网页无法使用。我想使这个看起来像一个应用程序尽可能的,所以我不想显示该页面。我发现一些有用的信息在网上教你如何隐藏或加载别的东西来覆盖它,但我真正想要的是留在当前页面和一个小弹出对话框说没有关系。基本上,当用户点击web视图里的任何东西,检查连接第一。如果没有连接,留在它过去是,弹出一个对话框。

I have an Android app which has a webview. When there's no internet connection, webview will display page not available. I want to make this look like an app as much as possible, so I don't want to display this page. I found some useful info online teaching you how to hide or load something else to cover it, but what I really want is to stay at the current page and a little pop up dialog says no connection. Basically, when user clicks on anything inside the webview, check for connection first. if no connection, stay at where it was and pop out a dialog box.

感谢您的帮助!

编辑:

就像我说的,我已经知道如何在网上查询从样品中的互联网连接。我的问题是我不知道如何停止加载下一页。需要明确的是,当用户试图进入下一个页面,请检查网络连接。如果没有连接,页面将留在地方是,不会进入下一个页面。我希望能够看到他们的最后加载的页面,并得到信息,而网页内容仍然存在用户。谢谢!

Like I said, I already know how to check internet connection from the samples online. My problem is I don't know how to stop loading the next page. To be clear, when users try to go to the next page, check for internet connection. If no connection, the page will stay at where it was and would not go to the next page. I want users able to see their last loaded page and get informed while webpage content is still there. thanks!

推荐答案

我已经用在我的项目如下:

I have used the following in my projects:

DetectConnection.Java

public class DetectConnection {             
  public static boolean checkInternetConnection(Context context) {   

    ConnectivityManager con_manager = (ConnectivityManager) 
      context.getSystemService(Context.CONNECTIVITY_SERVICE);

    if (con_manager.getActiveNetworkInfo() != null
        && con_manager.getActiveNetworkInfo().isAvailable()
        && con_manager.getActiveNetworkInfo().isConnected()) {
      return true;
    } else {
      return false;
    }
  }
}

主要code:

if (!DetectConnection.checkInternetConnection(this)) {
  Toast.makeText(getApplicationContext(), "No Internet!", Toast.LENGTH_SHORT).show();
} else {      
  wv = (WebView) findViewById(R.id.donate_webView1);
  c = new CustomWebViewClient();
  wv.setWebViewClient(c);
  wv.clearCache(true);
  wv.clearHistory();
  wv.getSettings().setJavaScriptEnabled(true);
  wv.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
  wv.getSettings().setBuiltInZoomControls(true);
  wv.loadUrl("http://www.google.com");
}


// Function to load all URLs in same webview
private class CustomWebViewClient extends WebViewClient {
  public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if (!DetectConnection.checkInternetConnection(this)) {
      Toast.makeText(getApplicationContext(), "No Internet!", Toast.LENGTH_SHORT).show();
    } else {
      view.loadUrl(url);
    }     
    return true;
  }
}

这篇关于机器人 - 如何prevent的WebView加载的时候没有互联网连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆