Get current location of user in Android without using GPS or internet Get current location of user in Android without using GPS or internet android android

Get current location of user in Android without using GPS or internet


What you are looking to do is get the position using the LocationManager.NETWORK_PROVIDER instead of LocationManager.GPS_PROVIDER. The NETWORK_PROVIDER will resolve on the GSM or wifi, which ever available. Obviously with wifi off, GSM will be used. Keep in mind that using the cell network is accurate to basically 500m.

http://developer.android.com/guide/topics/location/obtaining-user-location.html has some really great information and sample code.

After you get done with most of the code in OnCreate(), add this:

// Acquire a reference to the system Location ManagerLocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);// Define a listener that responds to location updatesLocationListener locationListener = new LocationListener() {    public void onLocationChanged(Location location) {      // Called when a new location is found by the network location provider.      makeUseOfNewLocation(location);    }    public void onStatusChanged(String provider, int status, Bundle extras) {}    public void onProviderEnabled(String provider) {}    public void onProviderDisabled(String provider) {}  };// Register the listener with the Location Manager to receive location updateslocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

You could also have your activity implement the LocationListener class and thus implement onLocationChanged() in your activity.


By getting the getLastKnownLocation you do not actually initiate a fix yourself.

Be aware that this could start the provider, but if the user has ever gotten a location before, I don't think it will. The docs aren't really too clear on this.

According to the docs getLastKnownLocation:

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.

Here is a quick snippet:

import android.content.Context;import android.location.Location;import android.location.LocationManager;import java.util.List;public class UtilLocation {    public static Location getLastKnownLoaction(boolean enabledProvidersOnly, Context context){        LocationManager manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);        Location utilLocation = null;        List<String> providers = manager.getProviders(enabledProvidersOnly);        for(String provider : providers){            utilLocation = manager.getLastKnownLocation(provider);            if(utilLocation != null) return utilLocation;        }        return null;    }}

You also have to add new permission to AndroidManifest.xml

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />


boolean gps_enabled = false;boolean network_enabled = false;LocationManager lm = (LocationManager) mCtx                .getSystemService(Context.LOCATION_SERVICE);gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);Location net_loc = null, gps_loc = null, finalLoc = null;if (gps_enabled)    gps_loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);if (network_enabled)    net_loc = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);if (gps_loc != null && net_loc != null) {    //smaller the number more accurate result will    if (gps_loc.getAccuracy() > net_loc.getAccuracy())         finalLoc = net_loc;    else        finalLoc = gps_loc;        // I used this just to get an idea (if both avail, its upto you which you want to take as I've taken location with more accuracy)} else {    if (gps_loc != null) {        finalLoc = gps_loc;    } else if (net_loc != null) {        finalLoc = net_loc;    }}