Question: I need to get current location information. For now I am using [getLastKnownLocation](https://developer.android.com/reference/android/location/LocationManager#getLastKnownLocation(java.lang.String)) but sometimes it might return null or obsolete information. I'm trying to use [requestSingleUpdate](https://developer.android.com/reference/android/location/LocationManager#requestSingleUpdate(java.lang.String,%20android.app.PendingIntent)) but it deprecated in API level 30 and ask me to use [getCurrentLocation](https://developer.android.com/reference/android/location/LocationManager#getCurrentLocation(java.lang.String,%20android.os.CancellationSignal,%20java.util.concurrent.Executor,%20java.util.function.Consumer%3Candroid.location.Location%3E)) instead. How can i achieve it?

Here is my current code for reference.

```
LocationManager locationManager = (LocationManager) ApplicationProvider.getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

float accuracy = location.getAccuracy();
double altitude = location.getAltitude();
double latitude = location.getLatitude();
double longitude = location.getLongitude();
float speed = location.getSpeed();

```

Answer: 
I've solved. Here is the code.

```
locationManager.getCurrentLocation(
    LocationManager.GPS_PROVIDER,
    null,
    application.getMainExecutor(),
    new Consumer<Location>() {
  @Override
  public void accept(Location location) {
    // code
  }
});

```
N.B. Below is **deprecated in API 30** (it still works at present) - but may be useful for older target devices

In a class that implements LocationListener, e.g. an Activity:

```
val locationMgr = getSystemService(Context.LOCATION_SERVICE) as LocationManager
...
locationMgr.requestSingleUpdate(LocationManager.GPS_PROVIDER, this, null)
...
// below needed for some API versions ...
override fun onStatusChanged(provider: String, status: Int, extras: Bundle) {}

override fun onLocationChanged(location: Location) {
    val lat = location.latitude
    val lon = location.longitude
    ...do your stuff here :)
}

```