Example usage for android.location LocationManager getLastKnownLocation

List of usage examples for android.location LocationManager getLastKnownLocation

Introduction

In this page you can find the example usage for android.location LocationManager getLastKnownLocation.

Prototype

@RequiresPermission(anyOf = { ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION })
public Location getLastKnownLocation(String provider) 

Source Link

Document

Returns a Location indicating the data from the last known location fix obtained from the given provider.

Usage

From source file:Main.java

/**
 * This method checks phone if it is able to get user's location
 * @param context//from  ww w . j  av a  2s . c om
 * @return
 */
public static boolean testLocationServices(Context context) {

    LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    Location locationGPS = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    Location locationNet = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

    boolean haveGPS = (locationGPS != null);
    boolean haveNet = (locationNet != null);

    return (haveGPS || haveNet);
}

From source file:Main.java

public static Location getLocation(Context context) {
    try {/*ww  w .  ja v  a  2s.  com*/
        LocationManager locMan = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        Location location = locMan.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if (location == null) {
            location = locMan.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        }
        return location;
    } catch (Exception e) {
    }
    return null;
}

From source file:Main.java

public static String getCountry(Context context) {
    @SuppressWarnings("static-access")
    LocationManager lm = (LocationManager) context.getSystemService(context.LOCATION_SERVICE);
    Location l = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    Geocoder gc = new Geocoder(context);

    try {//from  ww  w .  j a v  a  2s .c o m
        List<Address> address = gc.getFromLocation(l.getLatitude(), l.getLongitude(), 1);
        if (address != null && address.isEmpty() == false)
            return address.get(0).getCountryName();
    } catch (IOException e) {
        return "";
    }

    return "";
}

From source file:travel.izi.api.sample.util.Locations.java

@NonNull
public static Location currentLocation(@NonNull Context context) {
    String locationProvider = NETWORK_PROVIDER;
    if (ContextCompat.checkSelfPermission(context, ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
            && ContextCompat.checkSelfPermission(context,
                    ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return new Location(locationProvider);
    }/*from  ww  w.  j  a  va 2  s .com*/
    LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    Location lastKnownLocation = locationManager.getLastKnownLocation(locationProvider);
    return lastKnownLocation != null ? lastKnownLocation : new Location(locationProvider);
}

From source file:Main.java

private static Location getLastBestLocation(Context pContext) {
    LocationManager locationManager = (LocationManager) pContext.getSystemService(Context.LOCATION_SERVICE);
    Location locationGPS = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    Location locationNet = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

    long GPSLocationTime = 0;
    if (null != locationGPS) {
        GPSLocationTime = locationGPS.getTime();
    }//w  w  w .jav a 2  s.  c o  m

    long NetLocationTime = 0;
    if (null != locationNet) {
        NetLocationTime = locationNet.getTime();
    }

    if (0 < GPSLocationTime - NetLocationTime) {
        Log.e(TAG, "Located by GPS");
        return locationGPS;
    } else {
        Log.e(TAG, "Located by network");
        return locationNet;
    }
}

From source file:Main.java

public static String getLocation(Context context) {
    LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    String provider = LocationManager.NETWORK_PROVIDER;
    Location location = locationManager.getLastKnownLocation(provider);
    return location.getLatitude() + ":" + location.getLongitude();
}

From source file:Main.java

/**
 * This method returns the last,best known current location for user
 * @param context/*from   w  ww.  j  ava 2  s. co  m*/
 * @return
 */
public static Location getLastBestLocation(Context context) {

    // get location providers
    LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    Location locationGPS = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    Location locationNet = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

    // check which ones are available
    boolean haveGPS = (locationGPS != null);
    boolean haveNet = (locationNet != null);

    if (!haveGPS && !haveNet) { // no providers available
        String msg = "Could not find a location. Using random location now.";
        displayAlert(context, msg);
        Location l = new Location("yum fake location");
        l.setLatitude(45.0);
        l.setLongitude(45.0);
        // uh oh... no location
        return new Location(l);

    } else if (haveGPS && !haveNet) { // only gps available

        return locationGPS;

    } else if (!haveGPS && haveNet) { // only cell location available

        return locationNet;

    } else { // choose the most recent one

        long GPSLocationTime = locationGPS.getTime();
        long NetLocationTime = locationNet.getTime();

        if (GPSLocationTime - NetLocationTime > 0) {

            return locationGPS;

        } else {

            return locationNet;

        }
    }

}

From source file:Main.java

public static String getLocality(Context context) {
    @SuppressWarnings("static-access")
    LocationManager lm = (LocationManager) context.getSystemService(context.LOCATION_SERVICE);
    if (lm == null)
        return "";
    Location l = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    if (l == null)
        return "";
    Geocoder gc = new Geocoder(context);

    try {//from  w  w w.j  a v  a  2  s  .  co m
        List<Address> address = gc.getFromLocation(l.getLatitude(), l.getLongitude(), 1);
        if (address != null && address.isEmpty() == false)
            return address.get(0).getLocality();
    } catch (Exception e) {
        return "";
    }

    return "";
}

From source file:Main.java

public static Location getLastKnownLocation(Context context) {
    LocationManager manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    List<String> providers = manager.getProviders(true);
    for (String provider : providers) {
        Location location = manager.getLastKnownLocation(provider);
        if (location != null) {
            return location;
        }//from  w  ww.  j  a  va 2  s.c  o m
    }

    // At this point we've done all we can and no location is returned
    return null;
}

From source file:Main.java

public static String getGpsString(Context context) {
    if (context == null) {
        return null;
    }//w ww . j  a va2  s. c  o  m

    String gps = null;

    Location location = null;
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setAltitudeRequired(false);
    criteria.setBearingRequired(false);
    criteria.setCostAllowed(true);

    LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    String provider = locationManager.getBestProvider(criteria, true);
    if (provider != null) {
        location = locationManager.getLastKnownLocation(provider);
    }
    if (location != null) {
        double longtitude = location.getLongitude();
        double latitude = location.getLatitude();
        gps = "Lon:" + longtitude + "; Lat:" + latitude;
    }

    if (gps == null) {
        gps = "Lon:0; Lat:0";
    }
    return gps;
}