Example usage for android.location Criteria Criteria

List of usage examples for android.location Criteria Criteria

Introduction

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

Prototype

public Criteria() 

Source Link

Document

Constructs a new Criteria object.

Usage

From source file:com.mibr.android.intelligentreminder.INeedToo.java

public LocationManager getLocationManager() {
    if (mLocationManager == null) {
        mLocationManager = (android.location.LocationManager) getSystemService(Context.LOCATION_SERVICE);
    }//from w w w  .ja v  a2 s.c  om
    if (_bestProvider == null) {
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        _bestProvider = mLocationManager.getBestProvider(criteria, false);
        mLocationManager.requestLocationUpdates(_bestProvider, 20000, 10, this);
    }
    return mLocationManager;
}

From source file:com.music.mybarr.activities.ExampleActivity.java

private Boolean amIThere() {
    try {//from   ww  w  . ja v  a2s .co  m
        // get user location
        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        criteria = new Criteria();

        // Get the name of the best provider
        provider = locationManager.getBestProvider(criteria, true);

        // Get Current Location
        myLocation = locationManager.getLastKnownLocation(provider);

        myLatLng = new LatLng(myLocation.getLatitude(), myLocation.getLongitude());
        Log.i("lat", String.valueOf(myLocation.getLatitude()));
        Log.i("lon", String.valueOf(myLocation.getLongitude()));

        //distance between me and bar location
        float d = myLocation.distanceTo(barLocation);
        Log.i("distance", Float.toString(d));
        if (d > 200) {
            return false;
        } else {
            return true;
        }
    } catch (Exception e) {
        return false;
    }
}

From source file:org.wso2.edgeanalyticsservice.LocationSystemService.java

/** Initialize the location system Service and set the minimal update distance and time  */
public void startLocationService(Context context) {
    mContext = context;//w w w.j  a v a  2s  . c o  m
    mLocationManager = (LocationManager) mContext.getSystemService(mContext.LOCATION_SERVICE);
    Criteria locationCritera = new Criteria();
    locationCritera.setAccuracy(Criteria.ACCURACY_COARSE);
    locationCritera.setAltitudeRequired(false);
    locationCritera.setBearingRequired(false);
    locationCritera.setCostAllowed(true);
    locationCritera.setPowerRequirement(Criteria.NO_REQUIREMENT);

    String providerName = mLocationManager.getBestProvider(locationCritera, true);

    if (providerName != null && mLocationManager.isProviderEnabled(providerName)) {
        if (ActivityCompat.checkSelfPermission(mContext,
                Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                && ActivityCompat.checkSelfPermission(mContext,
                        Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public
            // void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            Log.e("Location", "No permission");
            return;
        }
        mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MINIMUM_TIME_BETWEEN_UPDATES,
                (float) MINIMUM_DISTANCE_FOR_UPDATES, new MyLocationListner(), Looper.getMainLooper());
        mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MINIMUM_TIME_BETWEEN_UPDATES,
                (float) MINIMUM_DISTANCE_FOR_UPDATES, new MyLocationListner(), Looper.getMainLooper());
        mlocation = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        //        mTaskManager.sendLocationData(new double[]{mlocation.getLatitude(), mlocation.getLongitude()});
        mLocationListner = new MyLocationListner();
        synchronized (this) {
            started = true;
        }
    } else {
        // Provider not enabled, prompt user to enable it
        Toast.makeText(mContext, "Please turn on GPS", Toast.LENGTH_LONG).show();
        Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        mContext.startActivity(myIntent);
    }
}

From source file:mx.developerbus.foodbus.FoodBus_Main.java

public LatLng getLocation() {
    // Get the location manager
    LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    String bestProvider = locationManager.getBestProvider(criteria, false);
    Location location = locationManager.getLastKnownLocation(bestProvider);
    Double lat, lon;//from  w ww.jav a2  s. c o m
    try {
        lat = location.getLatitude();
        lon = location.getLongitude();
        return new LatLng(lat, lon);
    } catch (NullPointerException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:com.mobicage.rogerthat.FriendsLocationActivity.java

private void getMyLocation() {
    mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    if (mLocationManager != null) {
        String bestProvider = mLocationManager.getBestProvider(new Criteria(), true);
        if (bestProvider != null) {
            try {
                mLocationManager.requestLocationUpdates(bestProvider, 0, 0, mLocationListener);
            } catch (SecurityException e) {
                L.bug(e);/*from w  w  w .j  a v  a2 s . co m*/
            }
        }
    }
}

From source file:edu.mit.mobile.android.locast.casts.LocatableListWithMap.java

/**
 * Gets the last-known location and updates with that.
 *///from   w  ww  .  ja v  a 2s  .  c  om
private void updateLocation() {
    final LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
    final String provider = lm.getBestProvider(new Criteria(), true);
    if (provider == null) {
        Toast.makeText(this, getString(R.string.error_no_providers), Toast.LENGTH_LONG).show();
        finish();
        return;
    }

    final Location loc = lm.getLastKnownLocation(provider);
    if (loc != null) {
        updateLocation(loc);
    } else {
        Toast.makeText(this, R.string.notice_finding_your_location, Toast.LENGTH_LONG).show();
        setRefreshing(true);
    }
    mLastLocation = loc;
}

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

/**
 * Try to update current location. Non blocking call.
 * //from   w  w  w . ja  v a  2 s .c  o  m
 * @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:name.gumartinm.weather.information.activity.MapActivity.java

public void onClickGetLocation(final View v) {
    // TODO: Somehow I should show a progress dialog.
    // If Google Play Services is available
    if (this.mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
        // TODO: Hopefully there will be results even if location did not change...   
        final Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        criteria.setAltitudeRequired(false);
        criteria.setBearingAccuracy(Criteria.NO_REQUIREMENT);
        criteria.setBearingRequired(false);
        criteria.setCostAllowed(false);// ww  w  .  j a  v  a  2s  .  c  om
        criteria.setHorizontalAccuracy(Criteria.ACCURACY_HIGH);
        criteria.setPowerRequirement(Criteria.POWER_MEDIUM);
        criteria.setSpeedAccuracy(Criteria.NO_REQUIREMENT);
        criteria.setSpeedRequired(false);
        criteria.setVerticalAccuracy(Criteria.ACCURACY_HIGH);

        this.mLocationManager.requestSingleUpdate(criteria, this, null);
    } else {
        Toast.makeText(this, this.getString(R.string.weather_map_not_enabled_location), Toast.LENGTH_LONG)
                .show();
    }
    // Trying to use the synchronous calls. Problems: mGoogleApiClient read/store from different threads.
    // new GetLocationTask(this).execute();
}

From source file:se.toxbee.sleepfighter.activity.EditGPSFilterAreaActivity.java

/**
 * Moves the user to its current location.
 *//* w ww .  j a v a 2 s .  c o  m*/
private void moveToCurrentLocation() {
    GPSFilterLocationRetriever retriever = new GPSFilterLocationRetriever(new Criteria());
    Location loc = retriever.getLocation(this);

    if (loc == null) {
        // User turned off GPS, send it to device location settings.
        Intent i = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        this.startActivity(i);
    } else {
        // First move to the last known location..
        this.moveCameraToLocation(loc, false);

        // Check if location fix is out dated, if it is, request a new location, ONCE.
        long elapsedTime = System.currentTimeMillis() - loc.getTime();
        if (elapsedTime > MAX_LOCATION_FIX_AGE) {
            // Therefore, we request a single fix.
            retriever.requestSingleUpdate(this, new LocationAdapter() {
                @Override
                public void onLocationChanged(Location loc) {
                    moveCameraToLocation(loc, true);
                }
            });
        }
    }
}

From source file:org.deviceconnect.android.deviceplugin.host.profile.HostGeolocationProfile.java

/**
 * ?????.//from w  w w .j a va 2s.  c o  m
 * @param accuracy .
 * @param response ?.
 */
private void getGPS(final boolean accuracy, final Intent response) {
    if (ActivityCompat.checkSelfPermission(getContext(),
            Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
            && ActivityCompat.checkSelfPermission(getContext(),
                    ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        MessageUtils.setIllegalDeviceStateError(response, "ACCESS_FINE_LOCATION permission not granted.");
        sendResponse(response);
        return;
    }

    Criteria criteria = new Criteria();
    if (accuracy) {
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
    } else {
        criteria.setAccuracy(Criteria.ACCURACY_COARSE);
    }

    mLocationManager.requestSingleUpdate(mLocationManager.getBestProvider(criteria, true),
            new LocationListener() {
                @Override
                public void onLocationChanged(Location location) {
                    Bundle position = createPositionObject(location);

                    DConnectProfile.setResult(response, DConnectMessage.RESULT_OK);
                    response.putExtra(GeolocationProfile.PARAM_POSITION, position);
                    sendResponse(response);
                }

                @Override
                public void onStatusChanged(String provider, int status, Bundle extras) {
                    // NOP
                }

                @Override
                public void onProviderEnabled(String provider) {
                    // NOP
                }

                @Override
                public void onProviderDisabled(String provider) {
                    // NOP
                }
            }, Looper.getMainLooper());
}