繁体   English   中英

没有互联网显示对话框

[英]No internet display dialog

我有一个应用程序,它有两种类型的“没有互联网错误”。 当您启动该应用程序时,它会查看您是否有互联网并显示错误No Internet ,并且我在我的 webviews 中有一个自定义页面的loadURL

我想隐藏webview网站,但是使用自定义页面,需要一点时间,你可以看到错误+网站。

有可能为我的需要做点什么吗?

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view=inflater.inflate(R.layout. fragment_paginainicio, container, false);
        final WebView mWebView = (WebView) view.findViewById(R.id.webView_websitepage);
        mWebView.loadUrl("https://google.pt");
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.setWebViewClient(new WebViewClient() {

            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {

                mWebView.loadUrl("file:///android_asset/errorpage.html");

            }

        });

You have to check if internet is available. Below code will help in that case. If it is not available you can simple make webview visiblity to gone or do not load anything and show alert dialog for no network 

public static boolean isNetworkAvailable(Context mContext) {

                ConnectivityManager connectivityManager
                        = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
                NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
                return activeNetworkInfo != null && activeNetworkInfo.isConnected();

        }

这个没有互联网检测有点棘手。 在我了解到这一点之前,我一直在挣扎:

在某些情况下,您已连接到路由器或您的数据连接已打开,但实际上您没有收到任何包裹返回。 由于某种原因,互联网提供商可能已关闭,或者您没有支付账单,他们切断了您的联系。

有一个简单的示例说明我如何检查是否有 Internet。

public boolean isInternetStable() {
    try {
        return new getInternetStabilityStatus().execute().get();
    } catch (InterruptedException e) {
        return false;
    } catch (ExecutionException e) {
        return false;
    }
}
public class getInternetStabilityStatus extends AsyncTask<Void, Void, Boolean> {

    @Override
    protected Boolean doInBackground(Void... params) {
        try {
            InetAddress ipAddr = InetAddress.getByName("google.com"); //You can replace it with your name
            return !ipAddr.equals("");

        } catch (Exception e) {
            return false;
        }
    }

    @Override
    protected void onPostExecute(Boolean result) {
        super.onPostExecute(result);
    }

}

暂无
暂无

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

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