【发布时间】:2019-07-08 05:41:59
【问题描述】:
屏幕关闭时无法更新位置。
屏幕关闭后如何运行位置跟踪服务?
我开始使用服务
val serviceClass = ControlLocationService::class.java
val intent = Intent(activity, serviceClass)
activity?.startService(intent)
在 onLocationChanged 方法中,我尝试使用 Log.e() 但屏幕关闭时不会在 logcat 中显示纬度和经度
class ControlLocationService : Service() {
lateinit var locationRequest: LocationRequest
lateinit var fusedLocationProviderClient: FusedLocationProviderClient
private lateinit var locationCallback:LocationCallback
override fun onBind(intent: Intent): IBinder? {
throw UnsupportedOperationException("Not yet implemented")
}
@SuppressLint("InvalidWakeLockTag")
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
//service is started
updateLocation()
return START_STICKY
}
@SuppressLint("MissingPermission")
private fun updateLocation() {
locationCallback = object : LocationCallback() {
override fun onLocationResult(locationResult: LocationResult?) {
onLocationChanged(locationResult!!.lastLocation)
}
}
buildLocationRequest()
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this)
fusedLocationProviderClient.requestLocationUpdates(locationRequest, locationCallback, Looper.myLooper())
}
private fun onLocationChanged(location: Location) {
Log.e("...", location.latitude.toString()+location.longitude.toString())
}
private fun buildLocationRequest() {
locationRequest = LocationRequest()
locationRequest.priority = LocationRequest.PRIORITY_HIGH_ACCURACY
locationRequest.interval = 3000
locationRequest.fastestInterval = 1000
//locationRequest.smallestDisplacement = 1f
}
}
帮我介绍一下关屏时的服务管理。 谢谢。
【问题讨论】:
标签: android kotlin service location