Android activity 和 html一起開發難免有之間通訊的問題。今天簡單測試了一下:
代碼很簡單:
activity:
- package com.lmn.webview.html;
-
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.app.AlertDialog.Builder;
- import android.content.DialogInterface;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Message;
- import android.view.Window;
- import android.webkit.JsResult;
- import android.webkit.WebChromeClient;
- import android.webkit.WebSettings;
- import android.webkit.WebStorage;
- import android.webkit.WebView;
- import android.widget.Toast;
-
- public class MainActivity extends Activity {
- private WebView mWebView;
-
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- mWebView = (WebView) findViewById(R.id.webview);
-
- WebSettings webSettings = mWebView.getSettings();
- webSettings.setSavePassword(false);
- webSettings.setSaveFormData(false);
- webSettings.setJavaScriptEnabled(true);
- webSettings.setSupportZoom(true);
-
- // Horizontal水平方向,Vertical豎直方向
- mWebView.setHorizontalScrollBarEnabled(false);
- mWebView.setVerticalScrollBarEnabled(false);
-
- mWebView.setWebChromeClient(new WebChromeClient() {
- // 處理javascript中的alert
- public boolean onJsAlert(WebView view, String url, String message,
- final JsResult result) {
- // 構建一個Builder來顯示網頁中的對話框
- Builder builder = new Builder(MainActivity.this);
- builder.setTitle("Alert");
- builder.setMessage(message);
- builder.setPositiveButton(android.R.string.ok,
- new AlertDialog.OnClickListener() {
- public void onClick(DialogInterface dialog,
- int which) {
- // 點擊確定按鈕之後,繼續執行網頁中的操作
- result.confirm();
- }
- });
- builder.setCancelable(false);
- builder.create();
- builder.show();
- return true;
- };
-
- // 處理javascript中的confirm
- public boolean onJsConfirm(WebView view, String url,
- String message, final JsResult result) {
- Builder builder = new Builder(MainActivity.this);
- builder.setTitle("confirm");
- builder.setMessage(message);
- builder.setPositiveButton(android.R.string.ok,
- new AlertDialog.OnClickListener() {
- public void onClick(DialogInterface dialog,
- int which) {
- result.confirm();
- }
- });
- builder.setNegativeButton(android.R.string.cancel,
- new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog,
- int which) {
- result.cancel();
- }
- });
- builder.setCancelable(false);
- builder.create();
- builder.show();
- return true;
- };
-
- // 設置網頁加載的進度條
- public void onProgressChanged(WebView view, int newProgress) {
- MainActivity.this.getWindow().setFeatureInt(
- Window.FEATURE_PROGRESS, newProgress * 100);
- super.onProgressChanged(view, newProgress);
- }
-
- // 設置應用程序的標題title
- public void onReceivedTitle(WebView view, String title) {
- MainActivity.this.setTitle(title);
- super.onReceivedTitle(view, title);
- }
-
- public void onReachedMaxAppCacheSize(long spaceNeeded,
- long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
- quotaUpdater.updateQuota(spaceNeeded * 2);
- }
- });
-
-
- mWebView.addJavascriptInterface(new JavaScriptInterface(), "demo");
- mWebView.loadUrl("file:///android_asset/demo.html");
- }
-
- final class JavaScriptInterface {
- JavaScriptInterface() {
- }
-
- public int mydata() {
- return 10;
- }
-
- public void DataFromHtml(String resHtml) {
- Message m = new Message();
- m.obj = resHtml;
- h.sendMessage(m);
- }
- }
-
- Handler h = new Handler() {
- public void handleMessage(Message msg) {
- Toast.makeText(MainActivity.this, msg.obj + "", Toast.LENGTH_LONG)
- .show();
- }
- };
- }
HTML:
- <!DOCTYPE html >
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=GBK">
- <meta name="viewport" content="width=device-width, initial-scale=1.0, initial-scale = 1.0 ,user-scalable=no , maximum-scale = 1.0 ">
- <link rel="stylesheet" href="css/jquery.mobile.min.css" />
- <script src="js/jquery-1.7.1.min.js"></script>
- <title>Insert title here</title>
- <script type="text/javascript">
- $(function() {
- $("#btn_getHtml").click(function(event) {
- demo.DataFromHtml("From Html");
- });
-
- $("#btn_getActivity").click(function(event) {
- alert(demo.mydata());
- });
- });
- </script>
- <script src="js/jquery.mobile.min.js"></script>
- </head>
- <body>
- <div data-role="page" id="home">
- <div data-role="content">
- -----------這是一個html頁面----------
- <button id="btn_getActivity">
- 獲取Activity中的數據
- </button>
- <button id="btn_getHtml">
- 獲取Html中的數據
- </button>
- </div>
- </div>
- </body>
- </html>