Android examples for User Interface:WebView Javascript
init WebView and show Javascript dialog
//package com.java2s; import android.app.ProgressDialog; import android.content.Context; import android.content.Intent; import android.net.Uri; import android.view.View; import android.webkit.JsPromptResult; import android.webkit.JsResult; import android.webkit.WebChromeClient; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { private static Pattern pattern = Pattern .compile(".*(.jpg|.png|.gif|.jpeg|.bmp)$"); public static void initView(WebView webView, String url, Context context) { webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setRenderPriority( WebSettings.RenderPriority.HIGH); webView.getSettings().setDomStorageEnabled(true); webView.getSettings().setLoadsImagesAutomatically(true); webView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT); webView.loadUrl(url);//w w w .j ava 2 s . c o m startWebView(webView, context); } static private void startWebView(WebView webView, final Context context) { webView.setWebViewClient(new WebViewClient() { ProgressDialog progressDialog; public boolean shouldOverrideUrlLoading(WebView view, String url) { Matcher matcher = pattern.matcher(url.toLowerCase()); if (matcher.matches()) { return true; } Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse(url)); context.startActivity(intent); return false; } public void onLoadResource(WebView view, String url) { if (progressDialog == null && url.contains("webview")) { progressDialog = new ProgressDialog(context); progressDialog.setCancelable(true); progressDialog.setCanceledOnTouchOutside(true); progressDialog.setMessage("???..."); progressDialog.show(); } } public void onPageFinished(WebView view, String url) { try { if (progressDialog.isShowing()) { progressDialog.dismiss(); progressDialog = null; } } catch (Exception exception) { } } @Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { view.setVisibility(View.GONE); // ?????????? } }); webView.setWebChromeClient(new WebChromeClient() { @Override public boolean onJsAlert(WebView view, String url, String message, JsResult result) { result.confirm(); return true; } @Override public boolean onJsConfirm(WebView view, String url, String message, JsResult result) { return super.onJsConfirm(view, url, message, result); } @Override public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result) { return super.onJsPrompt(view, url, message, defaultValue, result); } }); } }