Android-interaction between JS and Android

Source: Internet
Author: User
Http://code.google.com/p/apps-for-android/source/browse/trunk/Samples/WebViewDemo/

I modified it and added a simple comment. In this example, not only JS operations are performed, but Android browsers are also briefly introduced,
I will explain these in the annotations.

The javascript pop-up box contains the following three types:

Java code  
  1. Alert ();
  2. Window. Confirm ("are you srue? ");
  3. Window. Prompt ("Please input some word";, "this is text ");

Three dialog s are captured in webchromeclient, but unfortunately, no callback function is available,
Or you cannot obtain the operation result of clicking "OK" or "cancel.
I personally think that these methods are used to test some applications involving HTML operations.
The following methods are available in webchromeclient: Java code 

  1. Onprogresschanged (webview view, int newprogress );
  2. Onreceivedicon (webview view, bitmap icon );
  3. Onreceivedtitle (webview view, String title );
  4. Onrequestfocus (webview view );
  5. Onclosewindow (webview window );
  6. Onprogresschanged (webview view, int newprogress)

I will discuss the use of these methods in a later blog.
Let's take a look at the key JAVA code explained today. 

  1. Public class webviewdemo extends activity {
  2. Private Static final string log_tag = "webviewdemo ";
  3. Private webview mwebview;
  4. Private textview mreusulttext;
  5. Private handler mhandler = new handler ();
  6. @ Override
  7. Public void oncreate (bundle icicle ){
  8. Super. oncreate (icicle );
  9. Setcontentview (R. layout. Main );
  10. // Obtain browser Components
  11. // Webview is a simple browser
  12. // The Android browser source code is stored in Linux \ Android \ package \ apps \ Browser
  13. // All operations in it are carried out around webview
  14. Mwebview = (webview) findviewbyid (R. Id. webview );
  15. Mreusulttext = (textview) findviewbyid (R. Id. resulttext );
  16. // Websettings almost all browser settings are in this class
  17. Websettings = mwebview. getsettings ();
  18. Websettings. setsavepassword (false );
  19. Websettings. setsaveformdata (false );
  20. Websettings. setjavascriptenabled (true );
  21. Websettings. setsuppzoom zoom (false );
  22. Mwebview. setwebchromeclient (New mywebchromeclient ());
  23. /*
  24. * The demojavascriptinterface class provides interfaces for js to call the android server.
  25. * Android is called by JS as the client interface of the demojavascriptinterface class.
  26. * The call method is defined in demojavascriptinterface:
  27. * For example, clickonandroid
  28. */
  29. Mwebview. addjavascriptinterface (New demojavascriptinterface (), "androd ");
  30. Mwebview. loadurl ("file: // android_asset/page.html ");
  31. }
  32. Final class demow.criptinterface {
  33. Demow.criptinterface (){}
  34. /**
  35. * This method is called by the browser.
  36. */
  37. Public void clickonandroid (){
  38. Mhandler. Post (New runnable (){
  39. Public void run (){
  40. // Call the onjsandroid method in JS
  41. Mwebview. loadurl ("javascript: onjsandroid ()");
  42. }
  43. });
  44. }
  45. }
  46. /**
  47. * Inherit the webchromeclient class
  48. * Process the JS pop-up box time
  49. *
  50. */
  51. Final class mywebchromeclient extends webchromeclient {
  52. /**
  53. * Processing the alert pop-up box
  54. */
  55. @ Override
  56. Public Boolean onjsalert (webview view, string URL,
  57. String message, jsresult result ){
  58. Log. D (log_tag, "onjsalert:" + message ");
  59. Mreusulttext. settext ("alert:" + message );
  60. // Simple alert Encapsulation
  61. New alertdialog. Builder (webviewdemo. This ).
  62. Settitle ("alert"). setmessage (Message). setpositivebutton ("OK ",
  63. New dialoginterface. onclicklistener (){
  64. @ Override
  65. Public void onclick (dialoginterface arg0, int arg1 ){
  66. // Todo
  67. }
  68. }). Create (). Show ();
  69. Result. Confirm ();
  70. Return true;
  71. }
  72. /**
  73. * Handling the confirm pop-up box
  74. */
  75. @ Override
  76. Public Boolean onjsconfirm (webview view, string URL, string message,
  77. Jsresult result ){
  78. Log. D (log_tag, "onjsconfirm:" + message );
  79. Mreusulttext. settext ("confirm:" + message );
  80. Result. Confirm ();
  81. Return super. onjsconfirm (view, URL, message, result );
  82. }
  83. /**
  84. * Processing prompt pop-up box
  85. */
  86. @ Override
  87. Public Boolean onjsprompt (webview view, string URL, string message,
  88. String defaultvalue, jspromptresult result ){
  89. Log. D (log_tag, "onjsprompt:" + message );
  90. Mreusulttext. settext ("prompt input is:" + message );
  91. Result. Confirm ();
  92. Return super. onjsprompt (view, URL, message, message, result );
  93. }
  94. }
  95. }
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.