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:edu.neu.madcourse.kevinpacheco.butterflycatcher.MainActivity.java

private void setUpMap() {

    if (myMap == null) {
        SupportMapFragment myMapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        myMap = myMapFragment.getMap();/*ww w.ja  v a  2 s  .c  o  m*/
        myMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    }
    if (myMap != null) {
        //add some code here to initialize map

        myMap.setMyLocationEnabled(true);
        myMap.setOnMyLocationChangeListener(this);

        myMap.getUiSettings().setZoomControlsEnabled(false);
        myMap.getUiSettings().setZoomGesturesEnabled(false);

        myMap.getUiSettings().setMyLocationButtonEnabled(false);

        locationmanager = (LocationManager) getSystemService(LOCATION_SERVICE);
        String provider = locationmanager.getBestProvider(new Criteria(), true);

        Log.d(TAG, "provider = " + provider);

        if (provider == null || !locationmanager.isProviderEnabled(LocationManager.GPS_PROVIDER))
            onProviderDisabled(provider);

        if (provider != null) {
            Log.d(TAG, "locationmanager.getLastKnownLocation(provider) = "
                    + locationmanager.getLastKnownLocation(provider));
            LOCATION = locationmanager.getLastKnownLocation(provider);

            Runnable showWaitDialog = new Runnable() {
                @Override
                public void run() {
                    while (LOCATION == null) {
                        // Wait for first location fix (do nothing until LOCATION != null)
                    }
                    // After receiving first location fix dismiss the progress dialog
                    dialog.dismiss();
                }
            };
            // Create a dialog to let the user know that we're waiting for a location fix
            dialog = ProgressDialog.show(this, "Please wait.", "Retrieving location...", true);
            Thread t = new Thread(showWaitDialog);
            t.start();

            Log.d(TAG, "Map location = " + getLocation());

            if (getLocation() != null)
                animateCamera(getLocation(), birdseyeOn);

        }

    }

    myMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {

        @Override
        public void onMapClick(LatLng arg0) {
            Log.d(TAG, "You click on " + arg0);
        }
    });
}

From source file:org.akvo.flow.ui.fragment.MapFragment.java

/**
 * Center the map in the given record's coordinates. If no record is provided,
 * the user's location will be used./*from  w ww . j av  a  2  s  .co m*/
 * @param record
 */
private void centerMap(SurveyedLocale record) {
    if (mMap == null) {
        return; // Not ready yet
    }

    LatLng position = null;

    if (record != null && record.getLatitude() != null && record.getLongitude() != null) {
        // Center the map in the data point
        position = new LatLng(record.getLatitude(), record.getLongitude());
    } else {
        // When multiple points are shown, center the map in user's location
        LocationManager manager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        String provider = manager.getBestProvider(criteria, true);
        if (provider != null) {
            Location location = manager.getLastKnownLocation(provider);
            if (location != null) {
                position = new LatLng(location.getLatitude(), location.getLongitude());
            }
        }
    }

    if (position != null) {
        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(position, 10));
    }
}

From source file:ru.dublgis.androidlocation.PassiveLocationProvider.java

static public boolean requestSingleUpdate(PendingIntent intent) {
    try {/*w  w w  .j a  v a  2 s  .c  o m*/
        if (null != mContext && null != mLocationManager && isPermissionGranted(mContext)) {
            Criteria criteria = new Criteria();
            criteria.setPowerRequirement(Criteria.POWER_HIGH);
            mLocationManager.requestSingleUpdate(criteria, intent);
            return true;
        }
    } catch (Throwable e) {
        Log.e(TAG, "Failed to start location updates", e);
    }
    return false;
}

From source file:ly.apps.android.rest.client.example.activities.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textViewDescription = (TextView) findViewById(R.id.textview_description);
    textViewTemp = (TextView) findViewById(R.id.textview_temp);
    imageViewIcon = (ImageView) findViewById(R.id.imageview_icon);
    textViewWind = (TextView) findViewById(R.id.textview_wind_response);
    textViewHumidity = (TextView) findViewById(R.id.textview_humidity_response);
    textViewTempMax = (TextView) findViewById(R.id.textview_tempmax_response);
    textViewTempMin = (TextView) findViewById(R.id.textview_tempmin_response);
    textViewCity = (TextView) findViewById(R.id.textview_city);
    contentProgressBar = (LinearLayout) findViewById(R.id.content_progressbar);
    linearLayoutContent = (LinearLayout) findViewById(R.id.content);
    contentBottom = (LinearLayout) findViewById(R.id.bottom_content);
    descriptionTempContent = (LinearLayout) findViewById(R.id.description_temp_content);

    RestClient client = RestClientFactory.defaultClient(getApplicationContext());
    api = RestServiceFactory.getService(getString(R.string.base_url), OpenWeatherAPI.class, client);

    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    String provider = locationManager.getBestProvider(criteria, false);
    Location location = locationManager.getLastKnownLocation(provider);

    checkImmersiveMode();//from ww w .j  a  v  a2  s.  c  om

    if ((location != null) && checkConnection(getApplicationContext())) {
        setLocation(location);
    } else {
        failMessage();
    }

}

From source file:com.mobilevangelist.glass.helloworld.GetTheWeatherActivity.java

public static Location getLastLocation(Context context) {
    Location result = null;// ww w . j av a  2 s  .c  o m
    LocationManager locationManager;
    Criteria locationCriteria;
    List<String> providers;

    locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    locationCriteria = new Criteria();
    locationCriteria.setAccuracy(Criteria.NO_REQUIREMENT);
    providers = locationManager.getProviders(locationCriteria, true);

    // Note that providers = locatoinManager.getAllProviders(); is not used because the
    // list might contain disabled providers or providers that are not allowed to be called.

    //Note that getAccuracy can return 0, indicating that there is no known accuracy.

    for (String provider : providers) {
        Location location = locationManager.getLastKnownLocation(provider);
        if (result == null) {
            result = location;
        } else if (result.getAccuracy() == 0.0) {
            if (location.getAccuracy() != 0.0) {
                result = location;
                break;
            } else {
                if (result.getAccuracy() > location.getAccuracy()) {
                    result = location;
                }
            }
        }
    }

    return result;
}

From source file:com.ptts.fragments.BusLocation.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_bus_location);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    showProgressBar();//from   ww w .  j  av  a2  s .co  m

    txtBusId = (TextView) findViewById(R.id.bus_plate);
    txtBusTime = (TextView) findViewById(R.id.time_label);

    Intent in = getIntent();
    cd = new ConnectionDetector(getApplicationContext());

    bus_id = in.getStringExtra(FetchBusTask.getKeyBusid());
    String latitude = in.getStringExtra(FetchBusTask.getKeyLatitude());
    String longitude = in.getStringExtra(FetchBusTask.getKeyLongitude());

    bus_id = bus_id.substring(6).replace("/", "");

    txtBusId.setText("Bus " + bus_id);
    //txtBusTime.setText(latitude);
    if (!cd.isConnectingToInternet()) {
        Toast.makeText(getApplicationContext(), "Connect to Internet First", Toast.LENGTH_LONG).show();
        return;
    } else {
        //to get bus plate number
        ServerConnection connect = new ServerConnection();
        connect.execute(new String[] { bus_id });

    }

    // Getting LocationManager object from System Service LOCATION_SERVICE
    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

    // Getting Google Play availability status
    int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());

    if (status != ConnectionResult.SUCCESS) { // Google Play Services are not available

        int requestCode = 10;
        Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);
        dialog.show();

    } else { // Google Play Services are available

        // Getting reference to the SupportMapFragment
        SupportMapFragment fragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);

        // Getting Google Map
        mGoogleMap = fragment.getMap();

        // Enabling MyLocation in Google Map
        mGoogleMap.setMyLocationEnabled(true);

        // Creating a criteria object to retrieve provider
        Criteria criteria = new Criteria();

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

        if (provider != null) {

            // Getting Current Location From GPS
            Location location = locationManager.getLastKnownLocation(provider);

            if (location != null) {
                onLocationChanged(location);
            }

            locationManager.requestLocationUpdates(provider, 20000, 0, this);

        }

    } //end else

    mGoogleMap.addMarker(new MarkerOptions()
            .position(new LatLng(Double.parseDouble(latitude), Double.parseDouble(longitude))).title(bus_id)
            .snippet(latitude));

    //find the distance between the two
    LatLng origin = new LatLng(mLatitude, mLongitude);
    LatLng dest = new LatLng(Double.parseDouble(latitude), Double.parseDouble(longitude));

    // Getting URL to the Google Directions API
    String url = getDirectionsUrl(origin, dest);

    DownloadTask downloadTask = new DownloadTask();

    // Start downloading json data from Google Directions API
    downloadTask.execute(url);

}

From source file:com.xmobileapp.rockplayer.LastFmEventImporter.java

/*********************************
 * /*from  ww w .  j a  va  2  s  .c  o m*/
 * Constructor
 * @param context
 * 
 *********************************/
public LastFmEventImporter(Context context) {
    this.context = context;

    Log.i("LASTFMEVENT", "creating-------------------------");
    /*
     * Check for Internet Connection (Through whichever interface)
     */
    ConnectivityManager connManager = (ConnectivityManager) ((RockPlayer) context)
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = connManager.getActiveNetworkInfo();
    /******* EMULATOR HACK - false condition needs to be removed *****/
    //if (false && (netInfo == null || !netInfo.isConnected())){
    if ((netInfo == null || !netInfo.isConnected())) {
        Bundle data = new Bundle();
        data.putString("info", "No Internet Connection");
        Message msg = new Message();
        msg.setData(data);
        ((RockPlayer) context).analyseConcertInfoHandler.sendMessage(msg);
        return;
    }

    /*
     * Get location
     */
    MIN_UPDATE_INTVL = 5 * 24 * 60 * 60 * 1000; // 5 days
    SPREAD_INTVL = 21 * 24 * 60 * 60 * 1000; // 21 days;
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_COARSE);
    criteria.setPowerRequirement(Criteria.POWER_LOW);
    LocationManager locManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    if (locManager.getBestProvider(criteria, true) != null)
        myLocation = locManager.getLastKnownLocation(locManager.getBestProvider(criteria, true));
    else {
        myLocation = new Location("gps");
        myLocation.setLatitude(47.100301);
        myLocation.setLongitude(-119.982465);
    }

    /*
     * Get preferred distance
     */
    //      SharedPreferences prefs = ((Filex) context).getSharedPreferences(((Filex) context).PREFS_NAME, 0);
    RockOnPreferenceManager prefs = new RockOnPreferenceManager(((RockPlayer) context).FILEX_PREFERENCES_PATH);
    concertRadius = prefs.getLong("ConcertRadius", (long) (((RockPlayer) context).CONCERT_RADIUS_DEFAULT));

    //myLocation =  locManager.getLastKnownLocation(locManager.getBestProvider(Criteria.POWER_LOW, true));

    //      try {
    //         getArtistEvents();
    //      } catch (SAXException e) {
    //         e.printStackTrace();
    //      } catch (ParserConfigurationException e) {
    //         e.printStackTrace();
    //      }
}

From source file:com.metinkale.prayerapp.vakit.AddCity.java

@SuppressWarnings("MissingPermission")
public void checkLocation() {
    if (PermissionUtils.get(this).pLocation) {
        LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        Location loc = null;/*from   w  ww. j  ava2  s  . com*/
        List<String> providers = lm.getProviders(true);
        for (String provider : providers) {
            Location last = lm.getLastKnownLocation(provider);
            // one hour==1meter in accuracy
            if ((last != null) && ((loc == null)
                    || ((last.getAccuracy() - (last.getTime() / (1000 * 60 * 60))) < (loc.getAccuracy()
                            - (loc.getTime() / (1000 * 60 * 60)))))) {
                loc = last;
            }
        }

        if (loc != null)
            onLocationChanged(loc);

        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_MEDIUM);
        criteria.setAltitudeRequired(false);
        criteria.setBearingRequired(false);
        criteria.setCostAllowed(false);
        criteria.setSpeedRequired(false);
        String provider = lm.getBestProvider(criteria, true);
        if (provider != null) {
            lm.requestSingleUpdate(provider, this, null);
        }

    } else {
        PermissionUtils.get(this).needLocation(this);
    }
}

From source file:cubes.compass.service.WeatherActivity.java

private void getWeatherFromCurrentLocation() {
    // system's LocationManager
    LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

    // medium accuracy for weather, good for 100 - 500 meters
    Criteria locationCriteria = new Criteria();
    locationCriteria.setAccuracy(Criteria.ACCURACY_MEDIUM);

    String provider = locationManager.getBestProvider(locationCriteria, true);

    // single location update
    if (ActivityCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
            && ActivityCompat.checkSelfPermission(this,
                    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.
        return;//w  ww . j  a  v a 2 s  .c om
    }
    locationManager.requestSingleUpdate(provider, this, null);
}

From source file:org.akvo.flow.activity.GeoshapeActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.geoshape_activity);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    mFeatures = new ArrayList<>();
    mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

    mFeatureMenu = findViewById(R.id.feature_menu);
    mFeatureName = (TextView) findViewById(R.id.feature_name);
    mClearPointBtn = findViewById(R.id.clear_point_btn);
    mClearPointBtn.setOnClickListener(mFeatureMenuListener);
    findViewById(R.id.add_point_btn).setOnClickListener(mFeatureMenuListener);
    findViewById(R.id.clear_feature_btn).setOnClickListener(mFeatureMenuListener);
    findViewById(R.id.properties).setOnClickListener(mFeatureMenuListener);

    mAllowPoints = getIntent().getBooleanExtra(ConstantUtil.EXTRA_ALLOW_POINTS, true);
    mAllowLine = getIntent().getBooleanExtra(ConstantUtil.EXTRA_ALLOW_LINE, true);
    mAllowPolygon = getIntent().getBooleanExtra(ConstantUtil.EXTRA_ALLOW_POLYGON, true);
    mManualInput = getIntent().getBooleanExtra(ConstantUtil.EXTRA_MANUAL_INPUT, true);

    initMap();/*  w  w w  .  j  a v  a  2  s .com*/

    String geoJSON = getIntent().getStringExtra(ConstantUtil.GEOSHAPE_RESULT);
    if (!TextUtils.isEmpty(geoJSON)) {
        load(geoJSON);
    } else {
        // If user location is known, center map
        LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        String provider = manager.getBestProvider(criteria, true);
        if (provider != null) {
            Location location = manager.getLastKnownLocation(provider);
            if (location != null) {
                LatLng position = new LatLng(location.getLatitude(), location.getLongitude());
                mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(position, 10));
            }
        }
    }
}