android - 如何在没有互联网连接时阻止加载 webview [英] android - how to prevent webview to load when no internet connection

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

问题描述

我有一个带有 webview 的 Android 应用程序.当没有互联网连接时,webview 将显示页面不可用.我想让它看起来尽可能像一个应用程序,所以我不想显示这个页面.我在网上找到了一些有用的信息,教你如何隐藏或加载其他东西来覆盖它,但我真正想要的是留在当前页面,一个小的弹出对话框说没有连接.基本上,当用户点击 webview 中的任何东西时,首先检查连接.如果没有连接,就停留在原处,弹出一个对话框.

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

import android.content.Context;
import android.net.ConnectivityManager;


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

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

    return (con_manager.getActiveNetworkInfo() != null
        && con_manager.getActiveNetworkInfo().isAvailable()
        && con_manager.getActiveNetworkInfo().isConnected());
  }
}

主要代码:

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

更新清单:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

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

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