Example usage for android.location LocationManager getBestProvider

List of usage examples for android.location LocationManager getBestProvider

Introduction

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

Prototype

public String getBestProvider(Criteria criteria, boolean enabledOnly) 

Source Link

Document

Returns the name of the provider that best meets the given criteria.

Usage

From source file:Main.java

public static double[] getLastKnownLocation(Activity a) {
    LocationManager locationManager = (LocationManager) a.getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    String provider = locationManager.getBestProvider(criteria, true);
    System.out.println("provider: " + provider);
    Location location = locationManager.getLastKnownLocation(provider);
    return new double[] { location.getLatitude(), location.getLongitude() };
}

From source file:Main.java

public static String getGpsString(Context context) {
    if (context == null) {
        return null;
    }/*from ww w . j  a  v a2  s .  co 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;
}

From source file:Main.java

public static Location getLocation(Context context) {
    LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    // Gets the last known location from the best service.
    String bestLocationProvider = locationManager.getBestProvider(new Criteria(), true /*enabled only*/);
    return locationManager.getLastKnownLocation(bestLocationProvider);
}

From source file:Main.java

public static String getBestProvider(LocationManager locationManager) throws IllegalArgumentException {
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setPowerRequirement(Criteria.NO_REQUIREMENT);

    return locationManager.getBestProvider(criteria, true);
}

From source file:Main.java

public static String getBestProvider(LocationManager locationmanager) {
    String s = null;//from w ww . j  av  a2s . c o m
    if (locationmanager != null) {
        Criteria criteria = new Criteria();
        criteria.setAccuracy(2);
        criteria.setAltitudeRequired(false);
        criteria.setBearingRequired(false);
        criteria.setCostAllowed(true);
        criteria.setPowerRequirement(0);
        s = locationmanager.getBestProvider(criteria, true);
        if (s == null)
            s = "network";
    }
    return s;
}

From source file:com.airbop.library.simple.CommonUtilities.java

/** 
 * Get the current location from the location manager, and when we get it
 * post that information to the Airbop servers
 * @param appContext/*from w  ww .  ja v  a2s.c  o m*/
 * @param regId
 * @return
 */
public static boolean getCurrentLocation(LocationListener locationListener, final Context appContext) {
    if (true) {
        Criteria criteria = getCriteria();
        LocationManager locationManager = (LocationManager) appContext
                .getSystemService(Context.LOCATION_SERVICE);
        if (locationManager != null) {
            String provider = locationManager.getBestProvider(criteria, true);
            if (provider != null) {
                locationManager.requestLocationUpdates(provider, 2000, 10, locationListener);
                // We've posted so let the caller know
                return true;
            }
        }
    }
    // We couldn't get the location manager so let the caller know
    return false;
}

From source file:com.airbop.library.simple.CommonUtilities.java

/**
 * Get the last location from the LocationManager, if it's available, if not
 * return null./* w w  w  .  ja va 2s  . c o  m*/
 * @param appContext
 * @return
 */
public static Location getLastLocation(final Context appContext) {
    Location location = null;
    if (true) {
        Criteria criteria = getCriteria();
        LocationManager locationManager = (LocationManager) appContext
                .getSystemService(Context.LOCATION_SERVICE);
        if (locationManager != null) {
            String provider = locationManager.getBestProvider(criteria, true);
            if (provider != null) {
                location = locationManager.getLastKnownLocation(provider);

                if (location != null) {
                    displayMessage(appContext, String.format(AirBopStrings.airbop_got_last_location,
                            location.getLatitude(), location.getLongitude()));
                }
            }
        }
    }
    return location;
}

From source file:com.appdynamics.demo.gasp.utils.LocationServices.java

/**
 * Set Location Services and get current location
 *//*from  w ww  . j av  a 2  s. c  o  m*/
public static Location getLocation(Context context) {
    Location location = null;
    try {
        String svcName = Context.LOCATION_SERVICE;
        LocationManager locationManager = (LocationManager) context.getSystemService(svcName);

        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        criteria.setPowerRequirement(Criteria.POWER_LOW);
        criteria.setAltitudeRequired(false);
        criteria.setBearingRequired(false);
        criteria.setSpeedRequired(false);
        criteria.setCostAllowed(true);
        String provider = locationManager.getBestProvider(criteria, true);
        location = locationManager.getLastKnownLocation(provider);

        Log.i(TAG, "Current Latitude = " + location.getLatitude());
        Log.i(TAG, "Current Longitude = " + location.getLongitude());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return location;
}

From source file:de.madvertise.android.sdk.MadUtil.java

/**
 * Try to update current location. Non blocking call.
 * /*from w w  w . j  a  v  a 2 s.  c  om*/
 * @param context
 *            application context
 */
protected static void refreshCoordinates(Context context) {
    if (PRINT_LOG)
        Log.d(LOG, "Trying to refresh location");

    if (context == null) {
        if (PRINT_LOG)
            Log.d(LOG, "Context not set - quit location refresh");
        return;
    }

    // check if we need a regular update
    if ((locationUpdateTimestamp + MadUtil.SECONDS_TO_REFRESH_LOCATION * 1000) > System.currentTimeMillis()) {
        if (PRINT_LOG)
            Log.d(LOG, "It's not time yet for refreshing the location");
        return;
    }

    synchronized (context) {
        // recheck, if location was updated by another thread while we paused
        if ((locationUpdateTimestamp + MadUtil.SECONDS_TO_REFRESH_LOCATION * 1000) > System
                .currentTimeMillis()) {
            if (PRINT_LOG)
                Log.d(LOG, "Another thread updated the loation already");
            return;
        }

        boolean permissionCoarseLocation = context.checkCallingOrSelfPermission(
                android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED;
        boolean permissionFineLocation = context.checkCallingOrSelfPermission(
                android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED;

        // return (null) if we do not have any permissions
        if (!permissionCoarseLocation && !permissionFineLocation) {
            if (PRINT_LOG)
                Log.d(LOG, "No permissions for requesting the location");
            return;
        }

        // return (null) if we can't get a location manager
        LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        if (locationManager == null) {
            if (PRINT_LOG)
                Log.d(LOG, "Unable to fetch a location manger");
            return;
        }

        String provider = null;
        Criteria criteria = new Criteria();
        criteria.setCostAllowed(false);

        // try to get coarse location first
        if (permissionCoarseLocation) {
            criteria.setAccuracy(Criteria.ACCURACY_COARSE);
            provider = locationManager.getBestProvider(criteria, true);
        }

        // try to get gps location if coarse locatio did not work
        if (provider == null && permissionFineLocation) {
            criteria.setAccuracy(Criteria.ACCURACY_FINE);
            provider = locationManager.getBestProvider(criteria, true);
        }

        // still no provider, return (null)
        if (provider == null) {
            if (PRINT_LOG)
                Log.d(LOG, "Unable to fetch a location provider");
            return;
        }

        // create a finalized reference to the location manager, in order to
        // access it in the inner class
        final LocationManager finalizedLocationManager = locationManager;
        locationUpdateTimestamp = System.currentTimeMillis();
        locationManager.requestLocationUpdates(provider, 0, 0, new LocationListener() {
            public void onLocationChanged(Location location) {
                if (PRINT_LOG)
                    Log.d(LOG, "Refreshing location");
                currentLocation = location;
                locationUpdateTimestamp = System.currentTimeMillis();
                // stop draining battery life
                finalizedLocationManager.removeUpdates(this);
            }

            // not used yet
            public void onProviderDisabled(String provider) {
            }

            public void onProviderEnabled(String provider) {
            }

            public void onStatusChanged(String provider, int status, Bundle extras) {
            }
        }, context.getMainLooper());
    }
}

From source file:com.dwdesign.tweetings.activity.NearbyMapViewerActivity.java

protected void getLocationAndCenterMap() {
    LocationManager mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    final Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    final String provider = mLocationManager.getBestProvider(criteria, true);

    Location mRecentLocation = null;

    if (provider != null) {
        mRecentLocation = mLocationManager.getLastKnownLocation(provider);
    } else {//from ww  w. j av  a 2s . co m
        Toast.makeText(this, R.string.cannot_get_location, Toast.LENGTH_SHORT).show();
    }

    if (mRecentLocation != null && isNativeMapSupported()) {
        NativeNearbyMapFragment aFragment = (NativeNearbyMapFragment) mFragment;
        aFragment.setCenter(mRecentLocation);
    }
}