zoukankan      html  css  js  c++  java
  • Android 中 java 与 webview 的交互

    Android 中 java 与 webview 的交互 - - ITeye技术网站

    android 的webkit的api的开放程度还是非常不错,java可以和webview内的javascript很好的交互。下面的小例子可以说明这一点。



    先看我们的html文档:




    Html代码  收藏代码
    1. <html>  
    2.     <script language="javascript">  
    3.         /* This function is invoked by the activity */  
    4.         function wave() {  
    5.             alert("1");  
    6.             document.getElementById("droid").src="android_waving.png";  
    7.             alert("2");  
    8.         }  
    9.     </script>  
    10.     <body>  
    11.         <!-- Calls into the javascript interface for the activity -->  
    12.         <a onClick="window.demo.clickOnAndroid()"><div style="80px;  
    13.             margin:0px auto;  
    14.             padding:10px;  
    15.             text-align:center;  
    16.             border:2px solid #202020;" >  
    17.                 <img id="droid" src="android_normal.png"/><br>  
    18.                 Click me!  
    19.         </div></a>  
    20.     </body>  
    21. </html>  




    再看我们的java 代码。


    Java代码  收藏代码
    1. public class WebViewDemo extends Activity {  
    2.   
    3.     private static final String LOG_TAG = "WebViewDemo";  
    4.   
    5.     private WebView mWebView;  
    6.   
    7.     private Handler mHandler = new Handler();  
    8.   
    9.     @Override  
    10.     public void onCreate(Bundle icicle) {  
    11.         super.onCreate(icicle);  
    12.         setContentView(R.layout.main);  
    13.         mWebView = (WebView) findViewById(R.id.webview);  
    14.   
    15.         WebSettings webSettings = mWebView.getSettings();  
    16.         webSettings.setSavePassword(false);  
    17.         webSettings.setSaveFormData(false);  
    18.         webSettings.setJavaScriptEnabled(true);  
    19.         webSettings.setSupportZoom(false);  
    20.   
    21.         mWebView.setWebChromeClient(new MyWebChromeClient());  
    22.   
    23.         mWebView.addJavascriptInterface(new DemoJavaScriptInterface(), "demo");  
    24.   
    25.         mWebView.loadUrl("file:///android_asset/demo.html");  
    26.     }  
    27.   
    28.     final class DemoJavaScriptInterface {  
    29.   
    30.         DemoJavaScriptInterface() {  
    31.         }  
    32.   
    33.         /** 
    34.          * This is not called on the UI thread. Post a runnable to invoke 
    35.          * loadUrl on the UI thread. 
    36.          */  
    37.         public void clickOnAndroid() {  
    38.             mHandler.post(new Runnable() {  
    39.                 public void run() {  
    40.                     mWebView.loadUrl("javascript:wave()");  
    41.                 }  
    42.             });  
    43.   
    44.         }  
    45.     }  
    46.   
    47.     /** 
    48.      * Provides a hook for calling "alert" from javascript. Useful for 
    49.      * debugging your javascript. 
    50.      */  
    51.     final class MyWebChromeClient extends WebChromeClient {  
    52.         @Override  
    53.         public boolean onJsAlert(WebView view, String url, String message, JsResult result) {  
    54.             Log.d(LOG_TAG, message);  
    55.             result.confirm();  
    56.             return true;  
    57.         }  
    58.           
    59.     }  
    60. }  




    WebView 的addJavascriptInterface函数就可以实现彼此的交互。

  • 相关阅读:
    python --异常处理
    Python -- 函数对象
    python --循环对象
    python --循环设计
    python --模块
    python --文本文件的输入输出
    xpee.vbs
    oracle 有个xe版本
    POI对Excel单元格进行颜色设置
    POI进行ExcelSheet的拷贝
  • 原文地址:https://www.cnblogs.com/lexus/p/2370591.html
Copyright © 2011-2022 走看看