How to Get the Last Known Location

public Location getLastKnownLocation (String provider) was added in API level 1
Returns a Location indicating the data from the last known location fix obtained from the given provider.
This can be done without starting the provider. Note that this location could be out-of-date, for example if the device was turned off and moved to another location.
If the provider is currently disabled, null is returned.
The GPS values returned by getLastKnownLocation do not change unless at least one application requests location updates.
** getLastKnownLocation does not ask the Location Provider to update the current position. If the device has not recently updated the current position, this value may not exist or be out of date. **
Parameters: provider - the name of the provider
Returns: the last known location for the provider, or null
Throws:
SecurityException - if no suitable permission is present
IllegalArgumentException - if provider is null or doesn't exist

Note: In the settings (Location) of the Android Device, the Use GPS satellites needs to be enabled for this to work.

1. In the MainActivity.java file, Declare Location object named myLocation.

2. In the onCreate method,  Determine if GPS of Android Device is enabled/disabled

3. Add the below code between the enabled brackets from above. This will set the myLocation Location object to the Last Known Location, or to null if not available. If the GPS is not enabled, this code below will not work correctly.

try { 
    myLocation = myLocationManager.getLastKnownLocation(myProvider);
} catch (SecurityException e) { 
e.printStackTrace();
    //Toast.makeText(this, "getLastKnownLocation.SecurityException", Toast.LENGTH_LONG).show();
} catch (IllegalArgumentException e) {

e.printStackTrace();
    //Toast.makeText(this, "getLastKnownLocation.IllegalArgumentException", Toast.LENGTH_LONG).show();
}

4. Compile and run!

Resources: