android - Web page not Avaible Go another class or show an image -
1-)i have webview project.sometimes im forgot internet connect.and showing webpage not available.when if want go class or show error.xml bad displaying. all.
public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.webplayer); customviewcontainer = (framelayout) findviewbyid(r.id.customviewcontainer); webview = (webview) findviewbyid(r.id.webview); adview adview = (adview) this.findviewbyid(r.id.adview); adrequest adrequest = new adrequest.builder().build(); adview.loadad(adrequest); mwebviewclient = new mywebviewclient(); webview.setwebviewclient(mwebviewclient); mwebchromeclient = new mywebchromeclient(); webview.setwebchromeclient(mwebchromeclient); webview.getsettings().setjavascriptenabled(true); webview.getsettings().setappcacheenabled(true); webview.getsettings().setbuiltinzoomcontrols(true); webview.getsettings().setsaveformdata(true); intent = getintent(); string url = i.getstringextra("url"); // retrieve value passed if (savedinstancestate != null) { webview.restorestate(savedinstancestate); } else { webview.loadurl(url); // use value } } public boolean incustomview() { return (mcustomview != null); } public void hidecustomview() { mwebchromeclient.onhidecustomview(); } protected void onpause() { super.onpause(); //to change body of overridden methods use file | settings | file templates. webview.onpause(); } protected void onresume() { super.onresume(); //to change body of overridden methods use file | settings | file templates. webview.onresume(); } @override protected void onstop() { super.onstop(); //to change body of overridden methods use file | settings | file templates. if (incustomview()) { hidecustomview(); } } public boolean onkeydown(int keycode, keyevent event) { if (keycode == keyevent.keycode_back) { if (incustomview()) { hidecustomview(); return true; } if ((mcustomview == null) && webview.cangoback()) { webview.goback(); return true; } } return super.onkeydown(keycode, event); } class mywebchromeclient extends webchromeclient { private bitmap mdefaultvideoposter; private view mvideoprogressview; public void onshowcustomview(view view, int requestedorientation, customviewcallback callback) { onshowcustomview(view, callback); //to change body of overridden methods use file | settings | file templates. } public void onshowcustomview(view view,customviewcallback callback) { // if view exists terminate new 1 if (mcustomview != null) { callback.oncustomviewhidden(); return; } mcustomview = view; webview.setvisibility(view.gone); customviewcontainer.setvisibility(view.visible); customviewcontainer.addview(view); customviewcallback = callback; } public view getvideoloadingprogressview() { if (mvideoprogressview == null) { layoutinflater inflater = layoutinflater.from(webplayer.this); mvideoprogressview = inflater.inflate(r.layout.video_progress, null); } return mvideoprogressview; } @override public void onhidecustomview() { super.onhidecustomview(); //to change body of overridden methods use file | settings | file templates. if (mcustomview == null) return; webview.setvisibility(view.visible); customviewcontainer.setvisibility(view.gone); // hide custom view. mcustomview.setvisibility(view.gone); // remove custom view container. customviewcontainer.removeview(mcustomview); customviewcallback.oncustomviewhidden(); mcustomview = null; } } @override public void onhidecustomview() { super.onhidecustomview(); //to change body of overridden methods use file | settings | file templates. if (mcustomview == null) return; webview.setvisibility(view.visible); customviewcontainer.setvisibility(view.gone); // hide custom view. mcustomview.setvisibility(view.gone); // remove custom view container. customviewcontainer.removeview(mcustomview); customviewcallback.oncustomviewhidden(); mcustomview = null; } } class mywebviewclient extends webviewclient { @override public boolean shouldoverrideurlloading(webview view, string url) { return super.shouldoverrideurlloading(view, url); //to change body of overridden methods use file | settings | file templates. }
this easy
you can check internet conncection before requesting url
final connectivitymanager connmgr = (connectivitymanager) yourclass.this .getsystemservice(context.connectivity_service); final android.net.networkinfo wifi = connmgr .getnetworkinfo(connectivitymanager.type_wifi); final android.net.networkinfo mobile = connmgr .getnetworkinfo(connectivitymanager.type_mobile); if (wifi.isconnected() || mobile.isconnected()) { webview.loadurl(url); }else{ setcontentview(r.layout.your); }
//required permissions
add these permissions manifest file
<uses-permission android:name="android.permission.access_wifi_state" /> <uses-permission android:name="android.permission.access_network_state" />
for more contect me @ rajeshsaini890 @ gmail
Comments
Post a Comment