Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2019-10-16:Java与JS代码如何互调?有做过相关优化吗? #166

Open
Moosphan opened this issue Oct 16, 2019 · 3 comments
Open

Comments

@Moosphan
Copy link
Owner

No description provided.

@canyie
Copy link

canyie commented Oct 16, 2019

WebView?

  • java调js:
    可以用loadUrl指定 javascript: 协议,然后带上js代码,如
    webView.loadUrl("javascript:alert('hello!')")
    4.4以后还提供了一个execJavascript方法,更方便调用

  • js调java:
    可以先写好一个互调接口类,用addJavascriptInterface绑定好,然后js代码里调用
    注意:api17以后希望被js调用的需要添加@JavascriptInterface注解,因为api17以前存在漏洞,通过互调接口的getClass()方法可以拿到Class对象,之后通过Class.forName()等一系列反射api可以调用任何方法
    也可以在java层通过shouldOverrideUrlLoading拦截跳转请求,js代码里通过跳转页面传递给java

@adustdu2015
Copy link

jsbridge这个框架,完美解决了Java和js的交互。

@mlinqirong
Copy link

使用DSBridge与js做交互 可以同步和异步交互

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants