code: public Location getLastBestLocation(int minDistance, long minTime) {
          Location bestResult = null;
          float bestAccuracy = Float.MAX_VALUE;
          long bestTime = Long.MIN_VALUE;
  
          // Iterate through all the providers on the system, keeping
          // note of the most accurate result within the acceptable time limit.
          // If no result is found within maxTime, return the newest Location.
          List<String> matchingProviders = locationManager.getAllProviders();
          for (String provider : matchingProviders) {
              Location location = locationManager.getLastKnownLocation(provider);
              if (location != null) {
                  float accuracy = location.getAccuracy();
                  long time = location.getTime();
  
                  if ((time > minTime && accuracy < bestAccuracy)) {
                      bestResult = location;
                      bestAccuracy = accuracy;
                      bestTime = time;
                  } else if (time < minTime && bestAccuracy == Float.MAX_VALUE && time > bestTime) {
                      bestResult = location;
                      bestTime = time;
                  }
              }
          }
  
          // If the best result is beyond the allowed time limit, or the accuracy of the
          // best result is wider than the acceptable maximum distance, request a single update.
          // This check simply implements the same conditions we set when requesting regular
          // location updates every [minTime] and [minDistance].
          if (locationListener != null && (bestTime < minTime || bestAccuracy > minDistance)) {
              IntentFilter locIntentFilter = new IntentFilter(SINGLE_LOCATION_UPDATE_ACTION);
              context.registerReceiver(singleUpdateReceiver, locIntentFilter);
              locationManager.requestSingleUpdate(criteria, pendingIntent);
          }
  
          return bestResult;
      }
docs: Returns the most accurate and timely previously detected location.
  Where the last result is beyond the specified maximum distance or
  latency a one-off location update is returned via the {@link LocationListener}
  
  @param minDistance Minimum distance before we require a location update.
  @param minTime     Minimum time required between location updates.
  @return The most accurate and / or timely previously detected location.