diff --git a/app/src/main/java/com/example/app/CameraActivity.kt b/app/src/main/java/com/example/app/CameraActivity.kt index f3c5a5c..1c55ad9 100644 --- a/app/src/main/java/com/example/app/CameraActivity.kt +++ b/app/src/main/java/com/example/app/CameraActivity.kt @@ -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() {