简体   繁体   English

在前台服务中获取当前位置

[英]Get current location in foreground service

I run my foreground service every 3 minutes. 我每3分钟运行一次前台服务。 Therefore my goal is: Run a foreground service, get the current user location and then stop the foreground service. 因此,我的目标是:运行前台服务,获取当前用户位置,然后停止前台服务。 So now I use this: 所以现在我用这个:

locationCallback = object : LocationCallback(){
        override fun onLocationAvailability(locationAvailability: LocationAvailability?) {
            super.onLocationAvailability(locationAvailability)
            if(locationAvailability!!.isLocationAvailable){
                fusedLocationClient.lastLocation.addOnSuccessListener { location: Location? ->
                    if (location != null) {
                       // do my action with this location and finish the foreground service
                    }
                }
            }else{
                prepareFinishService() // finish service
            }
        }
    }
    fusedLocationClient.requestLocationUpdates(locationRequest,
            locationCallback, Looper.getMainLooper())

And my locationRequest: 和我的locationRequest:

fun createLocationRequest(): LocationRequest {
    val locationRequest = LocationRequest.create()
    locationRequest.interval = 10000L * 10
    locationRequest.fastestInterval = 50000L
    locationRequest.smallestDisplacement = 10f
    locationRequest.priority = LocationRequest.PRIORITY_HIGH_ACCURACY
    return locationRequest
}

But in this solution I get not fresh location. 但是在这种解决方案中,我找不到新的位置。 For example user change location but onLocationAvailability retrun previous location. 例如,用户更改位置,但是onLocationAvailability先前的位置。 After some time for example after 15 minutes when my service run it return a updated location. 经过一段时间(例如,在我的服务运行15分钟后),它将返回更新的位置。 I know that I can add also this callback : 我知道我也可以添加此回调:

override fun onLocationResult(locationResult: LocationResult?) {
     // my action with location
}

But as per documentation : onLocationResult 但是根据文档: onLocationResult

"Even when isLocationAvailable() returns true the onLocationResult(LocationResult) may not always be called regularly, however the device location is known and both the most recently delivered location and getLastLocation(GoogleApiClient) will be reasonably up to date given the hints specified by the active LocationRequests." “即使isLocationAvailable()返回true时,也可能不会始终定期调用onLocationResult(LocationResult),但是设备位置是已知的,并且根据指定的提示,最近交付的位置和getLastLocation(GoogleApiClient)都将保持最新状态。有效的LocationRequests。”

It is possible that this function will not be called and my service will run all the time until the next start. 可能不会调用此函数,并且我的服务将一直运行直到下一次启动。 I do not want this situation. 我不希望这种情况。 Is there any solution to always access the current service and then stop the service? 有什么解决方案可以始终访问当前服务然后停止该服务?

For example user change location but onLocationAvailability retrun previous location. 例如,用户更改位置,但是onLocationAvailability重新运行先前的位置。

This is what getLastLocation does. 这就是getLastLocation所做的。 It returns the last known location, it doesn't mean the newest location. 它返回最后一个已知的位置,并不意味着最新的位置。 According to the documentation : 根据文档:

It is particularly well suited for applications that do not require an accurate location [...] 它特别适合不需要精确位置的应用程序。

That said, for a decent accuracy, you should use requestLocationUpdates . 就是说,为了获得不错的准确性,您应该使用requestLocationUpdates But don't start/stop you service manually. 但是,请勿手动启动/停止服务。 If you want regular updates on the user location, just set the right parameters. 如果要定期更新用户位置,只需设置正确的参数即可。

LocationRequest locationRequest = new LocationRequest();
locationRequest.setInterval(1000*60*3); // max: 3 min
locationRequest.setFastestInterval(1000*60*1); // min: 1 min
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM