Example usage for android.location LocationManager GPS_PROVIDER

List of usage examples for android.location LocationManager GPS_PROVIDER

Introduction

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

Prototype

String GPS_PROVIDER

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

Click Source Link

Document

Name of the GPS location provider.

Usage

From source file:com.google.android.apps.mytracks.services.TrackRecordingService.java

/**
 * Registers the location listener.//from  ww  w . ja  v a 2 s  . c o  m
 */
private void registerLocationListener() {
    unregisterLocationListener();

    if (myTracksLocationManager == null) {
        Log.e(TAG, "locationManager is null.");
        return;
    }
    try {
        long interval = locationListenerPolicy.getDesiredPollingInterval();
        myTracksLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, interval,
                locationListenerPolicy.getMinDistance(), locationListener);
        currentRecordingInterval = interval;
    } catch (RuntimeException e) {
        Log.e(TAG, "Could not register location listener.", e);
    }
}

From source file:com.RSMSA.policeApp.OffenceReportForm.java

/**
 * try to get the 'best' location selected from all providers
 *//*from w w w . java  2  s .  com*/
private Location getBestLocation() {
    Location gpslocation = getLocationByProvider(LocationManager.GPS_PROVIDER);
    Location networkLocation = getLocationByProvider(LocationManager.NETWORK_PROVIDER);
    // if we have only one location available, the choice is easy
    if (gpslocation == null) {
        Log.d(TAG, "No GPS Location available.");
        return networkLocation;
    }
    if (networkLocation == null) {
        Log.d(TAG, "No Network Location available");
        return gpslocation;
    }
    // a locationupdate is considered 'old' if its older than the configured
    // update interval. this means, we didn't get a
    // update from this provider since the last check
    long old = System.currentTimeMillis() - 1 * 60 * 60 * 1000;
    boolean gpsIsOld = (gpslocation.getTime() < old);
    boolean networkIsOld = (networkLocation.getTime() < old);
    // gps is current and available, gps is better than network
    if (!gpsIsOld) {
        Log.d(TAG, "Returning current GPS Location");
        return gpslocation;
    }
    // gps is old, we can't trust it. use network location
    if (!networkIsOld) {
        Log.d(TAG, "GPS is old, Network is current, returning network");
        return networkLocation;
    }
    // both are old return the newer of those two
    if (gpslocation.getTime() > networkLocation.getTime()) {
        Log.d(TAG, "Both are old, returning gps(newer)");
        return gpslocation;
    } else {
        Log.d(TAG, "Both are old, returning network(newer)");
        return networkLocation;
    }
}

From source file:org.planetmono.dcuploader.ActivityUploader.java

private void queryLocation(boolean enabled) {
    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    if (enabled) {
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationTracker);
        lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationTracker);
        ((Button) findViewById(R.id.upload_ok)).setEnabled(false);
        findViewById(R.id.upload_location_progress).setVisibility(View.VISIBLE);
    } else {/*from  www. j  a v a2 s.  c  o m*/
        lm.removeUpdates(locationTracker);
        ((Button) findViewById(R.id.upload_ok)).setEnabled(true);
        findViewById(R.id.upload_location_progress).setVisibility(View.INVISIBLE);
    }
}

From source file:com.vonglasow.michael.satstat.ui.MainActivity.java

/**
 * Registers for updates with selected location providers.
 *///from   www  .  j a  va  2 s .  c o m
protected void registerLocationProviders() {
    Set<String> providers = new HashSet<String>(
            mSharedPreferences.getStringSet(Const.KEY_PREF_LOC_PROV, new HashSet<String>(Arrays
                    .asList(new String[] { LocationManager.GPS_PROVIDER, LocationManager.NETWORK_PROVIDER }))));
    locationManager.removeUpdates(this);

    if (mapSectionFragment != null)
        mapSectionFragment.onLocationProvidersChanged(providers);

    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)
        requestLocationUpdates();
    else
        permsRequested[Const.PERM_REQUEST_LOCATION_UPDATES] = true;
}

From source file:com.vonglasow.michael.satstat.ui.MainActivity.java

/**
 * Requests location updates from the selected location providers.
 * /*from w  w  w  .j a  v a  2s.c  om*/
 * This method is intended to be called by {@link #registerLocationProviders(Context)} or by
 * {@link #onRequestPermissionsResult(int, String[], int[])}, depending on whether permissions need to be
 * requested.
 */
private void requestLocationUpdates() {
    Set<String> providers = new HashSet<String>(
            mSharedPreferences.getStringSet(Const.KEY_PREF_LOC_PROV, new HashSet<String>(Arrays
                    .asList(new String[] { LocationManager.GPS_PROVIDER, LocationManager.NETWORK_PROVIDER }))));
    List<String> allProviders = locationManager.getAllProviders();

    if (!isStopped) {
        for (String pr : providers) {
            if (allProviders.indexOf(pr) >= 0) {
                try {
                    locationManager.requestLocationUpdates(pr, 0, 0, this);
                    Log.d(TAG, "Registered with provider: " + pr);
                } catch (SecurityException e) {
                    Log.w(TAG, "Permission not granted for " + pr
                            + " location provider. Data display will not be available for this provider.");
                }
            } else {
                Log.w(TAG, "No " + pr
                        + " location provider found. Data display will not be available for this provider.");
            }
        }
    }

    try {
        // if GPS is not selected, request location updates but don't store location
        if ((!providers.contains(LocationManager.GPS_PROVIDER)) && (!isStopped)
                && (allProviders.indexOf(LocationManager.GPS_PROVIDER) >= 0))
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

        locationManager.addGpsStatusListener(this);
    } catch (SecurityException e) {
        Log.w(TAG, "Permission not granted for " + LocationManager.GPS_PROVIDER
                + " location provider. Data display will not be available for this provider.");
    }
}

From source file:de.bogutzky.psychophysiocollector.app.MainActivity.java

private void startStreamingInternalSensorData() {
    internalSensorManager = new InternalSensorManager(root, new int[] { 1000, 1000, 1000 }, this);
    internalSensorManager.startStreaming();

    locationListener = new GPSListener(getString(R.string.file_name_gps_position), this.directoryName, 25,
            this);
    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
            && ContextCompat.checkSelfPermission(this,
                    Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[] {
                android.Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION },
                PERMISSIONS_REQUEST);/*from  w  w w  .  j  av  a 2s.co m*/

    } else {
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
        locationManager.addGpsStatusListener(new GpsStatus.Listener() {
            @Override
            public void onGpsStatusChanged(int event) {
                switch (event) {
                case GpsStatus.GPS_EVENT_STARTED:
                    gpsStatusText = getString(R.string.info_connected);
                    break;
                case GpsStatus.GPS_EVENT_STOPPED:
                    gpsStatusText = getString(R.string.info_not_connected);
                    break;
                case GpsStatus.GPS_EVENT_FIRST_FIX:
                    gpsStatusText = getString(R.string.info_connected_fix_received);
                    break;
                }
            }
        });
    }
}

From source file:br.com.bioscada.apps.biotracks.services.TrackRecordingService.java

/**
 * Called when location changed./*from  w ww .j  a v a 2  s. co m*/
 *
 * @param location the location
 */
private void onLocationChangedAsync(Location location) {
    try {
        if (!isRecording() || isPaused()) {
            Log.w(TAG, "Ignore onLocationChangedAsync. Not recording or paused.");
            return;
        }

        Track track = myTracksProviderUtils.getTrack(recordingTrackId);
        if (track == null) {
            Log.w(TAG, "Ignore onLocationChangedAsync. No track.");
            return;
        }

        if (!LocationUtils.isValidLocation(location)) {
            Log.w(TAG, "Ignore onLocationChangedAsync. location is invalid.");
            return;
        }

        if (!location.hasAccuracy() || location.getAccuracy() >= recordingGpsAccuracy) {
            Log.d(TAG, "Ignore onLocationChangedAsync. Poor accuracy.");
            return;
        }

        // Fix for phones that do not set the time field
        if (location.getTime() == 0L) {
            location.setTime(System.currentTimeMillis());
        }

        Location lastValidTrackPoint = getLastValidTrackPointInCurrentSegment(track.getId());
        long idleTime = 0L;
        if (lastValidTrackPoint != null && location.getTime() > lastValidTrackPoint.getTime()) {
            idleTime = location.getTime() - lastValidTrackPoint.getTime();
        }
        locationListenerPolicy.updateIdleTime(idleTime);
        if (currentRecordingInterval != locationListenerPolicy.getDesiredPollingInterval()) {
            registerLocationListener();
        }

        Sensor.SensorDataSet sensorDataSet = getSensorDataSet();
        if (sensorDataSet != null) {
            location = new MyTracksLocation(location, sensorDataSet);
        }

        // Always insert the first segment location
        if (!currentSegmentHasLocation) {
            insertLocation(track, location, null);
            currentSegmentHasLocation = true;
            lastLocation = location;
            return;
        }

        if (!LocationUtils.isValidLocation(lastValidTrackPoint)) {
            /*
             * Should not happen. The current segment should have a location. Just
             * insert the current location.
             */
            insertLocation(track, location, null);
            lastLocation = location;
            return;
        }

        double distanceToLastTrackLocation = location.distanceTo(lastValidTrackPoint);
        if (distanceToLastTrackLocation > maxRecordingDistance) {
            insertLocation(track, lastLocation, lastValidTrackPoint);

            Location pause = new Location(LocationManager.GPS_PROVIDER);
            pause.setLongitude(0);
            pause.setLatitude(PAUSE_LATITUDE);
            pause.setTime(lastLocation.getTime());
            insertLocation(track, pause, null);

            insertLocation(track, location, null);
            isIdle = false;
        } else if (sensorDataSet != null || distanceToLastTrackLocation >= recordingDistanceInterval) {
            insertLocation(track, lastLocation, lastValidTrackPoint);
            insertLocation(track, location, null);
            isIdle = false;
        } else if (!isIdle && location.hasSpeed() && location.getSpeed() < MAX_NO_MOVEMENT_SPEED) {
            insertLocation(track, lastLocation, lastValidTrackPoint);
            insertLocation(track, location, null);
            isIdle = true;
        } else if (isIdle && location.hasSpeed() && location.getSpeed() >= MAX_NO_MOVEMENT_SPEED) {
            insertLocation(track, lastLocation, lastValidTrackPoint);
            insertLocation(track, location, null);
            isIdle = false;
        } else {
            Log.d(TAG, "Not recording location, idle");
        }
        lastLocation = location;
    } catch (Error e) {
        Log.e(TAG, "Error in onLocationChangedAsync", e);
        throw e;
    } catch (RuntimeException e) {
        Log.e(TAG, "RuntimeException in onLocationChangedAsync", e);
        throw e;
    }
}

From source file:com.RSMSA.policeApp.OffenceReportForm.java

/**
 * Start listening and recording locations
 *///from w w w .  j  ava 2  s . c  o m
public void startRecording() {
    gpsTimer.cancel();
    gpsTimer = new Timer();
    long checkInterval = 60 * 1000;
    long minDistance = 1000;
    // receive updates
    LocationManager locationManager = (LocationManager) getApplicationContext()
            .getSystemService(Context.LOCATION_SERVICE);
    for (String s : locationManager.getAllProviders()) {
        locationManager.requestLocationUpdates(s, checkInterval, minDistance, new LocationListener() {

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

            @Override
            public void onProviderEnabled(String provider) {
            }

            @Override
            public void onProviderDisabled(String provider) {
            }

            @Override
            public void onLocationChanged(Location location) {
                // if this is a gps location, we can use it
                if (location.getProvider().equals(LocationManager.GPS_PROVIDER)) {
                    doLocationUpdate(location, true);
                }
            }
        });
    }
    // start the gps receiver thread
    gpsTimer.scheduleAtFixedRate(new TimerTask() {

        @Override
        public void run() {
            Location location = getBestLocation();
            doLocationUpdate(location, false);
        }
    }, 0, checkInterval);
}

From source file:com.openatk.fieldnotebook.MainActivity.java

private void checkGPS() {
    final LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
        buildAlertMessageNoGps();/*  w w  w .j a  v a  2  s. c  o m*/
    }
}

From source file:edu.cens.loci.provider.LociDbUtils.java

/**
 * /*from w  ww  .j a  v  a 2  s  .c om*/
 * @param start
 * @param end
 * @param filter
 * @return
 */
public ArrayList<LociLocation> getTrack(long start, long end, int filter) {
    String[] columns = new String[] { Tracks._ID, Tracks.TIME, Tracks.LATITUDE, Tracks.LONGITUDE,
            Tracks.ALTITUDE, Tracks.SPEED, Tracks.BEARING, Tracks.ACCURACY };
    String selection = Tracks.TIME + ">=" + start + " AND " + Tracks.TIME + " <= " + end;

    final SQLiteDatabase db = mDbHelper.getWritableDatabase();
    Cursor cursor = db.query(Tables.TRACKS, columns, selection, null, null, null, null);

    ArrayList<LociLocation> track = new ArrayList<LociLocation>();

    if (cursor.moveToFirst()) {
        do {
            LociLocation loc = new LociLocation(LocationManager.GPS_PROVIDER);
            loc.setTime(cursor.getLong(cursor.getColumnIndex(Tracks.TIME)));
            loc.setLatitude(cursor.getDouble(cursor.getColumnIndex(Tracks.LATITUDE)));
            loc.setLongitude(cursor.getDouble(cursor.getColumnIndex(Tracks.LONGITUDE)));
            loc.setAltitude(cursor.getDouble(cursor.getColumnIndex(Tracks.ALTITUDE)));
            loc.setSpeed(cursor.getFloat(cursor.getColumnIndex(Tracks.SPEED)));
            loc.setBearing(cursor.getFloat(cursor.getColumnIndex(Tracks.BEARING)));
            loc.setAccuracy(cursor.getFloat(cursor.getColumnIndex(Tracks.ACCURACY)));

            track.add(loc);
        } while (cursor.moveToNext());
    }
    cursor.close();
    return track;

}