简体   繁体   English

防止 WebView 显示“No Internet!”

[英]Prevent WebView from displaying “No Internet!”

I have a web view in my app in android studio.我在 android studio 的应用程序中有一个 web 视图。 I have a web view in my app and I want to make a popup saying "no internet" if there is no internet and a "ok" button on the popup which will recheck if there is no internet and if there is internet it will let the web page load and if not it says "no internet".我的应用程序中有一个网络视图,如果没有互联网,我想制作一个弹出窗口,说“没有互联网”,弹出窗口上的“确定”按钮将重新检查是否没有互联网,如果有互联网,它将让网页加载,如果不是它说“没有互联网”。 I am a beginner programmer so please keep the code simple我是初学者程序员所以请保持代码简单

My Code: (mainactivity)我的代码:(主要活动)

package com.myworldrules.test.testno;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {
    private WebView myWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myWebView = (WebView)findViewById(R.id.myWebView);
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        myWebView.loadUrl("http://space.myworldrules.com/");


        myWebView.setWebViewClient(new WebViewClient());



    }

}

Please feel free to ask any questions请随时提出任何问题

This can be accomplished by accessing the ConnectivityManager and requesting the active network info.这可以通过访问 ConnectivityManager 并请求活动网络信息来完成。 That alone may not always be sufficient so you can additionally determine if a site like google is accessible(in case there is a chance the server you are trying to access is down).仅此一项可能并不总是足够的,因此您可以另外确定是否可以访问像 google 这样的网站(以防您尝试访问的服务器有可能出现故障)。 Please see the link below;请看下面的链接;

Android check internet connection Android 检查互联网连接

检查是否有活动的网络连接,如果不存在,您可以执行以下操作

            webView.loadData("", null, "");

You can check if there is internet available or not before loading url in to webview and您可以在将 url 加载到 webview 之前检查是否有可用的互联网和

if available如果有的话

load url in webview在 webview 中加载 url

else别的

create alert dialog for popup为弹出窗口创建警报对话框

to check internet connection follow this answer here要检查互联网连接,请在此处按照此答案进行操作

只需使用,它应该可以工作:

mWebview.loadDataWithBaseURL(null,htmlContent,"text/html", "utf-8", null);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM