fix: 修复 MainActivity 多行字符串格式和 UI 线程安全问题
- 使用 Kotlin 原始字符串语法替代错误的拼接格式 - 修复 stopCamera() 中 UI 更新不在主线程的问题 - 添加 cameraHelper 重复启动检查 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -48,8 +48,11 @@ class MainActivity : AppCompatActivity() {
|
||||
Toast.makeText(this, "图片已保存到相册", Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
|
||||
// 自动启动
|
||||
if (checkPermission()) {
|
||||
if (ContextCompat.checkSelfPermission(
|
||||
this,
|
||||
Manifest.permission.CAMERA
|
||||
) == PackageManager.PERMISSION_GRANTED
|
||||
) {
|
||||
startCamera()
|
||||
}
|
||||
}
|
||||
@@ -68,15 +71,23 @@ class MainActivity : AppCompatActivity() {
|
||||
}
|
||||
|
||||
private fun startCamera() {
|
||||
if (cameraHelper != null) return
|
||||
|
||||
mjpegServer = MjpegServer(8080)
|
||||
cameraHelper = CameraHelper(this) { frame, _, _ ->
|
||||
mjpegServer?.updateFrame(frame)
|
||||
}
|
||||
|
||||
mjpegServer = MjpegServer(8080)
|
||||
// Start the server and update UI once started
|
||||
mjpegServer?.start {
|
||||
runOnUiThread {
|
||||
findViewById<TextView>(R.id.tv_status).text =
|
||||
"服务运行中端口: 8080IP: 无需IP (ADB模式)USB连接命令:adb forward tcp:8080 tcp:8080"
|
||||
findViewById<TextView>(R.id.tv_status).text = """
|
||||
服务运行中
|
||||
端口: 8080
|
||||
IP: 无需IP (ADB模式)
|
||||
USB连接命令:
|
||||
adb forward tcp:8080 tcp:8080
|
||||
""".trimIndent()
|
||||
findViewById<Button>(R.id.btn_start).isEnabled = false
|
||||
findViewById<Button>(R.id.btn_stop).isEnabled = true
|
||||
}
|
||||
@@ -87,11 +98,15 @@ class MainActivity : AppCompatActivity() {
|
||||
|
||||
private fun stopCamera() {
|
||||
cameraHelper?.stop()
|
||||
cameraHelper = null
|
||||
mjpegServer?.stop()
|
||||
mjpegServer = null
|
||||
|
||||
findViewById<TextView>(R.id.tv_status).text = "服务已停止"
|
||||
findViewById<Button>(R.id.btn_start).isEnabled = true
|
||||
findViewById<Button>(R.id.btn_stop).isEnabled = false
|
||||
runOnUiThread {
|
||||
findViewById<TextView>(R.id.tv_status).text = "服务已停止"
|
||||
findViewById<Button>(R.id.btn_start).isEnabled = true
|
||||
findViewById<Button>(R.id.btn_stop).isEnabled = false
|
||||
}
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
|
||||
Reference in New Issue
Block a user