JavascriptInterface标注为JELLY_BEAN及以下 [英] JavascriptInterface annotation for JELLY_BEAN and below

查看:247
本文介绍了JavascriptInterface标注为JELLY_BEAN及以下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于显然对<著名href=\"https://developer.android.com/reference/android/webkit/WebView.html#addJavascriptInterface%28java.lang.Object,%20java.lang.String%29\"相对=nofollow>官方文档,需要对API级别JELLY_BEAN_MR1以上@JavascriptInterface的使用,从网页视图侧访问Java功能。

As clearly noted on official docs, usage of @JavascriptInterface is needed for API level JELLY_BEAN_MR1 and above, to access a java function from the webview side.

这意味着,项目建设目标必须指向API 17或高于可解决以下导入:

This means that Project Build Target must point to API 17 or above which resolves the following import:

import android.webkit.JavascriptInterface;

如何安卓处理这个code API为16及以下?我会得到一个运行时异常或者它忽略运行时,这个进口?

How does android handles this code for API 16 and below? Will I get a runtime exception or does it ignore this import on runtime?

推荐答案

像你建议进口android.webkit.JavascriptInterface一个进口; 只是提示编译器因此它可以解决你是指在源进一步的完整的类路径。当您尝试使用类的虚拟机将只抛出一个的ClassNotFoundException 。所以,简单的答案是否定的,一切都会好起来,只要你保护你的 JavascriptInterface 使用的东西像下面这样。

Imports like the one you have suggested import android.webkit.JavascriptInterface; are only hints to the compiler so it can resolve the full class path you are referring to further on in your source. The VM will only throw a ClassNotFoundException when you try and use the class. So the simple answer is no, everything will be fine as long as you protect your use of the JavascriptInterface with something like the following.

    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){
        JavascriptInterface js = new JavascriptInterface(){ ... };
    }

这篇关于JavascriptInterface标注为JELLY_BEAN及以下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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