Improve location management in CameraActivity

This commit is contained in:
xiajiid
2026-02-08 12:18:29 +08:00
parent 089671436b
commit f08c472265

View File

@@ -395,16 +395,60 @@ class CameraActivity : AppCompatActivity() {
}
private var locationManagerHelper: LocationManagerHelper? = null
private var currentLocation: Location? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// 初始化视图
viewFinder = findViewById(R.id.viewFinder)
captureButton = findViewById(R.id.captureButton)
settingsButton = findViewById(R.id.settingsButton)
photoPreviewLayout = findViewById(R.id.photoPreviewLayout)
// 请求权限
if (allPermissionsGranted()) {
startCamera()
// 初始化位置管理器
initLocationManager()
} else {
ActivityCompat.requestPermissions(
this, REQUIRED_PERMISSIONS, REQUEST_CODE_PERMISSIONS
)
}
// 设置点击监听器
captureButton.setOnClickListener { takePhoto() }
settingsButton.setOnClickListener { openSettings() }
// 初始化线程池
cameraExecutor = Executors.newSingleThreadExecutor()
}
private fun initLocationManager() {
locationManagerHelper = LocationManagerHelper(this)
// 尝试获取最后已知位置
locationManagerHelper?.getLastKnownLocation { location ->
this.currentLocation = location
}
// 开始实时位置更新
locationManagerHelper?.requestLocationUpdates { location ->
this.currentLocation = location
}
}
@SuppressLint("MissingPermission")
private fun getCurrentLocation(): Location? {
// 使用LocationManagerHelper获取位置
var location: Location? = null
locationManagerHelper = LocationManagerHelper(this)
locationManagerHelper?.getLastKnownLocation { loc ->
location = loc
}
return location
return currentLocation
}
override fun onDestroy() {
super.onDestroy()
// 停止位置更新
locationManagerHelper?.stopLocationUpdates()
cameraExecutor.shutdown()
}
private fun openSettings() {