Improve location management in CameraActivity
This commit is contained in:
@@ -395,16 +395,60 @@ class CameraActivity : AppCompatActivity() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private var locationManagerHelper: LocationManagerHelper? = null
|
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")
|
@SuppressLint("MissingPermission")
|
||||||
private fun getCurrentLocation(): Location? {
|
private fun getCurrentLocation(): Location? {
|
||||||
// 使用LocationManagerHelper获取位置
|
return currentLocation
|
||||||
var location: Location? = null
|
|
||||||
locationManagerHelper = LocationManagerHelper(this)
|
|
||||||
locationManagerHelper?.getLastKnownLocation { loc ->
|
|
||||||
location = loc
|
|
||||||
}
|
}
|
||||||
return location
|
|
||||||
|
override fun onDestroy() {
|
||||||
|
super.onDestroy()
|
||||||
|
// 停止位置更新
|
||||||
|
locationManagerHelper?.stopLocationUpdates()
|
||||||
|
cameraExecutor.shutdown()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun openSettings() {
|
private fun openSettings() {
|
||||||
|
|||||||
Reference in New Issue
Block a user