Example usage for android.location LocationManager NETWORK_PROVIDER

List of usage examples for android.location LocationManager NETWORK_PROVIDER

Introduction

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

Prototype

String NETWORK_PROVIDER

To view the source code for android.location LocationManager NETWORK_PROVIDER.

Click Source Link

Document

Name of the network location 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. java  2  s  .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 boolean checkLocationService(Context context) {
    LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    boolean gps_enabled = false;
    boolean network_enabled = false;

    try {//from w w w . java2 s  .c om
        gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
    } catch (Exception ex) {
    }

    try {
        network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    } catch (Exception ex) {
    }

    return gps_enabled;
}

From source file:Main.java

/**
 * Tells whether location is enabled.//from   w  w  w.  j a  v  a  2 s.  c om
 *
 * @param context a reference to the context.
 * @return true if location is enabled, false otherwise.
 */
public static boolean isLocationEnabled(Context context) {
    LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    boolean isGpsEnabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
    return isGpsEnabled | lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
}

From source file:Main.java

/**
 * This method returns the last,best known current location for user
 * @param context/*from   w w  w  .ja v  a 2 s  .  c o 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 Location getLocation(Address addr) {
    Location location = new Location(LocationManager.NETWORK_PROVIDER);
    location.setLatitude(addr.getLatitude());
    location.setLongitude(addr.getLongitude());
    return location;
}

From source file:Main.java

public static boolean isNatworkEnable(Context context) {
    LocationManager locationMgr = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    return locationMgr.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
}

From source file:Main.java

/**
 * Returns whether location services are currently active in the specified context. Location
 * services are active if both GPS and network location services are enabled.
 * @param context the context from which to check
 * @return true if there location services are on, otherwise false
 *//*  ww w .  j  a  v  a 2  s .  co m*/
public static boolean isLocationServicesOn(Context context) {
    LocationManager lm = (LocationManager) context.getApplicationContext()
            .getSystemService(Context.LOCATION_SERVICE);
    boolean gpsEnabled = false;
    boolean networkEnabled = false;

    // check whether GPS and network providers are enabled
    try {
        gpsEnabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
    } catch (Exception e) {
    }

    try {
        networkEnabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    } catch (Exception e) {
    }

    // only show dialog if location services are not enabled
    return (gpsEnabled || networkEnabled);
}

From source file:Main.java

/** Returns the last known location latitude. Use this if you know that the location listener
 * doesn't need to turn on. This first tries GPS, then network, and returns 0.0 if GPS nor Network is enabled. */
public static final double getLastKnownLocationLatitude() {
    loadLocationManager();/*  w  w  w. j  a  va  2 s.  co  m*/
    double latitude = 0.0;
    // Load last known location coordinate, if available, so that user doesn't have to wait as long for a location.
    if (isGpsEnabled()) {
        Location gpsLocation = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if (gpsLocation != null) {
            latitude = gpsLocation.getLatitude();
        }
    } else if (isNetworkEnabled()) {
        Location networkLocation = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        if (networkLocation != null) {
            latitude = networkLocation.getLatitude();
        }
    }
    return latitude;
}

From source file:Main.java

/** Returns the last known location longitude. Use this if you know that the location listener
 * doesn't need to turn on. This first tries GPS, then network, and returns 0.0 if GPS nor Network is enabled. */
public static final double getLastKnownLocationLongitude() {
    loadLocationManager();//from w  w w  .j  a v a2 s.c o  m
    double longitude = 0.0;
    // Load last known location coordinate, if available, so that user doesn't have to wait as long for a location.
    if (isGpsEnabled()) {
        Location gpsLocation = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if (gpsLocation != null) {
            longitude = gpsLocation.getLongitude();
        }
    } else if (isNetworkEnabled()) {
        Location networkLocation = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        if (networkLocation != null) {
            longitude = networkLocation.getLongitude();
        }
    }
    return longitude;
}

From source file:Main.java

/**
 * Create a {@link Location} object from the speified latitude and longitude.
 * @param latitude the latitude for the location to create.
 * @param longitude the longitude for the location to create.
 * @return a {@link Location} object.//w w w .ja va 2  s.c o  m
 */
public static Location createLocation(double latitude, double longitude) {
    Location location = new Location(LocationManager.NETWORK_PROVIDER);
    location.setLatitude(latitude);
    location.setLongitude(longitude);
    location.setAccuracy(10f);
    location.setTime(System.currentTimeMillis());
    location.setAltitude(0d);
    location.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos());
    return location;
}