Android-----js and Android Intermodulation

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, this example is not only the operation of JS, it is also a simple introduction to the Android browser,
These I will explain in the note one by one.

The JavaScript pop-up box has the following three kinds:

    1. alert ();
    2. Window.confirm ("is you srue?");
    3. Window.prompt ("Please input some word";,"This is Text");


Three kinds of dialog were captured in Webchromeclient, but unfortunately, there is no callback function to use,
Or, you can't get the result of a user clicking "OK" or "Cancel".
Personally, these methods are set up to be used to test some applications that involve HTML operations.
The following methods are also available in webchromeclient:
    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)

The use of these methods will be described in a later blog post.
Please look at the focus of today's explanation
  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. //Get browser Components
  11. //webview is a simple browser.
  12. //android Browser source code exists in the Linux\android\package\apps\browser
  13. all operations are carried out around the WebView .
  14. Mwebview = (WebView) Findviewbyid (R.id.webview);
  15. Mreusulttext = (TextView) Findviewbyid (R.id.resulttext);
  16. //websettings Almost all of the browser settings are in this class
  17. WebSettings websettings = Mwebview.getsettings ();
  18. Websettings.setsavepassword (false);
  19. Websettings.setsaveformdata (false);
  20. Websettings.setjavascriptenabled (true);
  21. Websettings.setsupportzoom (false);
  22. Mwebview.setwebchromeclient (new Mywebchromeclient ());
  23. /* 
  24. * Demojavascriptinterface class provides interface for JS calling Android server side
  25. * Android as the client interface of the Demojavascriptinterface class is called by JS
  26. * The specific method of invocation is defined in Demojavascriptinterface:
  27. * For example, clickonandroid in this instance
  28. */
  29. Mwebview.addjavascriptinterface (new Demojavascriptinterface (),"Androd");
  30. Mwebview.loadurl ("file:///android_asset/page.html");
  31. }
  32. final class Demojavascriptinterface {
  33. Demojavascriptinterface () {}
  34. /** 
  35. * This method is called by the browser side
  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 Webchromeclient class
  48. * Processing of JS popup time
  49. *
  50. */
  51. final class Mywebchromeclient extends Webchromeclient {
  52. /** 
  53. * Handle Alert Popup 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 package for alert
  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. * Processing Confirm popup 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 popup 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. }
Category: Android good text to the top of my collection this article Oakpip
Follow-10
Fans-40 + plus attention40(Please comment on the article) «Previous: Android-----webview Use
» Next post: Android-----relativelayout Properties

Android-----js and Android Intermodulation

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.