Skip to content

Commit

Permalink
添加同步方法设置方式,优化异步回调注入流程
Browse files Browse the repository at this point in the history
  • Loading branch information
ChinaLike committed Sep 16, 2023
1 parent a3f5d9b commit 54a93d5
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 7 deletions.
1 change: 1 addition & 0 deletions app/src/main/assets/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
</body>
<script>

alert(window.JsBridgeConstant.statusHeight())

function nativeNoArgAndNoCallback(){
JsBridge.nativeNoArgAndNoCallback();
Expand Down
24 changes: 24 additions & 0 deletions app/src/main/java/com/like/jsbridge/ConstantJsBridge.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.like.jsbridge

import android.content.Context
import android.webkit.JavascriptInterface

/**
*
* @author like
* @date 2023/9/16 20:36
*/
class ConstantJsBridge(private val context: Context){

@JavascriptInterface
fun statusHeight():Int{
var result = 0
val resourceId = context.resources
.getIdentifier("status_bar_height", "dimen", "android")
if (resourceId > 0) {
result = context.resources.getDimensionPixelSize(resourceId)
}
return result
}

}
3 changes: 2 additions & 1 deletion app/src/main/java/com/like/jsbridge/JsBridgeDialog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import android.webkit.JavascriptInterface
import androidx.appcompat.app.AlertDialog
import com.alibaba.fastjson.JSON
import com.core.web.Callback
import com.core.web.base.IJavascriptInterface

/**
*
* @author like
* @date 6/1/21 5:28 PM
*/
class JsBridgeDialog(private val context: Context) {
class JsBridgeDialog(private val context: Context): IJavascriptInterface {

@JavascriptInterface
fun nativeDialog(params: String, callback: Callback) {
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/com/like/jsbridge/JsBridgeToast.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import android.webkit.JavascriptInterface
import android.widget.Toast
import com.core.web.Callback
import com.core.web.CallbackBean
import com.core.web.base.IJavascriptInterface

/**
* 测试类
* @author like
* @date 5/26/21 4:43 PM
*/
class JsBridgeToast(private val context: Context) {
class JsBridgeToast(private val context: Context):IJavascriptInterface {

@JavascriptInterface
fun nativeNoArgAndNoCallback(){
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/com/like/jsbridge/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ protected void onCreate(Bundle savedInstanceState) {

webView.addJavascriptInterface(new JsBridgeToast(this));
webView.addJavascriptInterface(new JsBridgeDialog(this));
webView.addJavascriptInterface(new ConstantJsBridge(this),webView.jsCallName() + "Constant");

webView.loadUrl("file:///android_asset/test.html");

Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/com/like/jsbridge/MainActivityKotlin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class MainActivityKotlin : AppCompatActivity() {
webView = findViewById(R.id.webView)
webView?.addJavascriptInterface(JsBridgeToast(this))
webView?.addJavascriptInterface(JsBridgeDialog(this))
webView?.addJavascriptInterface(ConstantJsBridge(this),webView?.jsCallName() + "Constant")
webView?.loadUrl("file:///android_asset/test.html")

//调用Js无参数无回调
Expand Down
4 changes: 2 additions & 2 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ android {
defaultConfig {
minSdkVersion 19
targetSdkVersion 30
versionCode 5
versionName "0.0.5"
versionCode 6
versionName "0.0.6"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
Expand Down
1 change: 0 additions & 1 deletion library/src/main/java/com/core/web/IWebView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ interface IWebView {
return "JsBridge"
}


/**
* 获取js的Window,可不用重写,如果遇到window对象不对可以使用这个重新赋值window
*/
Expand Down
9 changes: 7 additions & 2 deletions library/src/main/java/com/core/web/base/BaseWebView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ abstract class BaseWebView : WebView, IWebView {

private var uniqueId = 0

/**
* 常量注入次数
*/
private var constantInjectCount: Int = 0

constructor(context: Context) : super(context) {
initWebView()
}
Expand Down Expand Up @@ -142,7 +147,7 @@ abstract class BaseWebView : WebView, IWebView {

@SuppressLint("JavascriptInterface")
override fun addJavascriptInterface(`object`: Any, name: String) {
if (name != "MiWebViewDetector" && name != innerJavascriptInterfaceName) {
if (`object` is IJavascriptInterface) {
javascriptInterfaceList.add(JavascriptInterfaceBean(`object`, name))
} else {
super.addJavascriptInterface(`object`, name)
Expand All @@ -152,7 +157,7 @@ abstract class BaseWebView : WebView, IWebView {
/**
* 添加需要注入的类
*/
fun addJavascriptInterface(`object`: Any) {
fun addJavascriptInterface(`object`: IJavascriptInterface) {
addJavascriptInterface(`object`, "")
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.core.web.base

/**
* 使用异步回调的类需要实现此类,如果不实现此类,将会走正常[android.webkit.WebView.addJavascriptInterface]流程
* @author like
* @date 2023/9/16 20:44
*/
interface IJavascriptInterface

0 comments on commit 54a93d5

Please sign in to comment.