Alert is not appear from web view in android?
I use the sample provided by Google for demonstrating two way communication between JavaScript and Java ,
ref[1]:
开发者_开发技巧http://code.google.com/p/apps-for-android/source/browse/trunk/Samples/WebViewDemo/src/com/google/android/webviewdemo/WebViewDemo.java
The functionality is working fine. I am able to call JavaScript function from Java and callback the Java function from JavaScript.
The problem is when I use an alert inside a JavaScript function it won`t appear, but the functionality inside the function is working properly.
Why does alert("test")
inside a JavaScript function not appear in Android.
I load the JavaScript in a web-view
. When I a clicking button in Android
I call the function, but it does not appear.
If anyone knows the problem, pealse help me out.
Thanks
setContentView(R.layout.main);
WebView webview = (WebView) findViewById(R.id.webview);
WebSettings webSettings = webview.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setBuiltInZoomControls(true);
webview.requestFocusFromTouch();
webview.setWebViewClient(new WebViewClient());
webview.setWebChromeClient(new WebChromeClient());
webview.loadUrl("file:///android_asset/test.html");
this code working perfect and shows me alert box..
and this is my
test.html
<html>
<head>
<script type="text/javascript">
function show_alert()
{
alert("Hello! I am an alert box!");
}
</script>
</head>
<body>
<input type="button" onclick="show_alert()" value="Show alert box" />
</body>
</html>
By adding the following two lines, my JavaScript works:
mWebview.setWebViewClient(new WebViewClient()());
mWebview.setWebChromeClient(new WebChromeClient());
Use the following mehtod,
WebView wv=new WebView(this);
wv.setWebChromeClient(new WebChromeClient() {
@Override
public boolean onJsAlert(WebView view, String url, String message,JsResult result) {
//Required functionality here
return super.onJsAlert(view, url, message, result);
}
Just use WebChromeClient. It will do everything.
mWebview.setWebChromeClient(new WebChromeClient());
It will work.
精彩评论