简体   繁体   English

如何在WebView Android中使用“重新加载”按钮添加自定义错误消息?

[英]How to add custom error message with reload button in WebView Android?

Hi guys i am creating a web view app for my site the problem is whenever the internet is not available webview shows web page not available 嗨,大家好,我正在为我的网站创建一个网络视图应用程序,问题是每当互联网不可用时,网络视图显示网页不可用

But what i want is to Display a Message & a reload button saying 'You need to be connected to internet to use this app press reload after turning on internet' . 但是我想要的是显示一条消息和一个重新加载按钮,说“您需要连接到互联网才能使用此应用程序,然后在打开互联网后按重新加载”。 Thanks in advance. 提前致谢。

Simply check the net connectivity on opening the webView. 只需在打开webView时检查网络连接即可。

Here I am providing you the method for the same. 在这里,我为您提供了相同的方法。

public boolean isConnectingToInternet() {
    ConnectivityManager cm = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    @SuppressWarnings("deprecation")
    NetworkInfo wifiNetwork = cm
            .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (wifiNetwork != null && wifiNetwork.isConnected()) {
        return true;
    }

    @SuppressWarnings("deprecation")
    NetworkInfo mobileNetwork = cm
            .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    if (mobileNetwork != null && mobileNetwork.isConnected()) {
        return true;
    }

    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    if (activeNetwork != null && activeNetwork.isConnected()) {
        return true;
    }

    return false;

}

Here I am sharing the method for the same. 在这里,我将分享相同的方法。

public void showAlert(String msg) {

          ContextThemeWrapper themedContext;
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
           themedContext = new ContextThemeWrapper(context,android.R.style.Theme_Holo_Light_Dialog_NoActionBar);
          } else {
           themedContext = new ContextThemeWrapper(
             context,
             android.R.style.Theme_Light_NoTitleBar);
          }

            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                    themedContext);

            alertDialogBuilder.setTitle(context.getResources().getString(R.string.app_name));
            alertDialogBuilder.setCancelable(false);
            alertDialogBuilder.setMessage(msg).setPositiveButton("Reload",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                                //Call webview again.
                                dialog.cancel();

                        }
                    });
            AlertDialog alertDialog = alertDialogBuilder.create();
            alertDialog.show();

        }

call this method showAlert("You need to be connected to internet to use this app press reload after turning on internet") 调用此方法showAlert("You need to be connected to internet to use this app press reload after turning on internet")

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

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