WebAppInterfaced.kt
Код: Выделить всё
package com.example.barangkalearninghub
import android.content.Context
import android.webkit.JavascriptInterface
import android.webkit.WebView
class WebAppInterfaced(val webview: WebView) {
@JavascriptInterface
fun PageRedirect(newUri: String) {
webview.loadUrl(newUri)
}
}
Код: Выделить всё
package com.example.barangkalearninghub
import android.annotation.SuppressLint
import android.os.Bundle
import android.webkit.WebChromeClient
import android.webkit.WebView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
@SuppressLint("SetJavaScriptEnabled")
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_main)
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
insets
}
val webView:WebView = findViewById(R.id.webViewMain)
webView.webChromeClient = WebChromeClient()
WebView.setWebContentsDebuggingEnabled(true)
val jsInterface:WebAppInterfaced = WebAppInterfaced(webView);
webView.addJavascriptInterface(jsInterface, "WebAppInterfaced")
webView.settings.databaseEnabled = true
webView.settings.allowFileAccess = true
webView.settings.javaScriptEnabled = true
webView.settings.domStorageEnabled = true
webView.settings.useWideViewPort = true
webView.loadUrl("file:///android_asset/index.html")
}
}
Код: Выделить всё
window.addEventListener('load', function(){
var p = $("#demo");
p.html("hello jquery android");
setTimeout(function(){
WebAppInterfaced.PageRedirect("dashboard.html");
}, 5000);
});
Не работает. Я попробовал вывести строку для проверки, действительно ли js-интерфейс доступен и работает ли он. Однако когда я использую webview.loadurl, возникает ошибка.
Подробнее здесь: https://stackoverflow.com/questions/790 ... in-android