现在很多的手机应用,都可能会直接嵌入一个web页面。这样做的好处:一个是功能更新方便,维护起来容易,只需要维护服务器的页面即可,不需要更新客户端;另一个是功能通用,不仅Android可以用,iOS也可

  1. <pre name="code" class="java" style="font-family: Arial, Helvetica, simsun, u5b8bu4f53; "><span style="font-size:16px;">public class TestWebView extends Activity {  
  2.     private WebView mWebView;  
  3.   
  4.     @Override  
  5.     public void onCreate(Bundle savedInstanceState) {  
  6.         super.onCreate(savedInstanceState);  
  7.         setContentView(R.layout.main);  
  8.         mWebView = (WebView) findViewById(R.id.htmlview);  
  9.   
  10.         WebSettings webSettings = mWebView.getSettings();  
  11.           
  12.         // 是否允许在webview中执行javascript  
  13.         webSettings.setJavaScriptEnabled(true);  
  14.   
  15.         // 绑定java对象到JavaScript中,这样就能在JavaScript中调用java对象,实现通信。  
  16.         // 这种方法第一个参数就是java对象,第二个参数表示java对象的别名,在JavaScript中使用  
  17.          mWebView.addJavascriptInterface(new DemoJavaScriptInterface(), "demo");  
  18.   
  19.          // webview加载本地html代码,注意本地html代码必须放在工程assets目录下,然后通过  
  20.         // file:///android_asset/demo.html访问  
  21.         mWebView.loadUrl("file:///android_asset/demo.html");  
  22.     }  
  23.       
  24.     public class DemoJavaScriptInterface {  
  25.         public DemoJavaScriptInterface() {  
  26.   
  27.         }  
  28.   
  29.         public int mydata() {  
  30.             Log.i("TEST","mydata.....");  
  31.             return 0;  
  32.         }  
  33.     }  
  34. }</span></pre>  
  35. <pre style="font-family:Arial,Helvetica,simsun,u5b8bu4f53"></pre>  
  36. <p style="font-family:Arial,Helvetica,simsun,u5b8bu4f53"></p>  
  37. <pre style="font-family:Arial,Helvetica,simsun,u5b8bu4f53"></pre>  
  38. <p style="font-family:Arial,Helvetica,simsun,u5b8bu4f53"></p>  
  39. <p style="font-family:Arial,Helvetica,simsun,u5b8bu4f53"><span style="font-size:16px">3、修改 main.xml 文件如下</span></p>  
  40. <p style="font-family:Arial,Helvetica,simsun,u5b8bu4f53"></p>  
  41. <pre name="code" class="html" style="font-family: Arial, Helvetica, simsun, u5b8bu4f53; "><span style="font-size:16px;"><?xml version="1.0" encoding="utf-8"?>  
  42. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  43.     android:layout_width="fill_parent" android:layout_height="fill_parent"  
  44.     >  
  45.   
  46.     <WebView android:id="@+id/htmlview"  
  47.         android:layout_centerHorizontal="true" android:layout_centerVertical="true"  
  48.         android:layout_marginLeft="0px" android:layout_width="fill_parent"  
  49.         android:layout_height="fill_parent" />  
  50.   
  51. </RelativeLayout></span></pre><span style="font-size:16px; font-family:Arial,Helvetica,simsun,u5b8bu4f53"><br>  
  52. 4、<span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53; line-height:25px">在assets目录下,新建一个html文件:demo.html,使用JAVAScript代码编写</span></span>  
  53. <p style="font-family:Arial,Helvetica,simsun,u5b8bu4f53"></p>  
  54. <p style="font-family:Arial,Helvetica,simsun,u5b8bu4f53"><span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53; line-height:25px"><span style="font-size:16px"></span></span></p>  
  55. <pre name="code" class="javascript" style="font-family: Arial, Helvetica, simsun, u5b8bu4f53; "><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">   
  56. <html>   
  57. <head>   
  58. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">   
  59. <title>Insert title here</title>   
  60. </head>   
  61.     <body>this is web html  
  62.     <div id="output" >test</div>  
  63.         <input type="submit" value="buttons"   
  64.         οnclick="document.getElementById('output').innerHTML=window.demo.mydata()"/>   
  65.     </body>   
  66. </html>  
  67. </pre><span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53"><br>  
  68. 注意上面 </span><span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53; line-height:25px"></span><pre name="code" class="javascript" style="margin-top: 4px; margin-right: 0px; margin-bottom: 4px; margin-left: 0px; background-color: rgb(240, 240, 240); font-family: Arial, Helvetica, simsun, u5b8bu4f53; ">οnclick="document.getElementById('output').innerHTML=window.demo.mydata()"</pre>  
  69. <p style="font-family:Arial,Helvetica,simsun,u5b8bu4f53"></p>  
  70. <p style="font-family:Arial,Helvetica,simsun,u5b8bu4f53"><span style="font-size:16px">直接调用 window.demo.mydata() 的方法</span></p>  
  71. <p style="font-family:Arial,Helvetica,simsun,u5b8bu4f53"><span style="font-size:16px"><br>  
  72. </span></p>  
  73. <p style="font-family:Arial,Helvetica,simsun,u5b8bu4f53"><span style="font-size:16px">ok, 轻松就将代码写完,现在补充说明一下用到的知识点:</span></p>  
  74. <p style="font-family:Arial,Helvetica,simsun,u5b8bu4f53"><span style="font-size:16px">先看SDK API函数介绍:</span></p>  
  75. <p style="font-family:Arial,Helvetica,simsun,u5b8bu4f53"><span style="font-size:16px"><span style="font-family:arial,sans-serif; font-size:13px; color:rgb(51,51,51)"></span></span></p>  
  76. <h4 class="jd-details-title" style="margin-top:1.25em; margin-right:0px; margin-bottom:0.65em; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; font-size:1.1em; background-color:rgb(226,226,226); color:rgb(58,58,58); font-family:Arial,Helvetica,simsun,u5b8bu4f53"><a name="t0"></a>  
  77. <span class="normal" style="font-size:0.9em; font-weight:normal">public void </span><span class="sympad" style="margin-right:2px">addJavascriptInterface</span> <span class="normal" style="font-size:0.9em; font-weight:normal">(<a href="http://developer.android.com/reference/java/lang/Object.html" style="color:rgb(0,102,153)">Object</a> obj, <a href="http://developer.android.com/reference/java/lang/String.html" style="color:rgb(0,102,153)">String</a> interfaceName)</span></h4>  
  78. <p style="font-family:Arial,Helvetica,simsun,u5b8bu4f53"></p>  
  79. <p style="font-family:Arial,Helvetica,simsun,u5b8bu4f53"><span style="font-size:16px"></span></p>  
  80. <div class="jd-tagdata jd-tagdescr" style="margin-top:0.25em; margin-right:0px; margin-bottom:0.75em; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; line-height:1em; font-family:Arial,Helvetica,simsun,u5b8bu4f53">  
  81. <p style="margin-top:0.5em; margin-right:0px; margin-bottom:0.5em; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; line-height:1.3em">  
  82. Use this function to bind an object to JavaScript so that the methods can be accessed from JavaScript.</p>  
  83. <p style="margin-top:0.5em; margin-right:0px; margin-bottom:0.5em; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; line-height:1.3em">  
  84. <strong>IMPORTANT:</strong></p>  
  85. <ul style="margin-top:0px; margin-right:2.5em; margin-bottom:0px; margin-left:2.5em; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; line-height:1.3em">  
  86. <li style="margin-top:0px; margin-right:0px; margin-bottom:0.25em; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; line-height:1.3em">  
  87. Using addJavascriptInterface() allows JavaScript to control your application. This can be a very useful feature or a dangerous security issue. When the HTML in the WebView is untrustworthy (for example, part or all of the HTML is provided by some person or  
  88.  process), then an attacker could inject HTML that will execute your code and possibly any code of the attacker's choosing.<br>  
  89. Do not use addJavascriptInterface() unless all of the HTML in this WebView was written by you.</li><li style="margin-top:0px; margin-right:0px; margin-bottom:0.25em; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; line-height:1.3em">  
  90. The Java object that is bound runs in another thread and not in the thread that it was constructed in.</li></ul>  
  91. <p style="margin-top:0.5em; margin-right:0px; margin-bottom:0.5em; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; line-height:1.3em">  
  92. </p>  
  93. <p style="margin-top:0.5em; margin-right:0px; margin-bottom:0.5em; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; line-height:1.3em">  
  94. </p>  
  95. </div>  
  96. <div class="jd-tagdata" style="margin-top:0.5em; margin-right:1em; margin-bottom:0.5em; margin-left:1em; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; font-family:Arial,Helvetica,simsun,u5b8bu4f53">  
  97. <h5 class="jd-tagtitle" style="margin-top:1em; margin-right:0px; margin-bottom:0.65em; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; font-size:1em; color:rgb(58,58,58)"><a name="t1"></a>  
  98. Parameters</h5>  
  99. <table class="jd-tagtable          " style="margin-top:0px; margin-right:0px; margin-bottom:1em; margin-left:1em; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial; font-size:1em; border-collapse:collapse; empty-cells:show">  
  100. <tbody style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial">  
  101. <tr style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial">  
  102. <th style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:10px; padding-bottom:2px; padding-left:10px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-style:initial; border-color:initial; border-top-style:none; border-right-style:none; border-bottom-style:none; border-left-style:none; text-align:left; vertical-align:top; background-color:rgb(255,255,255); border-width:initial; border-color:initial; font-weight:normal; font-style:italic">  
  103. obj</th>  
  104. <td style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:10px; padding-bottom:2px; padding-left:10px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-style:initial; border-color:initial; border-top-style:none; border-right-style:none; border-bottom-style:none; border-left-style:none; text-align:left; vertical-align:top; background-color:rgb(255,255,255); border-width:initial; border-color:initial; font-weight:normal">  
  105. The class instance to bind to JavaScript, null instances are ignored.</td>  
  106. </tr>  
  107. <tr style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-top-width:0px; border-right-width:0px; border-bottom-width:0px; border-left-width:0px; border-style:initial; border-color:initial">  
  108. <th style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:10px; padding-bottom:2px; padding-left:10px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-style:initial; border-color:initial; border-top-style:none; border-right-style:none; border-bottom-style:none; border-left-style:none; text-align:left; vertical-align:top; background-color:rgb(255,255,255); border-width:initial; border-color:initial; font-weight:normal; font-style:italic">  
  109. interfaceName</th>  
  110. <td style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:2px; padding-right:10px; padding-bottom:2px; padding-left:10px; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-style:initial; border-color:initial; border-top-style:none; border-right-style:none; border-bottom-style:none; border-left-style:none; text-align:left; vertical-align:top; background-color:rgb(255,255,255); border-width:initial; border-color:initial; font-weight:normal">  
  111. The name to used to expose the instance in JavaScript.</td>  
  112. </tr>  
  113. </tbody>  
  114. </table>  
  115. </div>  
  116. <span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53"><br>  
  117. </span>  
  118. <p style="font-family:Arial,Helvetica,simsun,u5b8bu4f53"></p>  
  119. <p style="font-family:Arial,Helvetica,simsun,u5b8bu4f53"><span style="font-size:16px"><span style="color:#006600">通过<span style="font-family:verdana,sans-serif; font-size:16px; line-height:28px">addJavascriptInterface(Object obj,String interfaceName)这个方法,该方法将一个java对象绑定到一个javascript对象中,javascript对象名就是  
  120.  interfaceName(demo),作用域是Global。这样初始化webview后,在webview加载的页面中就可以直接通过 javascript:window.demo访问到绑定的java对象了。</span></span></span></p>  
  121. <p style="font-family:Arial,Helvetica,simsun,u5b8bu4f53"><span style="font-family:verdana,sans-serif; font-size:16px; color:#333333"><span style="line-height:28px"><br>  
  122. </span></span></p>  
  123. <p style="font-family:Arial,Helvetica,simsun,u5b8bu4f53"><span style="font-family:verdana,sans-serif; color:rgb(51,51,51)"><span style="line-height:28px"><span style="font-size:16px">再补充下<span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53; color:rgb(75,78,81); line-height:25px">在开发过程中应该注意几点:</span></span></span></span></p>  
  124. <p style="font-family:Arial,Helvetica,simsun,u5b8bu4f53"><span style="font-family:verdana,sans-serif; color:rgb(51,51,51)"><span style="line-height:28px"><span style="font-size:16px">1、<span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53; color:rgb(75,78,81); line-height:25px">AndroidManifest.xml中必须使用许可"android.permission.INTERNET",否则会出Web  
  125.  page not available错误</span></span></span></span></p>  
  126. <p style="font-family:Arial,Helvetica,simsun,u5b8bu4f53"><span style="font-family:verdana,sans-serif"><span style="line-height:28px"><span style="font-size:16px"><span style="color:#333333">2、</span><span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53; line-height:25px"><span style="color:#006600">如果访问的页面中有Javascript,则webview必须设置支持Javascript。<br style="line-height:25px">  
  127. webview.getSettings().setJavaScriptEnabled(true); </span></span></span></span></span></p>  
  128. <p style="font-family:Arial,Helvetica,simsun,u5b8bu4f53"><span style="font-family:verdana,sans-serif; color:rgb(51,51,51)"><span style="line-height:28px"><span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53; color:rgb(75,78,81); line-height:25px"><span style="font-size:16px">3、<span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53; color:rgb(75,78,81); line-height:25px">如果页面中链接,如果希望点击链接继续在当前browser中响应,而不是新开Android的系统browser中响应该链接,必须覆盖webview的WebViewClient对象。</span></span></span></span></span></p>  
  129. <p><span style="font-size:16px"></span><span style="font-size:16px"><span style="white-space:pre; line-height:25px"><span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53; color:#4B4E51"><span style="white-space:pre"></span></span></span>mWebView.setWebViewClient(new  
  130.  WebViewClient(){</span></p>  
  131. <pre name="code" class="java" style="font-family: Arial, Helvetica, simsun, u5b8bu4f53; "><span style="font-size:16px;">            @Override  
  132.             public boolean shouldOverrideUrlLoading(WebView view, String url) {  
  133.                 view.loadUrl(url);   
  134.                 return true;  
  135.             }  
  136.         });</span></pre>  
  137. <p style="font-family:Arial,Helvetica,simsun,u5b8bu4f53"></p>  
  138. <p style="font-family:Arial,Helvetica,simsun,u5b8bu4f53"><span style="font-family:verdana,sans-serif; color:rgb(51,51,51)"><span style="line-height:28px"><span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53; color:rgb(75,78,81); line-height:25px"><span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53; color:rgb(75,78,81); line-height:25px"><span style="font-size:16px">4、<span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53; color:rgb(75,78,81); line-height:25px">如果不做任何处理,浏览网页,点击系统“Back”键,整个Browser会调用finish()而结束自身,如果希望浏览的网页回退而不是推出浏览器,需要在当前Activity中处理并消费掉该Back事件。</span></span></span></span></span></span></p>  
  139. <p style="font-family:Arial,Helvetica,simsun,u5b8bu4f53"><span style="font-family:verdana,sans-serif; color:rgb(51,51,51)"><span style="line-height:28px"><span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53; color:rgb(75,78,81); line-height:25px"><span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53; color:rgb(75,78,81); line-height:25px"></span></span></span></span></p>  
  140. <pre name="code" class="java"><pre name="code" class="java" style="font-family: Arial, Helvetica, simsun, u5b8bu4f53; "><span style="font-family: Arial, Helvetica, simsun, u5b8bu4f53; "></span><pre name="code" class="java"@Override  
  141.     public boolean onKeyDown(int keyCode, KeyEvent event) {  
  142.         if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {  
  143.             mWebView.goBack();  
  144.             return true;  
  145.         }  
  146.         return super.onKeyDown(keyCode, event);  
  147.     }</pre><br>  
  148. <span style="font-size:16px">5、<span style="font-family:Arial,Helvetica,simsun,u5b8bu4f53; line-height:25px"><span style="color:#FF0000">addJavascriptInterface方法中要绑定的Java对象及方法要运行另外的线程中,不能运行在构造他的线程中,这也是使用Handler的目的。</span></span></span><span style="line-height:25px"><span style="font-size:16px"><span style="color:#FF0000">如此上面的代码则可以修改成如下即可,利用Handler发送消息进行处理</span><span style="color:#4B4E51">:</span></span></span>  
  149. <pre></pre>  
  150. <pre name="code" class="java" style="font-family: Arial, Helvetica, simsun, u5b8bu4f53; "><span style="color:rgb(75,78,81); line-height:25px"><span style="font-size:16px"></span></span><pre name="code" class="java">        public int mydata() {  
  151.         mHandler.post(new Runnable() {  
  152.             public void run() {  
  153.                 Log.i("TEST""mydata.....");  
  154.             }  
  155.         });  
  156.         return 0;  
  157.     }  
  158. </pre><br>  
  159. <pre></pre>  
  160. <pre></pre>  
  161. <pre></pre>  
  162. <pre></pre>  
  163. <pre></pre>  
  164. <pre></pre>  
  165. <pre></pre>  
  166. <pre></pre>  
  167. <pre></pre>  
  168. <pre></pre>  
  169. <pre></pre>  
  170. <pre></pre>  
  171. <pre></pre>  
  172. <pre></pre>  
  173. <pre></pre>  
  174.      
  175. </pre></pre></pre>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值