Android Activity 和 HTML 数据通信
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>
相关推荐
Microwave 2019-06-27
huaye00 2019-06-26
flyingdream 2011-01-24
编程与网站运营 2019-06-10
DuKeCan 2017-06-22
wssssg 2016-09-21
Violet 2014-05-31
ShawnXiaFei 2015-06-30