WebChromeClient onJsAlert接收不确定的消息 [英] WebChromeClient onJsAlert receives undefined message

查看:495
本文介绍了WebChromeClient onJsAlert接收不确定的消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个安装了一个WebChromeClient一个Android的WebView。
一旦在网页中某个动作,我创建了一个JavaScript警告与消息,它传递回onJsAlert方法。

I have an android webview with a WebChromeClient installed on it. Upon a certain action in the webpage, I create a javascript alert with a message, which is passed back to the onJsAlert method.

然后,根据消息我打开另一个活动。
这适用于Android> = 4不错,但在Android 2.3不起作用。

Then, based on the message I open another activity. This works good on Android >= 4 but does not work on Android 2.3.

在Android 2.3 onJsAlert方法也被调用,但传递的消息参数是未定义的某些原因。任何想法,为什么这件事会发生,将是非常美联社preciated!

On Android 2.3 the onJsAlert method also gets called, but the passed message parameter is undefined for some reason. Any idea why this might happen, will be really appreciated!

推荐答案

下面是code,我用

private WebView webView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.demo);

    try {
        webView = (WebView) findViewById(R.id.webView);

        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webView.setWebChromeClient(new MyJavaScriptChromeClient());

        webView.loadUrl("file:///android_asset/texthtml.html");
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

private class MyJavaScriptChromeClient extends WebChromeClient {
    @Override
    public boolean onJsAlert(WebView view, String url, String message,
            final JsResult result) {
        // handle Alert event, here we are showing AlertDialog


        new AlertDialog.Builder(WebActivity.this)
                .setTitle("JavaScript Alert !")
                .setMessage(message)
                .setPositiveButton(android.R.string.ok,
                        new AlertDialog.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                // do your stuff
                                result.confirm();
                            }
                        }).setCancelable(false).create().show();
        return true;
    }

}

和这是我的html文件

and this my html file

<html>

<div onclick="alert('hello')"> Click Me !!  </div>

这篇关于WebChromeClient onJsAlert接收不确定的消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆