Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
p x
GeelyHMIWeb
Commits
5b3f0dc5
Commit
5b3f0dc5
authored
Jan 04, 2026
by
p x
Browse files
解决模型不能加载的问题
parent
6a41fa11
Changes
4
Hide whitespace changes
Inline
Side-by-side
app/build.gradle.kts
View file @
5b3f0dc5
...
@@ -51,6 +51,7 @@ dependencies {
...
@@ -51,6 +51,7 @@ dependencies {
androidTestImplementation
(
libs
.
androidx
.
junit
)
androidTestImplementation
(
libs
.
androidx
.
junit
)
androidTestImplementation
(
libs
.
androidx
.
espresso
.
core
)
androidTestImplementation
(
libs
.
androidx
.
espresso
.
core
)
implementation
(
"androidx.webkit:webkit:1.15.0"
)
// ViewModel + coroutines
// ViewModel + coroutines
implementation
(
"androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.2"
)
implementation
(
"androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.2"
)
// Hilt dependencies
// Hilt dependencies
...
...
app/src/main/java/com/sd/geelyhmiweb/JsApi.kt
View file @
5b3f0dc5
...
@@ -25,7 +25,7 @@ class JsApi {
...
@@ -25,7 +25,7 @@ class JsApi {
@JavascriptInterface
@JavascriptInterface
fun
writeLog
(
msg
:
Object
)
{
fun
writeLog
(
msg
:
Object
)
{
println
(
"------------writeLog ${msg}"
)
//
println("------------writeLog ${msg}")
mainVM
?.
writeLog
(
msg
.
toString
())
mainVM
?.
writeLog
(
msg
.
toString
())
// FileIoUtils.writeToFile(gson.toJson(perceptionBean), "PerTarget_15_move.txt")
// FileIoUtils.writeToFile(gson.toJson(perceptionBean), "PerTarget_15_move.txt")
}
}
...
...
app/src/main/java/com/sd/geelyhmiweb/MainActivity.kt
View file @
5b3f0dc5
...
@@ -14,11 +14,15 @@ import android.webkit.WebView
...
@@ -14,11 +14,15 @@ import android.webkit.WebView
import
android.webkit.WebViewClient
import
android.webkit.WebViewClient
import
androidx.activity.viewModels
import
androidx.activity.viewModels
import
androidx.appcompat.app.AppCompatActivity
import
androidx.appcompat.app.AppCompatActivity
import
androidx.webkit.WebResourceErrorCompat
import
androidx.webkit.WebViewAssetLoader
import
androidx.webkit.WebViewClientCompat
import
com.sd.geelyhmiweb.databinding.ActivityMainBinding
import
com.sd.geelyhmiweb.databinding.ActivityMainBinding
import
com.sd.geelyhmiweb.viewmodels.MainVM
import
com.sd.geelyhmiweb.viewmodels.MainVM
import
dagger.hilt.android.AndroidEntryPoint
import
dagger.hilt.android.AndroidEntryPoint
import
kotlin.getValue
import
kotlin.getValue
@AndroidEntryPoint
@AndroidEntryPoint
class
MainActivity
:
AppCompatActivity
()
{
class
MainActivity
:
AppCompatActivity
()
{
...
@@ -26,6 +30,13 @@ class MainActivity : AppCompatActivity() {
...
@@ -26,6 +30,13 @@ class MainActivity : AppCompatActivity() {
private
val
mainVM
:
MainVM
by
viewModels
()
private
val
mainVM
:
MainVM
by
viewModels
()
private
lateinit
var
assetLoader
:
WebViewAssetLoader
override
fun
onDestroy
()
{
super
.
onDestroy
()
binding
.
dwebview
.
destroy
()
}
override
fun
onCreate
(
savedInstanceState
:
Bundle
?)
{
override
fun
onCreate
(
savedInstanceState
:
Bundle
?)
{
super
.
onCreate
(
savedInstanceState
)
super
.
onCreate
(
savedInstanceState
)
binding
=
ActivityMainBinding
.
inflate
(
layoutInflater
)
binding
=
ActivityMainBinding
.
inflate
(
layoutInflater
)
...
@@ -38,6 +49,13 @@ class MainActivity : AppCompatActivity() {
...
@@ -38,6 +49,13 @@ class MainActivity : AppCompatActivity() {
var
jsApi
=
JsApi
()
var
jsApi
=
JsApi
()
jsApi
.
mainVM
=
mainVM
jsApi
.
mainVM
=
mainVM
assetLoader
=
WebViewAssetLoader
.
Builder
()
.
setHttpAllowed
(
true
)
.
addPathHandler
(
"/assets/"
,
WebViewAssetLoader
.
AssetsPathHandler
(
this
))
.
build
()
binding
.
dwebview
.
clearCache
(
true
)
binding
.
dwebview
.
clearHistory
()
binding
.
dwebview
.
addJavascriptObject
(
jsApi
,
null
)
binding
.
dwebview
.
addJavascriptObject
(
jsApi
,
null
)
// DWebView.setWebContentsDebuggingEnabled(true)
// DWebView.setWebContentsDebuggingEnabled(true)
binding
.
dwebview
.
settings
.
run
{
binding
.
dwebview
.
settings
.
run
{
...
@@ -45,32 +63,47 @@ class MainActivity : AppCompatActivity() {
...
@@ -45,32 +63,47 @@ class MainActivity : AppCompatActivity() {
javaScriptEnabled
=
true
javaScriptEnabled
=
true
javaScriptCanOpenWindowsAutomatically
=
true
javaScriptCanOpenWindowsAutomatically
=
true
loadWithOverviewMode
=
true
loadWithOverviewMode
=
true
pluginState
=
WebSettings
.
PluginState
.
ON
domStorageEnabled
=
true
domStorageEnabled
=
true
setAllowFileAccess
(
true
)
setAllowContentAccess
(
true
)
databaseEnabled
=
true
databaseEnabled
=
true
//解决跨域问题
// 关键:禁用文件访问,强制走 AssetLoader 路由
setAllowFileAccessFromFileURLs
(
true
)
setAllowFileAccess
(
false
)
setAllowUniversalAccessFromFileURLs
(
true
)
setAllowContentAccess
(
false
)
setAllowFileAccessFromFileURLs
(
false
)
setAllowUniversalAccessFromFileURLs
(
false
)
// 混合内容模式(若前端有 HTTP 资源,需配置;正式环境建议仅 HTTPS)
setMixedContentMode
(
WebSettings
.
MIXED_CONTENT_ALWAYS_ALLOW
)
// 关键:禁用WebView的DNS缓存,强制走拦截逻辑
setCacheMode
(
WebSettings
.
LOAD_NO_CACHE
)
// 禁用网络加载(仅加载本地资产)
blockNetworkLoads
=
true
}
}
val
url
=
"file:///android_asset/dist/index.html"
val
url
=
"file:///android_asset/dist/index.html"
// val url = "http://192.168.60.244:5100/#/"
// val url = "http://192.168.60.155:5173/"
val
websiteUrl
=
"http://appassets.androidplatform.net/assets/dist/index.html"
binding
.
dwebview
.
loadUrl
(
u
rl
)
binding
.
dwebview
.
loadUrl
(
websiteU
rl
)
binding
.
dwebview
.
webViewClient
=
object
:
WebViewClient
()
{
binding
.
dwebview
.
webViewClient
=
object
:
WebViewClient
Compat
()
{
override
fun
shouldInterceptRequest
(
override
fun
shouldInterceptRequest
(
view
:
WebView
?
,
view
:
WebView
,
request
:
WebResourceRequest
request
:
WebResourceRequest
):
WebResourceResponse
?
{
):
WebResourceResponse
?
{
// var headers=request.requestHeaders
// var headers=request.requestHeaders
// headers.put("Referer", "https://www.tianditu.gov.cn/")
// headers.put("Referer", "https://www.tianditu.gov.cn/")
// headers.put("sec-ch-ua-platform", "")
// headers.put("sec-ch-ua-platform", "")
// headers.put("","Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:139.0) Gecko/20100101 Firefox/139.0")
// headers.put("","Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:139.0) Gecko/20100101 Firefox/139.0")
return
super
.
shouldInterceptRequest
(
view
,
request
)
// 优先拦截虚拟域名请求
return
assetLoader
.
shouldInterceptRequest
(
request
.
url
)
// return super.shouldInterceptRequest(view, request)
}
}
override
fun
onReceivedHttpError
(
view
:
WebView
,
request
:
WebResourceRequest
,
errorResponse
:
WebResourceResponse
)
{
super
.
onReceivedHttpError
(
view
,
request
,
errorResponse
)
}
override
fun
onReceivedSslError
(
override
fun
onReceivedSslError
(
view
:
WebView
?,
view
:
WebView
?,
...
@@ -78,24 +111,9 @@ class MainActivity : AppCompatActivity() {
...
@@ -78,24 +111,9 @@ class MainActivity : AppCompatActivity() {
error
:
SslError
?
error
:
SslError
?
)
{
)
{
// super.onReceivedSslError(view, handler, error)
// super.onReceivedSslError(view, handler, error)
handler
?.
proceed
()
handler
?.
cancel
()
}
}
override
fun
onReceivedError
(
view
:
WebView
?,
request
:
WebResourceRequest
?,
error
:
WebResourceError
?
)
{
super
.
onReceivedError
(
view
,
request
,
error
)
}
override
fun
onReceivedHttpError
(
view
:
WebView
?,
request
:
WebResourceRequest
?,
errorResponse
:
WebResourceResponse
?
)
{
super
.
onReceivedHttpError
(
view
,
request
,
errorResponse
)
}
}
}
binding
.
dwebview
.
webChromeClient
=
object
:
WebChromeClient
()
{
binding
.
dwebview
.
webChromeClient
=
object
:
WebChromeClient
()
{
...
...
app/src/main/java/com/sd/geelyhmiweb/viewmodels/LookInfoVM.kt
0 → 100644
View file @
5b3f0dc5
package
com.sd.geelyhmiweb.viewmodels
import
android.app.ActivityManager
import
android.content.Context
import
android.util.Log
import
androidx.lifecycle.ViewModel
import
dagger.hilt.android.lifecycle.HiltViewModel
import
dagger.hilt.android.qualifiers.ApplicationContext
import
javax.inject.Inject
@HiltViewModel
class
LookInfoVM
@Inject
constructor
(
// private var loginRepo: LoginRepo,
@ApplicationContext
var
context
:
Context
)
:
ViewModel
()
{
private
val
activityManager
=
context
.
getSystemService
(
Context
.
ACTIVITY_SERVICE
)
as
ActivityManager
/**
* 获取当前应用的内存使用信息
*/
fun
printMemoryInfo
(
context
:
Context
)
{
val
memoryInfo
=
ActivityManager
.
MemoryInfo
()
activityManager
.
getMemoryInfo
(
memoryInfo
)
// 总内存
val
totalMemory
=
memoryInfo
.
totalMem
// 可用内存
val
availableMemory
=
memoryInfo
.
availMem
// 内存使用率
val
memoryUsage
=
(
1
-
availableMemory
.
toDouble
()
/
totalMemory
)
*
100
// 是否低内存运行
val
lowMemory
=
memoryInfo
.
lowMemory
// 低内存阈值
val
threshold
=
memoryInfo
.
threshold
Log
.
d
(
"MemoryInfo"
,
"总内存: "
+
formatSize
(
totalMemory
))
Log
.
d
(
"MemoryInfo"
,
"可用内存: "
+
formatSize
(
availableMemory
))
Log
.
d
(
"MemoryInfo"
,
"内存使用率: "
+
String
.
format
(
"%.2f"
,
memoryUsage
)
+
"%"
)
// Log.d("MemoryInfo", "低内存状态: " + lowMemory)
// Log.d("MemoryInfo", "低内存阈值: " + formatSize(threshold))
}
private
fun
formatSize
(
size
:
Long
):
String
{
if
(
size
<
1024
)
{
return
size
.
toString
()
+
" B"
}
else
if
(
size
<
1024
*
1024
)
{
return
String
.
format
(
"%.2f KB"
,
size
/
1024.0
)
}
else
if
(
size
<
1024
*
1024
*
1024
)
{
return
String
.
format
(
"%.2f MB"
,
size
/
(
1024.0
*
1024.0
))
}
else
{
return
String
.
format
(
"%.2f GB"
,
size
/
(
1024.0
*
1024.0
*
1024.0
))
}
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment