建造一座橋! 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
特此免費授予獲得此軟件副本和相關文檔文件副本(“軟件”)的任何人,以無限制處理該軟件,包括無限制的使用權,複製,複製,修改,合併,合併,發布,分發,分發,分發,訂婚,和/或允許軟件的副本,並允許對以下條件提供以下條件,以下是以下條件。
上述版權通知和此許可通知應包含在軟件的所有副本或大量部分中。
該軟件是“原樣”提供的,沒有任何形式的明示或暗示保證,包括但不限於適銷性,特定目的的適用性和非侵權的保證。在任何情況下,作者或版權持有人都不應對任何索賠,損害賠償或其他責任責任,無論是在合同,侵權的訴訟中還是其他責任,是由軟件,使用或與軟件中的使用或其他交易有關的。