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

时间:2016-02-22 06:24:55

标签: android webview

大家好我正在为我的网站创建一个网络视图应用 问题是互联网不可用时webview显示网页不可用

但我想要的是显示消息&一个重新加载按钮,说“您需要连接到互联网才能使用此应用程序按下重新启动互联网后”。提前致谢。

1 个答案:

答案 0 :(得分:0)

只需在打开webView时检查网络连接。

我在这里为您提供相同的方法。

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;

}

我在这里分享相同的方法。

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();

        }

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

相关问题