如何在WebView Android中使用“重新加载”按钮添加自定义错误消息?
大家好,我正在为我的网站创建一个web视图应用程序 问题是,当internet不可用时,webview会显示网页不可用如何在WebView Android中使用“重新加载”按钮添加自定义错误消息?,android,webview,Android,Webview,大家好,我正在为我的网站创建一个web视图应用程序 问题是,当internet不可用时,webview会显示网页不可用 但我想要的是显示一条消息&一个重新加载按钮,上面写着“你需要连接到互联网才能使用这个应用,打开互联网后按重新加载”。提前谢谢 只需在打开webView时检查网络连接即可 在这里,我为您提供了同样的方法 public boolean isConnectingToInternet() { ConnectivityManager cm = (ConnectivityManag
但我想要的是显示一条消息&一个重新加载按钮,上面写着“你需要连接到互联网才能使用这个应用,打开互联网后按重新加载”。提前谢谢 只需在打开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();
}
在这里我分享同样的方法
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(“您需要连接到internet才能使用此应用程序,请在打开internet后按重新加载”)
webview.loadUrl(“关于:空白”);但是如何显示警告(“您需要连接到internet才能使用此应用程序,请在打开internet后按重新加载”)&重新加载按钮?