Example usage for android.location Criteria ACCURACY_COARSE

List of usage examples for android.location Criteria ACCURACY_COARSE

Introduction

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

Prototype

int ACCURACY_COARSE

To view the source code for android.location Criteria ACCURACY_COARSE.

Click Source Link

Document

A constant indicating an approximate accuracy requirement

Usage

From source file:com.google.android.apps.paco.ExperimentExecutorCustomRendering.java

private void getBestProvider(LocationManager lm) {
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_COARSE);
    criteria.setAccuracy(Criteria.ACCURACY_FINE);

    String bestProvider = lm.getBestProvider(criteria, true);
    if (bestProvider != null) {
        lm.requestLocationUpdates(bestProvider, 0, 0, this);
        location = lm.getLastKnownLocation(bestProvider);
        for (InputLayout input : locationInputs) {
            input.setLocation(location);
        }/* w  w w .ja  va  2  s. c  om*/

    } else {
        new AlertDialog.Builder(this).setMessage(R.string.need_location).setCancelable(true)
                .setPositiveButton(R.string.enable_button, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        launchGpsSettings();
                        dialog.dismiss();
                    }
                }).setNegativeButton(R.string.cancel_button, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                }).create().show();
    }
}

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

/**
 * ??.//w  w  w.  j  ava 2 s  .  c  o  m
 * @param accuracy .
 * @param interval ?.
 */
private void startGPS(final boolean accuracy, final int interval) {
    if (ActivityCompat.checkSelfPermission(getContext(),
            Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
            && ActivityCompat.checkSelfPermission(getContext(),
                    ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }

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

    mLocationManager.requestLocationUpdates(mLocationManager.getBestProvider(criteria, true), interval, 0, this,
            Looper.getMainLooper());
}

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

/**
 * Try to update current location. Non blocking call.
 * //w w w  .  ja  v  a 2 s.c o  m
 * @param context
 *            application context
 */
public static void refreshCoordinates(final Context context) {
    MadvertiseUtil.logMessage(null, Log.DEBUG, "Trying to refresh location");

    if (context == null) {
        MadvertiseUtil.logMessage(null, Log.DEBUG, "Context not set - quit location refresh");
        return;
    }

    // check if we need a regular update
    if ((sLocationUpdateTimestamp + MadvertiseUtil.SECONDS_TO_REFRESH_LOCATION * 1000) > System
            .currentTimeMillis()) {
        MadvertiseUtil.logMessage(null, Log.DEBUG, "It's not time yet for refreshing the location");
        return;
    }

    synchronized (context) {
        // recheck, if location was updated by another thread while we
        // paused
        if ((sLocationUpdateTimestamp + MadvertiseUtil.SECONDS_TO_REFRESH_LOCATION * 1000) > System
                .currentTimeMillis()) {
            MadvertiseUtil.logMessage(null, Log.DEBUG, "Another thread updated the loation already");
            return;
        }

        boolean permissionCoarseLocation = MadvertiseUtil
                .checkPermissionGranted(android.Manifest.permission.ACCESS_COARSE_LOCATION, context);
        boolean permissionFineLocation = MadvertiseUtil
                .checkPermissionGranted(android.Manifest.permission.ACCESS_FINE_LOCATION, context);

        // return (null) if we do not have any permissions
        if (!permissionCoarseLocation && !permissionFineLocation) {
            MadvertiseUtil.logMessage(null, Log.DEBUG, "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) {
            MadvertiseUtil.logMessage(null, Log.DEBUG, "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) {
            MadvertiseUtil.logMessage(null, Log.DEBUG, "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;
        sLocationUpdateTimestamp = System.currentTimeMillis();
        locationManager.requestLocationUpdates(provider, 0, 0, new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                MadvertiseUtil.logMessage(null, Log.DEBUG, "Refreshing location");
                sCurrentLocation = location;
                sLocationUpdateTimestamp = System.currentTimeMillis();
                // stop draining battery life
                finalizedLocationManager.removeUpdates(this);
            }

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

            @Override
            public void onProviderEnabled(String provider) {
            }

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

From source file:com.example.mapdemo.BasicMapDemoActivity.java

private void setGeoLocation() {
    latitude = (TextView) findViewById(R.id.lat);
    longitude = (TextView) findViewById(R.id.lon);
    altitude = (TextView) findViewById(R.id.alt);
    heading = (TextView) findViewById(R.id.hea);

    provText = (TextView) findViewById(R.id.prov);
    choice = (TextView) findViewById(R.id.choice);
    fineAcc = (CheckBox) findViewById(R.id.fineAccuracy);
    choose = (Button) findViewById(R.id.chooseRadio);
    // Get the location manager
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    // Define the criteria how to select the location provider
    criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_COARSE); //default

    // user defines the criteria
    choose.setOnClickListener(new View.OnClickListener() {
        @Override// w  ww  .  ja v a  2s. c om
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if (fineAcc.isChecked()) {
                criteria.setAccuracy(Criteria.ACCURACY_FINE);
                choice.setText("fine accuracy selected");
            } else {
                criteria.setAccuracy(Criteria.ACCURACY_COARSE);
                choice.setText("coarse accuracy selected");
            }
        }
    });
    criteria.setCostAllowed(false);
    // get the best provider depending on the criteria
    provider = locationManager.getBestProvider(criteria, false);
    // the last known location of this provider
    Location location = locationManager.getLastKnownLocation(provider);
    mylistener = new MyLocationListener();
    if (location != null) {
        mylistener.onLocationChanged(location);
    } else {
        // leads to the settings because there is no last known location
        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        startActivity(intent);
    }
    // location updates: at least 1 meter and 200 millsecs change
    locationManager.requestLocationUpdates(provider, 200, 1, mylistener);
}

From source file:com.alibaba.weex.extend.module.location.DefaultLocation.java

private WXLocationListener findLocation(String watchId, String sucCallback, String errorCallback,
        boolean enableHighAccuracy, boolean enableAddress) {
    //    WXLogUtils.d(TAG, "into--[findLocation] mWatchId:" + watchId + "\nsuccessCallback:" + sucCallback + "\nerrorCallback:" + errorCallback + "\nenableHighAccuracy:" + enableHighAccuracy + "\nmEnableAddress:" + enableAddress);

    if (mLocationManager == null) {
        mLocationManager = (LocationManager) mWXSDKInstance.getContext()
                .getSystemService(Context.LOCATION_SERVICE);
    }//from   w ww . j a va 2 s .c  om
    Criteria criteria = new Criteria();
    if (enableHighAccuracy) {
        criteria.setAccuracy(Criteria.ACCURACY_COARSE);
    }
    //String provider = locationManager.getBestProvider(criteria, false);
    if (ActivityCompat.checkSelfPermission(mWXSDKInstance.getContext(),
            Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
            && ActivityCompat.checkSelfPermission(mWXSDKInstance.getContext(),
                    Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
        WXLocationListener WXLocationListener = new WXLocationListener(mLocationManager, mWXSDKInstance,
                watchId, sucCallback, errorCallback, enableAddress);
        mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME, MIN_DISTANCE,
                WXLocationListener);
        mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME, MIN_DISTANCE,
                WXLocationListener);
        return WXLocationListener;
    } else {
        Map<String, Object> options = new HashMap<>();
        options.put(ERROR_CODE, ErrorCode.NO_PERMISSION_ERROR);
        options.put(ERROR_MSG, ErrorMsg.NO_PERMISSION_ERROR);
        WXSDKManager.getInstance().callback(mWXSDKInstance.getInstanceId(), errorCallback, options);
    }
    return null;
}

From source file:com.mobilyzer.util.PhoneUtils.java

/**
 * Lazily initializes the location manager.
 *
 * As a side effect, assigns locationManager and locationProviderName.
 *///from  w  w w  .  j ava 2s . c  o  m
private synchronized void initLocation() {
    if (locationManager == null) {
        LocationManager manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

        Criteria criteriaCoarse = new Criteria();
        /* "Coarse" accuracy means "no need to use GPS".
         * Typically a gShots phone would be located in a building,
         * and GPS may not be able to acquire a location.
         * We only care about the location to determine the country,
         * so we don't need a super accurate location, cell/wifi is good enough.
         */
        criteriaCoarse.setAccuracy(Criteria.ACCURACY_COARSE);
        criteriaCoarse.setPowerRequirement(Criteria.POWER_LOW);
        String providerName = manager.getBestProvider(criteriaCoarse, /*enabledOnly=*/true);

        List<String> providers = manager.getAllProviders();
        for (String providerNameIter : providers) {
            try {
                LocationProvider provider = manager.getProvider(providerNameIter);
            } catch (SecurityException se) {
                // Not allowed to use this provider
                Logger.w("Unable to use provider " + providerNameIter);
                continue;
            }
            Logger.i(providerNameIter + ": "
                    + (manager.isProviderEnabled(providerNameIter) ? "enabled" : "disabled"));
        }

        /* Make sure the provider updates its location.
         * Without this, we may get a very old location, even a
         * device powercycle may not update it.
         * {@see android.location.LocationManager.getLastKnownLocation}.
         */
        manager.requestLocationUpdates(providerName, /*minTime=*/0, /*minDistance=*/0,
                new LoggingLocationListener(), Looper.getMainLooper());
        locationManager = manager;
        locationProviderName = providerName;
    }
    assert locationManager != null;
    assert locationProviderName != null;
}

From source file:com.wishlist.Wishlist.java

public void fetchCurrentLocation() {
    new Thread() {
        public void run() {
            Looper.prepare();/*from  w w  w  .j a  v  a  2  s .  co m*/
            mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            MyLocationListener locationListener = new MyLocationListener();
            Criteria criteria = new Criteria();
            criteria.setAccuracy(Criteria.ACCURACY_COARSE);
            String provider = mLocationManager.getBestProvider(criteria, true);
            if (provider != null && mLocationManager.isProviderEnabled(provider)) {
                mLocationManager.requestLocationUpdates(provider, 1, 0, locationListener,
                        Looper.getMainLooper());
            } else {
                showToast("Please turn on handset's GPS");
            }
            Looper.loop();
        }
    }.start();
}

From source file:com.zainsoft.ramzantimetable.QiblaActivity.java

private void registerForGPS() {
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_COARSE);
    criteria.setPowerRequirement(Criteria.POWER_LOW);
    criteria.setAltitudeRequired(false);
    criteria.setBearingRequired(false);/*  w w w . j a v  a2  s.c o m*/
    criteria.setSpeedRequired(false);
    criteria.setCostAllowed(true);
    LocationManager locationManager = ((LocationManager) getSystemService(Context.LOCATION_SERVICE));
    String provider = locationManager.getBestProvider(criteria, true);

    if (provider != null) {
        locationManager.requestLocationUpdates(provider, MIN_LOCATION_TIME, MIN_LOCATION_DISTANCE,
                qiblaManager);
    }
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_LOCATION_TIME,
            MIN_LOCATION_DISTANCE, qiblaManager);
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_LOCATION_TIME,
            MIN_LOCATION_DISTANCE, qiblaManager);
    Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    if (location == null) {
        location = ((LocationManager) getSystemService(Context.LOCATION_SERVICE))
                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
    }
    if (location != null) {
        qiblaManager.onLocationChanged(location);
    }

}

From source file:com.ht.app.RestaurantsActivity.java

private Location getLastKnownLocation() {
    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_COARSE);
    String provider = lm.getBestProvider(criteria, true);
    if (provider == null) {
        return null;
    }//from w ww .  j a  va 2s. c  om
    return lm.getLastKnownLocation(provider);
}