当应用程序是后台时,我无法在间隔时间获得服务位置。服务工作并获取位置,但位置未更新,获取旧位置。我通过小米测试,也许miui有问题?我尝试了大约 5-10 种方式,但结果是一样的。当我打开应用程序位置获取新位置并关闭应用程序时获取旧位置,获取具有旧纬度和经度的位置
例如第一种方式:
locationManager = getApplicationContext().getSystemService(LOCATION_SERVICE) as LocationManager
isGPSEnable = locationManager!!.isProviderEnabled(LocationManager.GPS_PROVIDER)
isNetworkEnable = locationManager!!.isProviderEnabled(LocationManager.NETWORK_PROVIDER)
if (!isGPSEnable && !isNetworkEnable) {
} else {
Log.e(TAG,"${isGPSEnable} ")
if (isGPSEnable) {
location = null
locationManager!!.requestLocationUpdates(LocationManager.GPS_PROVIDER, LastKnownService.LOCATION_INTERVAL.toLong(), 0f, this)
if (locationManager != null) {
location = locationManager!!.getLastKnownLocation(LocationManager.GPS_PROVIDER)
if (App.lastKnownLocation == null){
if (location != null) {
MLog.d(TAG,"${location!!.latitude} ${location!!.longitude} == none")
App.lastKnownLocation = location
sendLocation(App.lastKnownLocation!!)
latitude = location!!.getLatitude()
longitude = location!!.getLongitude()
fn_update(location!!)
}
}else{
if (location != null) {
MLog.d(TAG,"${location!!.latitude} ${location!!.longitude} == ${location!!.distanceTo(App.lastKnownLocation)}")
App.lastKnownLocation = location
latitude = location!!.getLatitude()
longitude = location!!.getLongitude()
fn_update(location!!)
sendLocation(App.lastKnownLocation!!)
}
}
}
}else if (isNetworkEnable) {
location = null
locationManager!!.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, LastKnownService.LOCATION_INTERVAL.toLong(), 0f, this)
if (locationManager != null) {
location = locationManager!!.getLastKnownLocation(LocationManager.NETWORK_PROVIDER)
if (App.lastKnownLocation == null){
if (location != null) {
MLog.d(TAG,"${location!!.latitude} ${location!!.longitude} == none")
App.lastKnownLocation = location
sendLocation(App.lastKnownLocation!!)
latitude = location!!.getLatitude()
longitude = location!!.getLongitude()
fn_update(location!!)
}
}else{
if (location != null) {
MLog.d(TAG,"${location!!.latitude} ${location!!.longitude} == ${location!!.distanceTo(App.lastKnownLocation)}")
App.lastKnownLocation = location
latitude = location!!.getLatitude()
longitude = location!!.getLongitude()
fn_update(location!!)
sendLocation(App.lastKnownLocation!!)
}
}
}
}
}
第二种方式:
mLocationManager!!.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, LOCATION_INTERVAL.toLong(), LOCATION_DISTANCE,
mLocationListeners[1])
private var mLocationListeners = arrayOf(LocationListener(LocationManager.GPS_PROVIDER), LocationListener(LocationManager.NETWORK_PROVIDER))
private inner class LocationListener(provider: String) : android.location.LocationListener {
internal var mLastLocation: Location
init {
Log.e(TAG, "LocationListener $provider")
mLastLocation = Location(provider)
}
override fun onLocationChanged(location: Location) {
Log.e(TAG, "onLocationChanged: $location")
try{
val mBuilder = NotificationCompat.Builder(this@LocationService, "123")
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("location sended")
.setContentText("onlocation changed")
.setStyle(NotificationCompat.BigTextStyle()
.bigText("onlocation changed"))
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
if (notificationManager != null) notificationManager!!.notify(1,mBuilder.build())
val distance = location.distanceTo(mLastLocation)
if (distance > 10){
mLastLocation = location
sendLocation(mLastLocation)
}
}catch (e :java.lang.Exception){
Log.e(TAG, "send http lat lon exception: $e")
}
mLastLocation.set(location)
}
override fun onProviderDisabled(provider: String) {
Log.e(TAG, "onProviderDisabled: $provider")
}
override fun onProviderEnabled(provider: String) {
Log.e(TAG, "onProviderEnabled: $provider")
}
override fun onStatusChanged(provider: String, status: Int, extras: Bundle) {
Log.e(TAG, "onStatusChanged: $provider")
}
}