1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > js调用android方法参数 Android与Js交互之JSBridge的使用

js调用android方法参数 Android与Js交互之JSBridge的使用

时间:2019-01-30 18:30:54

相关推荐

js调用android方法参数 Android与Js交互之JSBridge的使用

什么是JsBridge

JsBridge是js与Native之间进行通信的桥梁。

为什么要使用JsBridge

Android4.2以下的addJavascriptInterface存在安全漏洞,虽然在Android4.2之后用@JavascriptInterface代替了addJavascriptInterface但是由于兼容性和安全性问题,基本上我们不会再利用Android系统为我们提供的addJavascriptInterface方法或者@JavascriptInterface注解来实现,所以我们只能另辟蹊径,去寻找既安全,又能实现兼容Android各个版本的方案。

如何使用JsBridge

在Android中我们使用JsBridge开源项目来实现,实现JSBridge分为以下几个步骤。

步骤一:导入依赖

在项目gradle文件中添加

repositories {

...

maven { url "https://jitpack.io" }

}

在module的gradle中添加

dependencies {

...

implementation com.github.lzyzsd:jsbridge:1.0.4

}

步骤二:布局文件中用BridgeWebView代替WebView

xmlns:app="/apk/res-auto"

xmlns:tools="/tools"

android:layout_width="match_parent"

android:orientation="vertical"

android:layout_height="match_parent"

tools:context=".MainActivity">

android:layout_width="match_parent"

android:id="@+id/et"

android:layout_height="wrap_content" />

android:layout_width="match_parent"

android:text="调用js方法"

android:id="@+id/bt"</

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。