Kotlin:FusedLocationProviderClient.removeLocationUpdates 总是 return 错误

Kotlin: FusedLocationProviderClient.removeLocationUpdates always return false

这个问题似乎是 this one but trust me I've tried the solutions but couldn't figure out the reason behind my problem. I'm a newbie in Kotlin and I'm requesting location updates 与 FusedLocationProvider 的重复。

相关代码:

private lateinit  var mLocationCallback: LocationCallback? = null
private lateinit var mFusedLocationProviderClient: FusedLocationProviderClient
private lateinit var mLocationRequest: LocationRequest

mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(mContext)
// Create a LocationRequest
mLocationRequest = LocationRequest.create().apply {
    interval = java.util.concurrent.TimeUnit.SECONDS.toMillis(1000)
    smallestDisplacement = 0f
    // Sets the fastest rate for active location updates. This interval is exact, and your
    // application will never receive updates more frequently than this value.
    fastestInterval = java.util.concurrent.TimeUnit.SECONDS.toMillis(1000)
    // Sets the maximum time when batched location updates are delivered. Updates may be
    // delivered sooner than this interval.
    maxWaitTime = java.util.concurrent.TimeUnit.MINUTES.toMillis(1000)
    priority = LocationRequest.PRIORITY_HIGH_ACCURACY
}

// Initialize the LocationCallback.
mLocationCallback = object : LocationCallback() {
    override fun onLocationResult(locationResult: LocationResult) {
        super.onLocationResult(locationResult)
        // Normally, you want to save a new location to a database. We are simplifying
        // things a bit and just saving it as a local variable, as we only need it again
        mCurrentLocation = locationResult.lastLocation
        //Get Date Time
        // saveLatLngData(this,mCurrentLocation.latitude.toString(),mCurrentLocation.longitude.toString())
        mTimeStamp = Calendar.getInstance().time
        Log.d(TAG, "LocationCallback=> TimeStamp: " + mDateFormat!!.format(mTimeStamp) + " Latitude: " + mCurrentLocation!!.latitude + " - Longitude: " + mCurrentLocation!!.longitude + " - Altitude: " + mCurrentLocation!!.altitude)
        mNotificationText = mDateFormat!!.format(mTimeStamp) + "\nLat: ${locationResult.lastLocation.latitude} | Long: ${locationResult.lastLocation.longitude}"
        createForegroundInfo()
    }
}

// Subscribe to location changes.
if (ActivityCompat.checkSelfPermission(mContext,Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(mContext,Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
    if(mFusedLocationProviderClient != null)
    {
        mFusedLocationProviderClient.requestLocationUpdates(mLocationRequest,mLocationCallback,Looper.getMainLooper())
        Log.d(TAG, "startMonitoring: mFusedLocationProviderClient is registered now.")
    }
    else
    {
        Log.d(TAG, "startMonitoring: mFusedLocationProviderClient is not initialized!")
    }
    return
}

//STOPPING LOCATION UPDATES (AFTER GETTING SOME VALUES) USING THE FOLLOWING CODE
if (mFusedLocationProviderClient != null) {
    try {
        val voidTask: Task<Void> = mFusedLocationProviderClient.removeLocationUpdates(mLocationCallback)
        if (voidTask.isSuccessful)
        {
            Log.d(TAG, "stopMonitoring: removeLocationUpdates successful.")
        }
        else
        {
            Log.d(TAG, "stopMonitoring: removeLocationUpdates updates unsuccessful! " + voidTask.toString())
        }
    }
    catch (exp: SecurityException)
    {
        Log.d(TAG, "stopMonitoring: Security exception.")
    }
}

预期行为:

调用 mFusedLocationProviderClient.removeLocationUpdates(mLocationCallback) 方法后,应删除位置更新并在记录器上打印 stopMonitoring: removeLocationUpdates successful.

问题:

位置更新没有被删除,因为我在记录器上看到 stopMonitoring: removeLocationUpdates updates unsuccessful!,这意味着 else 条件已执行。

我试图找出原因并应用了一些解决方案。请指导我一些可能的选择。

A Task 需要一些时间才能完成。您希望它立即完成,这不太可能发生。你应该这样检查:

try {
    val voidTask: Task<Void> = mFusedLocationProviderClient.removeLocationUpdates(mLocationCallback)
    voidTask.addOnCompleteListener {
        if(it.isSuccessful) {
            Log.d("Location", "stopMonitoring: removeLocationUpdates successful.")
        } else {
            Log.d("Location", "stopMonitoring: removeLocationUpdates updates unsuccessful! " + voidTask.toString())
        }
    }
} catch (exp: SecurityException){
    Log.d("Location", "stopMonitoring: Security exception.")
}