code: protected static Location getLocation() {
          Context applicationContext = getApplicationContext();
  
          if (applicationContext == null)
              return null;
  
          LocationManager locationManager = (LocationManager) applicationContext.getSystemService(Context.LOCATION_SERVICE);
          try {
              Location best = null;
              for (String provider : locationManager.getAllProviders()) {
                  Location location;
                  try {
                      location = locationManager.getLastKnownLocation(provider);
                  } catch (SecurityException ex) {
                      continue;
                  }
                  // If this location is null, discard
                  if (null == location) {
                      continue;
                  }
  
                  // If no, broken or poor accuracy, discard
                  if (location.getAccuracy() <= 0 || location.getAccuracy() >= 10000) {
                      continue;
                  }
  
                  // Skip locations old enough to belong to an older session
                  if (location.getTime() < System.currentTimeMillis() - WonderPush.DIFFERENT_SESSION_REGULAR_MIN_TIME_GAP) {
                      continue;
                  }
  
                  // If we have no best yet, use this first location
                  if (null == best) {
                      best = location;
                      continue;
                  }
  
                  // If this location is more than 2 minutes older than the current best, discard
                  if (location.getTime() < best.getTime() - 2 * 60 * 1000) {
                      continue;
                  }
  
                  // If this location is less precise (ie. has a *larger* accuracy radius), discard
                  if (location.getAccuracy() > best.getAccuracy()) {
                      continue;
                  }
  
                  best = location;
              }
  
              return best;
          } catch (java.lang.SecurityException e) {
              // Missing permission;
              return null;
          }
      }
docs: Returns the last known location of the {@link LocationManager}
  or null if permission was not given.