Android Activity 和 HTML 数据通信

maozhuofan 2011-12-06

Android activity 和 html一起开发难免有之间通讯的问题。今天简单测试了一下:

Android Activity 和 HTML 数据通信 Android Activity 和 HTML 数据通信  Android Activity 和 HTML 数据通信

代码很简单:

activity:

  1. package com.lmn.webview.html;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.AlertDialog;  
  5. import android.app.AlertDialog.Builder;  
  6. import android.content.DialogInterface;  
  7. import android.os.Bundle;  
  8. import android.os.Handler;  
  9. import android.os.Message;  
  10. import android.view.Window;  
  11. import android.webkit.JsResult;  
  12. import android.webkit.WebChromeClient;  
  13. import android.webkit.WebSettings;  
  14. import android.webkit.WebStorage;  
  15. import android.webkit.WebView;  
  16. import android.widget.Toast;  
  17.   
  18. public class MainActivity extends Activity {  
  19.     private WebView mWebView;  
  20.   
  21.     /** Called when the activity is first created. */  
  22.     @Override  
  23.     public void onCreate(Bundle savedInstanceState) {  
  24.         super.onCreate(savedInstanceState);  
  25.         setContentView(R.layout.main);  
  26.         mWebView = (WebView) findViewById(R.id.webview);  
  27.   
  28.         WebSettings webSettings = mWebView.getSettings();  
  29.         webSettings.setSavePassword(false);  
  30.         webSettings.setSaveFormData(false);  
  31.         webSettings.setJavaScriptEnabled(true);  
  32.         webSettings.setSupportZoom(true);  
  33.   
  34.         // Horizontal水平方向,Vertical竖直方向   
  35.         mWebView.setHorizontalScrollBarEnabled(false);  
  36.         mWebView.setVerticalScrollBarEnabled(false);  
  37.   
  38.         mWebView.setWebChromeClient(new WebChromeClient() {  
  39.             // 处理javascript中的alert   
  40.             public boolean onJsAlert(WebView view, String url, String message,  
  41.                     final JsResult result) {  
  42.                 // 构建一个Builder来显示网页中的对话框   
  43.                 Builder builder = new Builder(MainActivity.this);  
  44.                 builder.setTitle("Alert");  
  45.                 builder.setMessage(message);  
  46.                 builder.setPositiveButton(android.R.string.ok,  
  47.                         new AlertDialog.OnClickListener() {  
  48.                             public void onClick(DialogInterface dialog,  
  49.                                     int which) {  
  50.                                 // 点击确定按钮之后,继续执行网页中的操作   
  51.                                 result.confirm();  
  52.                             }  
  53.                         });  
  54.                 builder.setCancelable(false);  
  55.                 builder.create();  
  56.                 builder.show();  
  57.                 return true;  
  58.             };  
  59.   
  60.             // 处理javascript中的confirm   
  61.             public boolean onJsConfirm(WebView view, String url,  
  62.                     String message, final JsResult result) {  
  63.                 Builder builder = new Builder(MainActivity.this);  
  64.                 builder.setTitle("confirm");  
  65.                 builder.setMessage(message);  
  66.                 builder.setPositiveButton(android.R.string.ok,  
  67.                         new AlertDialog.OnClickListener() {  
  68.                             public void onClick(DialogInterface dialog,  
  69.                                     int which) {  
  70.                                 result.confirm();  
  71.                             }  
  72.                         });  
  73.                 builder.setNegativeButton(android.R.string.cancel,  
  74.                         new DialogInterface.OnClickListener() {  
  75.                             public void onClick(DialogInterface dialog,  
  76.                                     int which) {  
  77.                                 result.cancel();  
  78.                             }  
  79.                         });  
  80.                 builder.setCancelable(false);  
  81.                 builder.create();  
  82.                 builder.show();  
  83.                 return true;  
  84.             };  
  85.   
  86.             // 设置网页加载的进度条   
  87.             public void onProgressChanged(WebView view, int newProgress) {  
  88.                 MainActivity.this.getWindow().setFeatureInt(  
  89.                         Window.FEATURE_PROGRESS, newProgress * 100);  
  90.                 super.onProgressChanged(view, newProgress);  
  91.             }  
  92.   
  93.             // 设置应用程序的标题title   
  94.             public void onReceivedTitle(WebView view, String title) {  
  95.                 MainActivity.this.setTitle(title);  
  96.                 super.onReceivedTitle(view, title);  
  97.             }  
  98.   
  99.             public void onReachedMaxAppCacheSize(long spaceNeeded,  
  100.                     long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {  
  101.                 quotaUpdater.updateQuota(spaceNeeded * 2);  
  102.             }  
  103.         });  
  104.   
  105.           
  106.         mWebView.addJavascriptInterface(new JavaScriptInterface(), "demo");  
  107.         mWebView.loadUrl("file:///android_asset/demo.html");  
  108.     }  
  109.   
  110.     final class JavaScriptInterface {  
  111.         JavaScriptInterface() {  
  112.         }  
  113.   
  114.         public int mydata() {  
  115.             return 10;  
  116.         }  
  117.   
  118.         public void DataFromHtml(String resHtml) {  
  119.             Message m = new Message();  
  120.             m.obj = resHtml;  
  121.             h.sendMessage(m);  
  122.         }  
  123.     }  
  124.   
  125.     Handler h = new Handler() {  
  126.         public void handleMessage(Message msg) {  
  127.             Toast.makeText(MainActivity.this, msg.obj + "", Toast.LENGTH_LONG)  
  128.                     .show();  
  129.         }  
  130.     };  
  131. }  

HTML:

  1. <!DOCTYPE html >  
  2. <html>  
  3.     <head>  
  4.         <meta http-equiv="Content-Type" content="text/html; charset=GBK">  
  5.         <meta name="viewport" content="width=device-width, initial-scale=1.0, initial-scale = 1.0 ,user-scalable=no , maximum-scale = 1.0 ">  
  6.         <link rel="stylesheet" href="css/jquery.mobile.min.css" />  
  7.         <script  src="js/jquery-1.7.1.min.js"></script>  
  8.         <title>Insert title here</title>  
  9.         <script type="text/javascript">  
  10.             $(function() {  
  11.                 $("#btn_getHtml").click(function(event) {  
  12.                     demo.DataFromHtml("From Html");  
  13.                 });  
  14.   
  15.                 $("#btn_getActivity").click(function(event) {  
  16.                     alert(demo.mydata());  
  17.                 });  
  18.             });  
  19.         </script>  
  20.         <script src="js/jquery.mobile.min.js"></script>  
  21.     </head>  
  22.     <body>  
  23.         <div data-role="page" id="home">  
  24.             <div data-role="content">  
  25.                 -----------这是一个html页面----------  
  26.                 <button id="btn_getActivity">  
  27.                     获取Activity中的数据  
  28.                 </button>  
  29.                 <button id="btn_getHtml">  
  30.                     获取Html中的数据  
  31.                 </button>  
  32.             </div>  
  33.         </div>  
  34.     </body>  
  35. </html>  

相关推荐