安卓4.2.1,WebView功能和JavaScript界面​​突破 [英] Android 4.2.1, WebView and javascript interface breaks

查看:116
本文介绍了安卓4.2.1,WebView功能和JavaScript界面​​突破的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我添加了这完美的作品在大多数设备上,除了那些运行Android 4.2.1的JavaScript接口的WebView。

I have a webview with added javascript interface which works perfectly on most devices, except for those running Android 4.2.1.

我删除了大部分的code,并留在了一个非常基本的code:

I removed most of the code, and stayed with a very basic code:

this.webView.getSettings().setJavaScriptEnabled(true);
this.webView.setWebChromeClient(new WebChromeClient());
this.webView.addJavascriptInterface(new Object() {
    public void handshake() {
        Log.d("JS", "handshake no params");
    }

    public void handshake(String json) {
        Log.d("JS", "handshake with params: " + json);
    }
}, "Android");

在事物的JavaScript端的测试code是这样的:

In the javascript side of things the testing code looks like this:

Android.handshake();

不过,我得到这个在logcat中:

But I get this in the logcat:

E / Web控制台:未捕获的类型错误:对象[对象的对象]没有   方法握手

E/Web Console: Uncaught TypeError: Object [object Object] has no method 'handshake'

再次,这同样的事情工作正常,在谁是有旧的Andr​​oid OS(小于4.2.1)的设备,我已经测试(最低版本为2.3.3)

Again, this same exact thing works fine in devices who are have older android os (< 4.2.1) that I have tested (the minimal version being 2.3.3).

但奇怪的是,如果我开始一个全新的项目,只是一个单一的活动具有只是一个web视图,用同样的code,一切工作正常,甚至为4.2.1,但是当它的一部分我的实际项目,事情打破。 没有什么我做与不包括在我提供了这个code段的web视图。

The strange thing is that if I start a completely new project, with nothing but a single Activity which has just a WebView, with the same code, everything works fine even for 4.2.1, but when it's part of my actual project, things break. There's nothing I'm doing with the webview which is not included in this code snippets I provided.

什么是最奇怪,我是JavaScript发现Android的对象,但它只是不具有请求的方法(握手),这怎么可能?

What is the most strange to me is that the javascript finds the Android object but it just does not have the requested method (handshake), how can that be?

任何帮助将是很大的,因为这pciated AP $ P $已经快把我逼疯了近2周左右(这是一个错误我回去的时候,然后放弃,等)。 谢谢你。

Any help will be greatly appreciated since this has been driving me crazy for the past 2 weeks or so (it's a bug I go back to all the time, then give up, etc). Thanks.

推荐答案

从Android 4.2的文档:

From the Android 4.2 documentation:

注意:如果你已经设置你的targetSdkVersion为17或更高,则必须添加@JavascriptInterface标注任何你想用你的网页code(该方法也必须是公共的)方法。如果你不提供注释,则该方法将在Android 4.2或跑步时,不是被你的网页中较高的。

Caution: If you've set your targetSdkVersion to 17 or higher, you must add the @JavascriptInterface annotation to any method that you want available your web page code (the method must also be public). If you do not provide the annotation, then the method will not accessible by your web page when running on Android 4.2 or higher.

这篇关于安卓4.2.1,WebView功能和JavaScript界面​​突破的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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