In our project, it is often necessary to determine whether the content loaded in the WebView is complete or failed to load, a small application written in the two days involves this.
WebView is the essence of loadurl, so the premise is the device access network, then how to judge it? Look at the code:
Network state Public Boolean isnetworkconnected (context context) {if (context! = null) {Connectivitymanager Mconnectivitymanager = (Connectivitymanager) context.getsystemservice (Context.connectivity_service); Networkinfo mnetworkinfo = Mconnectivitymanager.getactivenetworkinfo (); if (mnetworkinfo! = null) {return Mnetworkinfo.isavailable ();}} return false;
If the network is normal load webview, otherwise prompts the user to access the network.
In the process of using webview, I found that in addition to the current Activity load page, the browser will be opened to load our URLs. Need to be
Wvlast.setwebviewclient (New Webviewclient () {//...}
The following replication is made in:
@Overridepublic Boolean shouldoverrideurlloading (WebView view, String URL) {//TODO auto-generated method stub//return su per.shouldoverrideurlloading (view, URL); view.loadurl (URL); return true;}
Accordingly, the page loading complete and the loading failure is also Google has provided the good, we make the replication we need the operation to be able, also is in the setwebviewclient:
@Overridepublic void onpagefinished (WebView view, String URL) {//TODO auto-generated method stub//super.onpagefinished ( view, URL); Finish ();} @Overridepublic void Onreceivederror (WebView view, int errorcode,string description, String failingurl) {//TODO Auto-gen Erated method Stubsuper.onreceivederror (view, ErrorCode, description, Failingurl); Toast.maketext (Onsave.this, "Synchronization failed, please try again later", Toast.length_short). Show ();
Note that the page load failure is also a loading complete one, that is, the loading progress is also 100, so onreceivederror is indispensable.
Just write it here.
Reprint Please specify source: Zhou Mushi's csdn blog Http://blog.csdn.net/zhoumushui
My github: Zhou Mushi's GitHub Https://github.com/zhoumushui
Android network status gain and WebView loading complete, load failed to listen