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;
-
-
- @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);
-
-
- mWebView.setHorizontalScrollBarEnabled(false);
- mWebView.setVerticalScrollBarEnabled(false);
-
- mWebView.setWebChromeClient(new WebChromeClient() {
-
- public boolean onJsAlert(WebView view, String url, String message,
- final JsResult result) {
-
- 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;
- };
-
-
- 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);
- }
-
-
- 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>