歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

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>  
Copyright © Linux教程網 All Rights Reserved