Android listens to the start and finish events of the webview of Phonegap.

Source: Internet
Author: User

I have previously written an article about how to capture the webview event of Phoengap. It is mainly used to judge the webview loading event in onMessage Based on the message name of the first parameter in the Activity that implements CordovaInterface. However, in Fragment, if an Activity loads multiple fragment containing WebView, it cannot determine the WebView loading event in onMessage.

After trying, we found that we can still use the setWebViewClient method to rewrite the corresponding onPageStart and onPageFinish methods of WebViewClient to handle loading events.


[Java]
CordovaWebView webView = (CordovaWebView) findViewById (R. id. webView );

CordovaWebView webView = (CordovaWebView) findViewById (R. id. webView); [java] view plaincopyprint? CordovaInterface cordovaInterface = (CordovaInterface) context;
 
CordovaWebViewClient cordovaWebViewClient = new CordovaWebViewClient (cordovaInterface, webView ){
@ Override
Public void onPageStarted (WebView view, String url, Bitmap favicon ){
 
LogUtil. debug ("onPageStarted" + url );
Super. onPageStarted (view, url, favicon );
ProgressBar. setVisibility (View. VISIBLE );
}
 
@ Override
Public void onPageFinished (WebView view, String url ){
LogUtil. debug ("onPageFinished" + url );
Super. onPageFinished (view, url );
ProgressBar. setVisibility (View. GONE );
}
 
@ Override
Public void onReceivedError (WebView view, int errorCode, String description, String failingUrl ){
LogUtil. debug ("onReceivedError" + url );
Super. onReceivedError (view, errorCode, description, failingUrl );
ProgressBar. setVisibility (View. GONE );
ErrorView. setVisibility (View. VISIBLE );
}
};
WebView. setWebViewClient (cordovaWebViewClient );
 
WebView. loadUrl (url );

CordovaInterface cordovaInterface = (CordovaInterface) context;

CordovaWebViewClient cordovaWebViewClient = new CordovaWebViewClient (cordovaInterface, webView ){
@ Override
Public void onPageStarted (WebView view, String url, Bitmap favicon ){

LogUtil. debug ("onPageStarted" + url );
Super. onPageStarted (view, url, favicon );
ProgressBar. setVisibility (View. VISIBLE );
}

@ Override
Public void onPageFinished (WebView view, String url ){
LogUtil. debug ("onPageFinished" + url );
Super. onPageFinished (view, url );
ProgressBar. setVisibility (View. GONE );
}

@ Override
Public void onReceivedError (WebView view, int errorCode, String description, String failingUrl ){
LogUtil. debug ("onReceivedError" + url );
Super. onReceivedError (view, errorCode, description, failingUrl );
ProgressBar. setVisibility (View. GONE );
ErrorView. setVisibility (View. VISIBLE );
}
};
WebView. setWebViewClient (cordovaWebViewClient );

WebView. loadUrl (url );

 

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.