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(); } }; }
Android Activity 和 HTML 数据通信
内容版权声明:除非注明,否则皆为本站原创文章。