java - Checking Network Connectivity before dismissing Alert Dialog - RESOLVED -
so have webview based app. question when onreceivederror
called when there internet loss, i'd
when user clicks "ok" on dialog check connectivity,
- if connectivity available dismiss alert , call
web.reload();
- if there isn't web connectivity (wifi or mobile) call
finish();
resolved following:
changed alert dialog postive button following
.setpositivebutton(r.string.alert_dialog_ok,new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog,int id) {
the issue following
dialoginterface paramdialoginterface,int paramint)
context context;
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 class
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 roaming option can change if want // disable internet while roaming, return false return false; } return true; }
correcting answer:
context context; @override protected 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 paramdialoginterface,int paramint) { if(haveinternet(context)) web.reload(); else finish(); } }); localbuilder2.show(); }; public boolean haveinternet(context ctx) { networkinfo info = (networkinfo) ((connectivitymanager) ctx .getsystemservice(context.connectivity_service)).getactivenetworkinfo(); if (info == null || !info.isconnected()) { return false; } if (info.isroaming()) { // here roaming option can change if want // disable internet while roaming, return false return false; } return true; }
Comments
Post a Comment