Phone’s last known location using LocationManager (android)

This is a fast code to get the last known location of the phone. If there is no exact gps-information it falls back to the network-based location info. This code is using LocationManager.

private double[] getGPS() {
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
List providers = lm.getProviders(true);

/* Loop over the array backwards, and if you get an accurate location, then break out the loop*/
Location loc = null;

for (int i=providers.size()-1; i>=0; i–) {
loc = lm.getLastKnownLocation(providers.get(i));
if (loc != null) break;

double[] gps = new double[2];
if (loc != null) {
gps[0] = loc.getLatitude();
gps[1] = loc.getLongitude();
return gps;

Incoming search terms:

  • android device manager last known location
  • android last known location
  • last known location android
  • google device manager last known location
  • phone last known location
  • genymotion getlastknownlocation
  • last known location of android phone

Tushar Roy

Software Engineer (Backend engineer), Android Developer,

Leave a Reply

Your email address will not be published. Required fields are marked *