108 lines
3.5 KiB
Kotlin
108 lines
3.5 KiB
Kotlin
package com.stand.standapp
|
|
|
|
import android.os.Bundle
|
|
import android.view.KeyEvent
|
|
import android.view.ViewGroup
|
|
import android.widget.LinearLayout
|
|
import android.util.Log
|
|
import android.widget.Toast
|
|
import androidx.appcompat.app.AppCompatActivity
|
|
import com.just.agentweb.AgentWeb
|
|
import com.stand.standapp.R
|
|
|
|
class MainActivity : AppCompatActivity() {
|
|
|
|
private var mAgentWeb: AgentWeb? = null
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
super.onCreate(savedInstanceState)
|
|
|
|
try {
|
|
setContentView(R.layout.activity_main)
|
|
|
|
val container = findViewById<LinearLayout>(R.id.container)
|
|
if (container == null) return
|
|
|
|
// 从配置中获取服务器地址
|
|
val serverUrl = AppConfig.getServerUrl(this)
|
|
|
|
mAgentWeb = AgentWeb.with(this)
|
|
.setAgentWebParent(container, LinearLayout.LayoutParams(-1, -1))
|
|
.useDefaultIndicator()
|
|
.addJavascriptInterface("android", AndroidInterface(this))
|
|
.createAgentWeb()
|
|
.ready()
|
|
.go("https://template.gyouzhe.com/example/print_demo.html?t=\${System.currentTimeMillis()}")
|
|
|
|
// 配置 WebView 属性以支持 SSE 和跨域
|
|
mAgentWeb?.let { agent ->
|
|
try {
|
|
val webView = agent.webCreator.webView
|
|
val settings = webView.settings
|
|
// 允许混合内容 (HTTP/HTTPS)
|
|
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
|
|
settings.mixedContentMode = android.webkit.WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
|
|
}
|
|
// 允许跨域和存储
|
|
settings.allowUniversalAccessFromFileURLs = true
|
|
settings.allowFileAccessFromFileURLs = true
|
|
settings.domStorageEnabled = true
|
|
settings.javaScriptEnabled = true
|
|
} catch (e: Exception) {
|
|
Log.e("MainActivity", "WebView settings error: ${e.message}")
|
|
}
|
|
}
|
|
// .go("http://152.32.186.199/local/print_demo.html")
|
|
|
|
// 启动时检查更新
|
|
UpdateManager(this).checkUpdate()
|
|
|
|
} catch (e: Exception) {
|
|
Log.e("MainActivity", "Fatal Crash: ${e.message}")
|
|
e.printStackTrace()
|
|
}
|
|
}
|
|
|
|
fun getAgentWeb(): AgentWeb? {
|
|
return mAgentWeb
|
|
}
|
|
|
|
fun showExitDialog() {
|
|
// 调用 JS 函数
|
|
mAgentWeb?.jsAccessEntrace?.quickCallJs("addExitText")
|
|
|
|
androidx.appcompat.app.AlertDialog.Builder(this)
|
|
.setTitle("提示")
|
|
.setMessage("确定要退出应用吗?")
|
|
.setPositiveButton("确定") { _, _ -> finish() }
|
|
.setNegativeButton("取消", null)
|
|
.show()
|
|
}
|
|
|
|
override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
|
|
if (mAgentWeb?.handleKeyEvent(keyCode, event) == true) {
|
|
return true
|
|
}
|
|
if (keyCode == KeyEvent.KEYCODE_BACK) {
|
|
showExitDialog()
|
|
return true
|
|
}
|
|
return super.onKeyDown(keyCode, event)
|
|
}
|
|
|
|
override fun onPause() {
|
|
mAgentWeb?.webLifeCycle?.onPause()
|
|
super.onPause()
|
|
}
|
|
|
|
override fun onResume() {
|
|
mAgentWeb?.webLifeCycle?.onResume()
|
|
super.onResume()
|
|
}
|
|
|
|
override fun onDestroy() {
|
|
mAgentWeb?.webLifeCycle?.onDestroy()
|
|
super.onDestroy()
|
|
}
|
|
}
|