Android network status gain and WebView loading complete, load failed to listen

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.