建造一座桥! Android和WebApp之间的无缝集成
通过SimpleAndroidBridge库在您的Android和WebApp之间毫不费力地建造了坚固的桥梁。该库允许您在Android WebView中渲染Web应用程序,并创建JSInterface,以在两个平台之间进行平稳的通信。在您的Web应用程序保持响应迅速和高效的同时,共享复杂的对象,承诺和回调功能。
✓共享对象 - Android⇄网络
✓分享承诺 - Android⇄网
✓回调功能 - Android←Web
✓呼叫功能非阻滞 - Android⇄web
✓用打字稿键入安全性-Android + Web
Android SDK中的内置JavaScript桥仅支持原始类型。使用SimpleAndroidBridge,您可以轻松地在Android和Web之间共享复杂的对象。只需将类型定义为Android本机函数中的参数或返回值即可,该库将自动将JavaScript对象转换为Kotlin对象,反之亦然。
// Kotlin
class AndroidNativeInterface ( val contactService : ContactService ): DefaultJSInterface( " Android " ) {
@NativeCall( CallType . FULL_SYNC )
fun searchContact ( contactFilter : ContactFilter ): List < Contact > {
return contactService.search(contactFilter)
}
}
data class ContactFilter ( val surname : String? = null , val firstname : String? = null )
data class Contact ( val surname : String? = null , val fistname : String? = null ,
val mail : String? = null , val phonenumber : String? = null ) // Javascript
console . log ( Bridge . interfaces . Android . searchContact ( { surname : "Pitt" } ) )Android SDK中的JavaScript桥以阻止方式执行功能,导致Web应用程序冻结直到本机函数返回。但是,使用此库,您可以定义承诺返回类型,以进行非阻滞执行。通过利用“ doinbackground”函数,在背景线程中执行Android代码,从而阻止Web应用程序被阻止。
// Kotlin
class AndroidNativeInterface ( val contactService : ContactService ): DefaultJSInterface( " Android " ) {
@NativeCall( CallType . FULL_PROMISE )
fun searchContact ( contactFilter : ContactFilter ) = doInBackground< List < Contact >> { promise ->
try {
promise.resolve(contactService.search(contactFilter))
} catch (e : Exception ) {
promise.reject(e)
}
}
}
data class ContactFilter ( val surname : String? = null , val firstname : String? = null )
data class Contact ( val surname : String? = null , val fistname : String? = null ,
val mail : String? = null , val phonenumber : String? = null ) // Javascript
Bridge . interfaces . Android . searchContact ( { surname : "Pitt" } ) . then ( ( list ) => {
console . log ( list ) ;
} ) ;如果您熟悉JavaScript,则可能对回调功能并不陌生。 SimpleAndroidBridge将此概念更进一步,使您可以将这些JavaScript回调功能直接注入Android层,从而在Web应用程序和Android之间创建无缝的交互。
// Kotlin
class AndroidNativeInterface ( val button : Button ): DefaultJSInterface( " Android " ) {
@NativeCall( CallType . FULL_SYNC )
fun registerOnClickAction ( jsFunction : JSFunction ) {
button.setOnClickListener { jsFunction() }
}
} // Javascript
Bridge . interfaces . Android . registerOnClickAction ( ( ) => {
console . log ( "Button Clicked!" )
} )要将参数传递给JavaScript函数,请使用JSFunctionWithArg类型的JSFunction类型,该类型是专门为接受参数而设计的。
// Kotlin
class AndroidNativeInterface ( val button : Button ): DefaultJSInterface( " Android " ) {
var i = 0
@NativeCall( CallType . FULL_SYNC )
fun registerOnClickAction ( jsFunction : JSFunctionWithArg < Int >) {
button.setOnClickListener { jsFunction( ++ i) }
}
} // Javascript
Bridge . interfaces . Android . registerOnClickAction ( ( i ) => {
console . log ( "Button Clicked! " + i )
} )要将多个参数传递给函数,请考虑创建数据类。
对于需要将结果返回到Android层的函数,您可以在没有参数的情况下使用JSFunctionWithPromise或在接受参数的函数的情况下,或JSFunctionWithPromiseAndArg 。
// Kotlin
class AndroidNativeInterface ( val button : Button ): DefaultJSInterface( " Android " ) {
@NativeCall( CallType . FULL_SYNC )
fun registerOnClickAction ( jsFunction : JSFunctionWithPromiseAndArg < Add , Int >) {
button.setOnClickListener {
val add = Add (( Math .random() * 10 ).toInt(), ( Math .random() * 10 ).toInt())
jsFunction(add)
.then{ Log .d( " AndroidNativeInterface " , " Web calculated: ${add.a} + ${add.b} = $it " ) }
. catch { Log .e( " AndroidNativeInterface " , " ERROR IN WEB LAYER: $it " ) }
}
}
data class Add ( a : Int , b : Int )
} // Javascript
Bridge . interfaces . Android . registerOnClickAction ( ( add ) => {
return new Promise ( ( resolve ) => { resolve ( add . a + add . b ) } )
} )笔记
要释放JSFunction并清除其绑定,只需调用close功能。 JSFunction实现了AutoCloseable接口,使您能够使用try-with-with-resources或AutoCloseable.use {}块自动管理函数的生命周期并确保正确清理。
function.use { it() }此外,如果您的Web应用程序或WebView支持重新加载,则建议在桥梁中添加AfterInitializelistener。该侦听器将有助于发布任何可用的JSFunctions ,以确保初始化后的干净状态。
该库支持不同的本机呼叫类型,可让您决定如何调用本机代码。
CallType.FULL_SYNC调用类型以阻止方式调用本机代码,导致JavaScript执行暂停,直到本机Android函数返回。结果,在本机执行完成之前,Web视图保持不响应。 (不建议长期运行的任务)
// Kotlin
@NativeCall( CallType . FULL_SYNC )
fun searchContact ( contactFilter : ContactFilter ): List < Contact > {
return contactService.search(contactFilter)
} // Javascript
console . log ( Bridge . interfaces . Android . searchContact ( { surname : "Pitt" } ) ) CallType.WEB_PROMISE呼叫类型的功能类似于FULL_SYNC调用,其关键区别在于JavaScript调用返回承诺。但是,本机Android函数仍以阻塞方式调用。 (建议如果您不确定任务持续时间,并且可能需要将来迁移到FULL_PROMISE )
// Kotlin
@NativeCall( CallType . WEB_PROMISE )
fun searchContact ( contactFilter : ContactFilter ): List < Contact > {
return contactService.search(contactFilter)
} // Javascript
Bridge . interfaces . Android . searchContact ( { surname : "Pitt" } ) . then ( ( list ) => {
console . log ( list ) ;
} ) ; CallType.FULL_PROMISE调用类型使您可以在背景线程中执行本机Android代码,从而允许JavaScript执行继续不间断。结果,Web视图保持响应良好,可以自由执行其任务。 (建议长期运行的任务)
// Kotlin
@NativeCall( CallType . FULL_PROMISE )
fun searchContact ( contactFilter : ContactFilter ) = doInBackground< List < Contact >> { promise ->
try {
promise.resolve(contactService.search(contactFilter))
} catch (e : Exception ) {
promise.reject(e)
}
} // Javascript
Bridge . interfaces . Android . searchContact ( { surname : "Pitt" } ) . then ( ( list ) => {
console . log ( list ) ;
} ) ; 将Maven Central添加到存储库块中。
repositories {
google()
mavenCentral()
}将库添加到依赖项块中。
dependencies {
implementation ' com.github.andycandy-de:simple-android-bridge:1.1.1 '
} class AndroidNativeInterface : DefaultJSInterface ( " Android " ) {
@NativeCall( CallType . FULL_SYNC )
fun helloFullSync ( name : String ): String {
return " hello $name "
}
@NativeCall( CallType . WEB_PROMISE )
fun helloWebPromise ( name : String ): String {
return " hello $name "
}
@NativeCall( CallType . FULL_PROMISE )
fun helloFullPromise ( name : String ) = doInBackground< String > { promise ->
promise.resolve( " hello $name " )
}
} val bridge = Bridge (applicationContext, webView)
bridge.addJSInterface( AndroidNativeInterface ())Android代码
// Bridge can be initialized by calling the 'init' function inside
// the 'onPageStarted' function of a WebViewClient
webView.webViewClient = object : WebViewClient () {
override fun onPageStarted ( view : WebView ? , url : String? , favicon : Bitmap ? ) {
bridge. init ()
}
}JavaScript代码
// Bridge can be initialized by calling the 'init' function in
// Javascript. Register function to 'Bridge.afterInitialize' to
// start the webapp after the bridge is initialized.
function startApp ( f ) {
if ( Bridge . initialized ) {
f ( )
} else {
Bridge . afterInitialize = f
}
}
Bridge . init ( )
startApp ( ( ) => {
// Start your webapp
} ) ; console . log ( Bridge . interfaces . Android . helloFullSync ( "Web" ) ) 麻省理工学院许可证
版权(C)2020 Andycandy-de
版权(C)2021 Andycandy-de
版权(C)2024 Andycandy-de
特此免费授予获得此软件副本和相关文档文件副本(“软件”)的任何人,以无限制处理该软件,包括无限制的使用权,复制,复制,修改,合并,合并,发布,分发,分发,分发,订婚,和/或允许软件的副本,并允许对以下条件提供以下条件,以下是以下条件。
上述版权通知和此许可通知应包含在软件的所有副本或大量部分中。
该软件是“原样”提供的,没有任何形式的明示或暗示保证,包括但不限于适销性,特定目的的适用性和非侵权的保证。在任何情况下,作者或版权持有人均不应对任何索赔,损害赔偿或其他责任责任,无论是在合同,侵权的诉讼中还是其他索赔,与软件或使用或其他软件中的使用或其他交易有关。