반응형
As promised earlier, here is an example of how to add support for alert() function to a WebView in your Android application:
- final WebView browser = (WebView)findViewById(R.id.browser);
- /* JavaScript must be enabled if you want it to work, obviously */
- browser.getSettings().setJavaScriptEnabled(true);
- final Context myApp = this;
- /* WebChromeClient must be set BEFORE calling loadUrl! */
- browser.setWebChromeClient(new WebChromeClient() {
- @Override
- public boolean onJsAlert(WebView view, String url, String message, final android.webkit.JsResult result)
- {
- new AlertDialog.Builder(myApp)
- .setTitle("javaScript dialog")
- .setMessage(message)
- .setPositiveButton(android.R.string.ok,
- new AlertDialog.OnClickListener()
- {
- public void onClick(DialogInterface dialog, int which)
- {
- result.confirm();
- }
- })
- .setCancelable(false)
- .create()
- .show();
- return true;
- };
- });
- /* load a web page which uses the alert() function */
- browser.loadUrl("http://lexandera.com/files/jsexamples/alert.html");
final WebView browser = (WebView)findViewById(R.id.browser); /* JavaScript must be enabled if you want it to work, obviously */ browser.getSettings().setJavaScriptEnabled(true); final Context myApp = this; /* WebChromeClient must be set BEFORE calling loadUrl! */ browser.setWebChromeClient(new WebChromeClient() { @Override public boolean onJsAlert(WebView view, String url, String message, final android.webkit.JsResult result) { new AlertDialog.Builder(myApp) .setTitle("javaScript dialog") .setMessage(message) .setPositiveButton(android.R.string.ok, new AlertDialog.OnClickListener() { public void onClick(DialogInterface dialog, int which) { result.confirm(); } }) .setCancelable(false) .create() .show(); return true; }; }); /* load a web page which uses the alert() function */ browser.loadUrl("http://lexandera.com/files/jsexamples/alert.html");
Code for adding support for confirm() and prompt() is almost identical and can be found in Mosembro’s source code.
출처 : http://lexandera.com/2009/01/adding-alert-support-to-a-webview/