Here you can find the source of getLastKnownLocation(Context context)
public static Location getLastKnownLocation(Context context)
//package com.java2s; import android.content.Context; import android.content.SharedPreferences; import android.location.Location; import android.location.LocationManager; import android.preference.PreferenceManager; import java.util.List; public class Main { public static final String LOCATION_IS_LOATION_AVA = "isLocationAvailable"; public static Location getLastKnownLocation(Context context) { LocationManager lm = (LocationManager) context .getSystemService(Context.LOCATION_SERVICE); List<String> providers = lm.getProviders(true); SharedPreferences settings = PreferenceManager .getDefaultSharedPreferences(context); SharedPreferences.Editor settingsEditor = settings.edit(); // Loop over the array backwards (more accurate) // if you get an accurate location, then break out the loop Location loc = null;/*from w w w . j a v a 2 s. co m*/ for (int i = providers.size() - 1; i >= 0; i--) { loc = lm.getLastKnownLocation(providers.get(i)); if (loc != null) break; } // Since the user disabled GPS, we assume they are in downtown Toronto if (loc == null) { settingsEditor.putBoolean(LOCATION_IS_LOATION_AVA, false); loc = new Location("Mock Location"); loc.setLatitude(43.6481); loc.setLongitude(-79.4042); } else { settingsEditor.putBoolean(LOCATION_IS_LOATION_AVA, true); } settingsEditor.commit(); return loc; } }