王者荣耀体验服申请表哪一天

安卓webview的addJavascriptInterface在签名打包后无效的问题
现在android下应用开发的界面用html5+css3写,交互用javascript和java沟通,但是用上混淆后发现javascript调用java类定义的方法老说找不到这个方法。
发现是用proguard混淆后java定义的类变了名字,不仅如此,这个类定义的方法不知所踪,所以才导致这个问题,
假设我们定义了一个类:
public interface OnWebViewImageListener {
onImageClick(String bigImageUrl);
webView.addJavascriptInterface(new OnWebViewImageListener() {
&&& public void
onImageClick(String bigImageUrl) {
Log.e(BenguoApp.TAG, "img_url = "+bigImageUrl);
(bigImageUrl != null){
&&& Intent izd =
new Intent(context, ImageZoomDialog.class);
izd.putExtra("img_url", bigImageUrl);
context.startActivity(izd);
"mWebViewImageListener");
在混淆文件加上这段就没问题了:
-keep public class (package name).OnWebViewImageListener {
public void onImageClick(java.lang.String);
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!&&|&&
LOFTER精选
网易考拉推荐
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
&uses-permission android:name="android.permission.INTERNET"/&
Then I created a folder named www within my asset folder. After that I created a file index.html
within the www folder. I have used webview and textview controls in my
layout file named main.xml.
My main.xml code is given below.
Collapse | &?xml version="1.0" encoding="utf-8"?&
&LinearLayout xmlns:android="/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" &
android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" /&
&LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_weight="1"&
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceLarge" /&
&/LinearLayout&
&/LinearLayout&
In the main.xml file, I have used a parent layout. It's a linear layout
and within this layout I have used some child controls
and layout which are known as webview, textview controls and linear layout.
Now I am going to write a few more lines in my JavaScriptInterfaceDemoActivity.java class. Let me show my code.
Collapse | package my.
import my.demo.R;
import android.app.A
import android.content.C
import android.os.B
import android.os.H
import android.webkit.WebV
import android.widget.TextV
import android.widget.T
public class JavaScriptInterfaceDemoActivity extends Activity {
private WebView Wv;
private TextView myTextV
final Handler myHandler = new Handler();
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Wv = (WebView)findViewById(R.id.webView1);
myTextView = (TextView)findViewById(R.id.textView1);
final JavaScriptInterface myJavaScriptInterface
= new JavaScriptInterface(this);
Wv.getSettings().setLightTouchEnabled(true);
Wv.getSettings().setJavaScriptEnabled(true);
Wv.addJavascriptInterface(myJavaScriptInterface, "AndroidFunction");
Wv.loadUrl("file:///android_asset/www/index.html");
public class JavaScriptInterface {
Context mC
JavaScriptInterface(Context c) {
mContext =
public void showToast(String webMessage){
final String msgeToast = webM
myHandler.post(new Runnable() {
public void run() {
// This gets executed on the UI thread so it can safely modify Views
myTextView.setText(msgeToast);
Toast.makeText(mContext, webMessage, Toast.LENGTH_SHORT).show();
In my Java class file, I have written some code in the oncreate method. In this method, I find my webview and textview controls using the
findViewById method.
Then I create a JavaScriptInterface class. This class has a constructor and that
constructor initializes the Context class. Now suddenly a question
raises in your mind, what is a Context Class?
Context Class is an abstract class whose
implementation is provided by the Android
system. It allows access to application-specific resources and classes,
as well as up-calls for application-level operations such as launching
activities,
broadcasting and receiving intents, etc.
After the initialization of the constructor, I create a method named showToast with
a string parameter.
This method has a variable msgeToast string and then I created a
Handler named myHandler. Click
to know more about Handlers. This handler has a Post method.
In the method declaration, I create a new instance of the Runnable thread class and inside this class I override a run method.
This run method sets the value for the textview control.
Now I create an instance of my JavaScriptInterface class in my
OnCreate method.&
Collapse | final JavaScriptInterface myJavaScriptInterface = new JavaScriptInterface(this);
After the initialization of the JavaScriptInterface class, I added one more line in my
OnCreate method:
Collapse | Wv.addJavascriptInterface(myJavaScriptInterface, "AndroidFunction");
Webview provides the addJavascriptInterface method. This method contains two parameters:
The class instance to bind to JavaScript.The name to be used to expose the instance in JavaScript.
For webview, we need to call some settings to enable the JavaScript.
Collapse | Wv.getSettings().setJavaScriptEnabled(true);
And finally, you need to provide a web URL in your web view:
Collapse | Wv.loadUrl("file:///android_asset/www/index.html");
Then I created an HTML file named index.html. This HTML file has a textbox and
a Submit button. The HTML file code is given below:
Collapse | &!DOCTYPE &
&html xmlns="http://www.w3.org/1999/xhtml" debug="true"&
&meta http-equiv="Content-Type" content="text/ charset=utf-8" /&
&meta name="viewport"
content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"&
&meta name="apple-mobile-web-app-capable" content="yes"&
&meta name="viewport" content="target-densitydpi=device-dpi" /&
&script type="text/javascript"&
function init()
var testVal = document.getElementById('mytextId').
AndroidFunction.showToast(testVal);
&div style="float:width: 50%;"&
&input type="text" style="width: 180"
name="myText" id="mytextId" /&
&div style="clear:height: 3"& &/div&
&input value="submit" type="button" name="submit"
id="btnSubmit" onclick="javascript:return init();" /&
This HTML file has a JavaScript function named init. This function calls the activity method.
Collapse | AndroidFunction.showToast(testVal);
AndroidFunction is the same name used to expose the instance in JavaScript. We have already given this name in our
addJavascriptInterface method.&
Now finally run your app.
I have also added a toast message in my view.
Points of Interest&
This is a very good feature provided by a addJavascriptInterface
method. But you must be more careful
when you implement this
approach. Otherwise the hacker can inject bad HTML inside code.
This is the good thing that I have learned during my development phase. I
hope you guys will enjoy it and it will help you out in your problems. &
Conclusion &
For more details on the source code, please download the source code
from the link at the top of this article. The source code is easy and
well commented.
Hope this tutorial is useful.
This article, along with any associated source code and files, is licensed under
阅读(1871)|
用微信&&“扫一扫”
将文章分享到朋友圈。
用易信&&“扫一扫”
将文章分享到朋友圈。
历史上的今天
loftPermalink:'',
id:'fks_',
blogTitle:'Android addJavaScriptInterface',
blogAbstract:'Introduction\nHi everyone again. Today I am going to share my experience with the addJavaScriptInterface method in Android. This \nclass basically helps us call any activity method \ninside your JavaSscript function. Some of the points I want to mention below:&\naddJavaScriptInterface method helps us pass values from a webpage',
blogTag:'',
blogUrl:'blog/static/77',
isPublished:1,
istop:false,
modifyTime:0,
publishTime:7,
permalink:'blog/static/77',
commentCount:0,
mainCommentCount:0,
recommendCount:0,
bsrk:-100,
publisherId:0,
recomBlogHome:false,
currentRecomBlog:false,
attachmentsFileIds:[],
groupInfo:{},
friendstatus:'none',
followstatus:'unFollow',
pubSucc:'',
visitorProvince:'',
visitorCity:'',
visitorNewUser:false,
postAddInfo:{},
mset:'000',
remindgoodnightblog:false,
isBlackVisitor:false,
isShowYodaoAd:false,
hostIntro:'',
hmcon:'1',
selfRecomBlogCount:'0',
lofter_single:''
{list a as x}
{if x.moveFrom=='wap'}
{elseif x.moveFrom=='iphone'}
{elseif x.moveFrom=='android'}
{elseif x.moveFrom=='mobile'}
${a.selfIntro|escape}{if great260}${suplement}{/if}
{list a as x}
推荐过这篇日志的人:
{list a as x}
{if !!b&&b.length>0}
他们还推荐了:
{list b as y}
转载记录:
{list d as x}
{list a as x}
{list a as x}
{list a as x}
{list a as x}
{if x_index>4}{break}{/if}
${fn2(x.publishTime,'yyyy-MM-dd HH:mm:ss')}
{list a as x}
{if !!(blogDetail.preBlogPermalink)}
{if !!(blogDetail.nextBlogPermalink)}
{list a as x}
{if defined('newslist')&&newslist.length>0}
{list newslist as x}
{if x_index>7}{break}{/if}
{list a as x}
{var first_option =}
{list x.voteDetailList as voteToOption}
{if voteToOption==1}
{if first_option==false},{/if}&&“${b[voteToOption_index]}”&&
{if (x.role!="-1") },“我是${c[x.role]}”&&{/if}
&&&&&&&&${fn1(x.voteTime)}
{if x.userName==''}{/if}
网易公司版权所有&&
{list x.l as y}
{if defined('wl')}
{list wl as x}{/list}js 调java addJavascriptInterface
你好,想跟你请教个问题:js通过addJavascriptInterface调用java的时候。为什么报[object object]has no method的错,但是我的类中的确写了方法。
恩 谢谢 &我已经解决了。 &是java在对外的函数上加上&@JavascriptInterface 才行。 &不让的话JS调的时候找不到函数。
你是如何写的代码?方便贴上来吗?参考这里面的方法二很简单:
看看对你有没有帮助!!!学习笔记:HybridApp_使用 addJavaScriptInterface() 方法在 WebView 中绑定 Java 对象_张炯焕 - 厚朴〖HOPE〗工作室 >> 厚朴教育 >>
学习笔记:HybridApp_使用 addJavaScriptInterface() 方法在 WebView 中绑定 Java 对象_张炯焕
&使用 addJavaScriptInterface() 方法在 WebView 中绑定 Java 对象
& & WebView 允许开发者拓展 JavaScript API 命名空间,通过 Java 定义各自的组件然后令其在 JavaScript 环境中可用。在你想接入一个平台却在 JavaScript 的方法中不可用时或想要在 JavaScript 中使用用 Java 编写的组件时,此项技术 WebView 的这个方法可以达到你的目的,例子如下:
& & JavaScriptInterface JavaScriptInterface = new JavaScriptInterface(this);
& & myWebView = new MyWebView(this);
& & myWebView.addJavaScriptInterface(JavaScriptInterface, "HybridNote");
& & 在这个例子中,JavaScriptInterface 被绑定到 WebView 的 JavaScript 环境并且可通过 HybridNote 对象 (aka namespace) 接入。这个绑定对象的所有共有或一些特定方法在 JavaScript 代码中可接入引用。一旦这个对象被使用前面指定的函数添加到 WebView 中,这个对象将只有在 WebView 中的页面加载完成或存在已被加载完的页面时,方可在 JavaScript 中引用。
& & 虽然 addJavaScriptInterface() 在设计 HybridApp 时是一个强大的方法,但是使用这个方法存在一个较大的安全隐患。这是因为同源规则 (SOP) 不适用与该方法,加上第三方 JavaScript 库或来自一个陌生域名的 iframe 可能在 Java 层访问这些被暴露的方法。因此,攻击者可通过一个 XSS 漏洞执行原生代码或者注入病毒代码到应用程序中。
& & JavaScript 层中暴露的 Java 对象的所有公有方法在 Android 版本低于 JerryBean MRI(API Level 17) 以下时可访问。而在 Google API 17 以上,暴露的函数必须通过 @JavaScriptInterface 注释来防止方法的暴露。
文章录入:张炯焕 责任编辑:张炯焕
分享本文:
上一篇文章:下一篇文章:

我要回帖

更多关于 王者荣耀体验服申请号 的文章

 

随机推荐