Capturing JavaScript events in WebView of Android Development

Source: Internet
Author: User

A few days ago, I encountered a BUG in the project:

We used WebView In the Android project to open an online banking payment url. When we click "pay", they will pop up an inquiry box using js on the webpage. The js Code is as follows:

 
 
  1. function testConfirm() {  
  2.     if(confirm("pay or not?")) {  
  3.       alert("yes! i do");  
  4.     }  
  5.     else 
  6.     {  
  7.       alert("no!!!");  
  8.     }  

If I didn't process the js event in webView, the process could not be correctly executed.

After checking the api, the solution is as follows:

First, set the webView attributes:

 
 
  1. mWebView.getSettings().setJavaScriptEnabled(true); mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true); 

Second, set WebChromeClient:

 
 
  1. MWebView. setWebChromeClient (new WebChromeClient (){
  2.  
  3. @ Override
  4. Public boolean onJsAlert (WebView view, String url, String message,
  5. Final JsResult result ){
  6. AlertDialog. Builder builder = new AlertDialog. Builder (mContext );
  7. Builder. setMessage (message)
  8. . SetNeutralButton ("OK", new OnClickListener (){
  9. @ Override
  10. Public void onClick (DialogInterface arg0, int arg1 ){
  11. Arg0.dismiss ();
  12. }
  13. }). Show ();
  14. Result. cancel ();
  15. Return true;
  16. }
  17.  
  18. @ Override
  19. Public boolean onJsConfirm (WebView view, String url,
  20. String message, final JsResult result ){
  21. // TODO Auto-generated method stub
  22. Log. I (TAG, "onJsConfirm" + "," + "url:" + url );
  23.  
  24. DialogUtils. dialogBuilder (mContext, "tip", message,
  25. New DialogCallBack (){
  26.  
  27. @ Override
  28. Public void onCompate (){
  29. Log. I (TAG, "onJsConfirm, onCompate ");
  30. Result. confirm ();
  31. }
  32.  
  33. @ Override
  34. Public void onCancel (){
  35. Log. I (TAG, "onJsConfirm, onCancel ");
  36. Result. cancel ();
  37. }
  38. });
  39. Return true;
  40. }
  41. });

In this way, the alert () and confirm () Events of javascript can be captured in webView.

Url for test: http://liucundong.sinaapp.com/testWebView.php

Related Article

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.