所以我有一个基于 webview 的应用程序。我的问题是,当OnReceivedError
互联网丢失时被调用,我想
当用户单击对话框上的“确定”以检查连接时,
- 如果连接可用,则解除警报并致电
web.reload();
- 如果没有网络连接(wifi 或手机),请致电
finish();
通过以下方式解决:
将警报对话框 Postive 按钮更改为以下内容
.setPositiveButton(R.string.alert_dialog_ok,new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int id) {
问题出在以下
DialogInterface paramDialogInterface,int paramInt)
上下文上下文;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.context = this;
...
@Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
AlertDialog.Builder localBuilder2 = new AlertDialog.Builder(
Webview_Main.this);
localBuilder2.setTitle(R.string.webview_error_received_title);
localBuilder2.setMessage(R.string.webview_error_received);
localBuilder2.setIcon(R.drawable.ic_launcher);
localBuilder2.setPositiveButton(R.string.alert_dialog_ok,new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
if(isNetworkAvailible(context))
web.reload();
else
finish();
}
};
localBuilder2.show();
};
isNetworkAvailible 类
public boolean isNetworkAvailible(Context ctx) {
NetworkInfo info = (NetworkInfo) ((ConnectivityManager) ctx
.getSystemService(Context.CONNECTIVITY_SERVICE))
.getActiveNetworkInfo();
if (info == null || !info.isConnected()) {
return false;
}
if (info.isRoaming()) {
// here is the roaming option you can change it if you want to
// disable internet while roaming, just return false
return false;
}
return true;
}