Java Code Examples for android.location.LocationManager#GPS_PROVIDER

The following examples show how to use android.location.LocationManager#GPS_PROVIDER . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NmeaParser.java    From contrib-drivers with Apache License 2.0 6 votes vote down vote up
private void postLocation(long timestamp, double latitude, double longitude, double altitude, float speed, float bearing) {
    if (mGpsModuleCallback != null) {
        Location location = new Location(LocationManager.GPS_PROVIDER);
        // We cannot compute accuracy from NMEA data alone.
        // Assume that a valid fix has the quoted accuracy of the module.
        // Framework requires accuracy in DRMS.
        location.setAccuracy(mGpsAccuracy * 1.2f);
        location.setTime(timestamp);

        location.setLatitude(latitude);
        location.setLongitude(longitude);
        if (altitude != -1) {
            location.setAltitude(altitude);
        }
        if (speed != -1) {
            location.setSpeed(speed);
        }
        if (bearing != -1) {
            location.setBearing(bearing);
        }

        mGpsModuleCallback.onGpsLocationUpdate(location);
    }
}
 
Example 2
Source File: MainActivity.java    From together-go with GNU Lesser General Public License v3.0 6 votes vote down vote up
private void setLocation(double longtitude, double latitude) {

        // 因为我是在北京的,我把这个海拔设置了一个符合北京的范围,随机生成
        altitude = genDouble(38.0, 50.5);
        // 而定位有精度,GPS精度一般小于15米,我设置在1到15米之间随机
        accuracy = (float)genDouble(1.0, 15.0);

        // 下面就是自己设置定位的位置了
        Location location = new Location(LocationManager.GPS_PROVIDER);
        location.setTime(System.currentTimeMillis());
        location.setLatitude(latitude);
        location.setLongitude(longtitude);
        // 北京海拔大概范围,随机生成
        location.setAltitude(altitude);
        // GPS定位精度范围,随机生成
        location.setAccuracy(accuracy);
        if (Build.VERSION.SDK_INT > 16) {
            location.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos());
        }
        try {
            locationManager.setTestProviderLocation(LocationManager.GPS_PROVIDER, location);
        } catch (SecurityException e) {
            simulateLocationPermission();
        }
    }
 
Example 3
Source File: LocationSubjectTest.java    From android-test with Apache License 2.0 6 votes vote down vote up
@Test
public void isAt_notAt() {
  Location location = new Location(LocationManager.GPS_PROVIDER);
  location.setLatitude(1);
  location.setLongitude(-1);

  Location other = new Location(LocationManager.GPS_PROVIDER);
  other.setLatitude(1);
  other.setLongitude(-2);

  try {
    assertThat(location).isAt(other);
    fail();
  } catch (AssertionError e) {
    assertThat(e).factValue("expected").isEqualTo("-2.0");
    assertThat(e).factValue("but was").isEqualTo("-1.0");
  }
}
 
Example 4
Source File: LocationHelper.java    From ESeal with Apache License 2.0 6 votes vote down vote up
public void requestLocation() {
    String provider = LocationManager.GPS_PROVIDER;

    Location lastKnownLocation = locationManager.getLastKnownLocation(provider);

    if (lastKnownLocation == null) {
        lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    }
    if (lastKnownLocation != null) {
        if (mCallBack != null) {
            mCallBack.onSuccess(lastKnownLocation);
        }
    } else {
        locationManager.requestLocationUpdates(provider, 3000, 0, mLocationListener);
    }
}
 
Example 5
Source File: LocationSubjectTest.java    From android-test with Apache License 2.0 6 votes vote down vote up
@Test
public void isFaraway_notFaraway() {
  Location location = new Location(LocationManager.GPS_PROVIDER);
  location.setLatitude(1);
  location.setLongitude(-1);

  Location other = new Location(LocationManager.GPS_PROVIDER);
  other.setLatitude(1.1);
  other.setLongitude(-1.1);

  try {
    assertThat(location).isFaraway(other, 100000);
    fail();
  } catch (AssertionError e) {
    assertThat(e).factValue("expected to be at least").isEqualTo("100000.0");
    assertThat(e).factValue("but was").isEqualTo("15689.056");
  }
}
 
Example 6
Source File: LocationService.java    From privatelocation with GNU General Public License v3.0 6 votes vote down vote up
private void randomize() {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    try {
        RANDOMIZE_LOCATION_INTERVAL = Integer.parseInt(prefs.getString("RANDOMIZE_LOCATION_INTERVAL", "60"));

        float randomLat = randomLat();
        float randomLng = randomLng();

        mockNetwork = new LocationProvider(LocationManager.NETWORK_PROVIDER, context);
        mockGps = new LocationProvider(LocationManager.GPS_PROVIDER, context);

        mockNetwork.pushLocation(randomLat, randomLng);
        mockGps.pushLocation(randomLat, randomLng);

        randomizeTimer();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 7
Source File: MainActivity.java    From HereGPSLocation with GNU General Public License v3.0 6 votes vote down vote up
private void clickCheckBoxRefresh() {
	refreshCounter = 0;
	textCounter.setText("0");
	if (checkBoxRefresh.isChecked()) {
		String provider;
		if (radio0.isChecked())
			provider = LocationManager.GPS_PROVIDER;
		else
			provider = LocationManager.NETWORK_PROVIDER;
		locationManager.requestLocationUpdates(provider, 1000, 0,
				locationListener);
	} else
		locationManager.removeUpdates(locationListener);
}
 
Example 8
Source File: WaytodayServicesTest.java    From itag with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void uplaodService_shouldUploadLocation() {
    ArgumentCaptor<UploadJobService.Status> argument =
            ArgumentCaptor.forClass(UploadJobService.Status.class);
    Location location = new Location(LocationManager.GPS_PROVIDER);
    UploadJobService.enqueueUploadLocation(
            ApplicationProvider.getApplicationContext(),
            location
    );
    verify(mockUploadListiner, timeout(5000).times(3)).onStatusChange(argument.capture());
    List<UploadJobService.Status> statuses = argument.getAllValues();
    assertThat(statuses.size(), equalTo(3));
    assertThat(statuses.get(0), equalTo(UploadJobService.Status.QUEUED));
    assertThat(statuses.get(1), equalTo(UploadJobService.Status.UPLOADING));
    assertThat(statuses.get(2), equalTo(UploadJobService.Status.EMPTY));
}
 
Example 9
Source File: GeoLineString.java    From android_maplib with GNU Lesser General Public License v3.0 6 votes vote down vote up
public double getLength() {
    double length = 0;

    if (mPoints.size() < 2)
        return length;

    Location location1 = new Location(LocationManager.GPS_PROVIDER);
    GeoPoint point = (GeoPoint) mPoints.get(0).copy();
    point.setCRS(CRS_WEB_MERCATOR);
    point.project(CRS_WGS84);
    location1.setLongitude(point.getX());
    location1.setLatitude(point.getY());

    for (int i = 1; i < mPoints.size(); i++) {
        Location location2 = new Location(LocationManager.GPS_PROVIDER);
        point = (GeoPoint) mPoints.get(i).copy();
        point.setCRS(CRS_WEB_MERCATOR);
        point.project(CRS_WGS84);
        location2.setLongitude(point.getX());
        location2.setLatitude(point.getY());
        length += location1.distanceTo(location2);
        location1 = location2;
    }

    return length;
}
 
Example 10
Source File: LocationUpdatesProvider.java    From PrivacyStreams with Apache License 2.0 6 votes vote down vote up
@Override
protected void provide() {
    Looper.prepare();
    locationManager = (LocationManager) this.getContext().getSystemService(Context.LOCATION_SERVICE);
    locationListener = new MyLocationListener();

    long minTime = this.interval;
    float minDistance = 0;
    String provider;
    if (Geolocation.LEVEL_EXACT.equals(level)) {
        provider = LocationManager.GPS_PROVIDER;
    }
    else {
        provider = LocationManager.NETWORK_PROVIDER;
    }
    locationManager.requestLocationUpdates(provider, minTime, minDistance, locationListener);
    Looper.loop();
}
 
Example 11
Source File: LocationSubjectTest.java    From android-test with Apache License 2.0 5 votes vote down vote up
@Test
public void isNearby() {
  Location location = new Location(LocationManager.GPS_PROVIDER);
  location.setLatitude(1);
  location.setLongitude(-1);

  Location other = new Location(LocationManager.GPS_PROVIDER);
  other.setLatitude(1.1);
  other.setLongitude(-1.1);

  assertThat(location).isNearby(other, 100000);
}
 
Example 12
Source File: MyTracksLocationManager.java    From mytracks with Apache License 2.0 5 votes vote down vote up
/**
 * Returns true if gps provider is enabled.
 */
public boolean isGpsProviderEnabled() {
  if (!isAllowed()) {
    return false;
  }
  String provider = LocationManager.GPS_PROVIDER;
  if (locationManager.getProvider(provider) == null) {
    return false;
  }
  return locationManager.isProviderEnabled(provider);
}
 
Example 13
Source File: WalkEditService.java    From android_maplibui with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void startWalkEdit() {
    SharedPreferences sharedPreferences = getSharedPreferences(getPackageName() + "_preferences", MODE_MULTI_PROCESS);

    String minTimeStr = sharedPreferences.getString(SettingsConstants.KEY_PREF_LOCATION_MIN_TIME, "2");
    String minDistanceStr = sharedPreferences.getString(SettingsConstants.KEY_PREF_LOCATION_MIN_DISTANCE, "10");
    long minTime = Long.parseLong(minTimeStr) * 1000;
    float minDistance = Float.parseFloat(minDistanceStr);

    if (!PermissionUtil.hasLocationPermissions(this))
        return;

    mLocationManager.addGpsStatusListener(this);

    String provider = LocationManager.GPS_PROVIDER;
    if (mLocationManager.getAllProviders().contains(provider)) {
        mLocationManager.requestLocationUpdates(provider, minTime, minDistance, this);
    }

    provider = LocationManager.NETWORK_PROVIDER;
    if (mLocationManager.getAllProviders().contains(provider)) {
        mLocationManager.requestLocationUpdates(provider, minTime, minDistance, this);
    }

    NotificationHelper.showLocationInfo(this);
    initTargetIntent(mTargetActivity);
    addNotification();
}
 
Example 14
Source File: LocationSubjectTest.java    From android-test with Apache License 2.0 5 votes vote down vote up
@Test
public void isEqualTo() {
  Location location = new Location(LocationManager.GPS_PROVIDER);
  location.setLatitude(1);
  location.setLongitude(-1);
  location.setTime(2);

  Location other = new Location(location);

  assertThat(location).isEqualTo(other);

  assertThat((Location) null).isEqualTo(null);
}
 
Example 15
Source File: LocationSubjectTest.java    From android-test with Apache License 2.0 5 votes vote down vote up
@Test
public void extras() {
  Bundle bundle = new Bundle();
  bundle.putInt("extra", 2);

  Location location = new Location(LocationManager.GPS_PROVIDER);
  location.setExtras(bundle);

  assertThat(location).extras().containsKey("extra");
  assertThat(location).extras().integer("extra").isEqualTo(2);
}
 
Example 16
Source File: CameraMetadataNative.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private static String translateLocationProviderToProcess(final String provider) {
    if (provider == null) {
        return null;
    }
    switch(provider) {
        case LocationManager.GPS_PROVIDER:
            return GPS_PROCESS;
        case LocationManager.NETWORK_PROVIDER:
            return CELLID_PROCESS;
        default:
            return null;
    }
}
 
Example 17
Source File: LocationManagerService.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private String pickBest(List<String> providers) {
    if (providers.contains(LocationManager.GPS_PROVIDER)) {
        return LocationManager.GPS_PROVIDER;
    } else if (providers.contains(LocationManager.NETWORK_PROVIDER)) {
        return LocationManager.NETWORK_PROVIDER;
    } else {
        return providers.get(0);
    }
}
 
Example 18
Source File: TrackerService.java    From android_maplibui with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    String targetActivity = "";

    if (intent != null) {
        targetActivity = intent.getStringExtra(ConstantsUI.TARGET_CLASS);
        String action = intent.getAction();

        if (action != null && !TextUtils.isEmpty(action)) {
            switch (action) {
                case ACTION_SYNC:
                    if (mIsRunning || mLocationSenderThread != null)
                        return START_STICKY;

                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                        int res = R.string.sync_started;
                        String title = getString(res);
                        NotificationCompat.Builder builder = createBuilder(this, res);
                        builder.setSmallIcon(mSmallIcon)
                                .setLargeIcon(mLargeIcon)
                                .setTicker(title)
                                .setWhen(System.currentTimeMillis())
                                .setAutoCancel(false)
                                .setContentTitle(title)
                                .setContentText(title)
                                .setOngoing(true);

                        startForeground(TRACK_NOTIFICATION_ID, builder.build());
                    }

                    mLocationSenderThread = createLocationSenderThread(500L);
                    mLocationSenderThread.start();
                    return START_NOT_STICKY;
                case ACTION_STOP:
                    removeNotification();
                    stopSelf();
                    return START_NOT_STICKY;
                case ACTION_SPLIT:
                    stopTrack();
                    startTrack();
                    addNotification();
                    return START_STICKY;
            }
        }
    }

    if (!mIsRunning) {
        if (!PermissionUtil.hasLocationPermissions(this)) {
            stopForeground(true);
            stopSelf();
            return START_NOT_STICKY;
        }

        mLocationManager.addGpsStatusListener(this);

        String time = SettingsConstants.KEY_PREF_TRACKS_MIN_TIME;
        String distance = SettingsConstants.KEY_PREF_TRACKS_MIN_DISTANCE;
        String minTimeStr = mSharedPreferences.getString(time, "2");
        String minDistanceStr = mSharedPreferences.getString(distance, "10");
        long minTime = Long.parseLong(minTimeStr) * 1000;
        float minDistance = Float.parseFloat(minDistanceStr);

        String provider = LocationManager.GPS_PROVIDER;
        if (mLocationManager.getAllProviders().contains(provider)) {
            mLocationManager.requestLocationUpdates(provider, minTime, minDistance, this);

            if (Constants.DEBUG_MODE)
                Log.d(Constants.TAG, "Tracker service request location updates for " + provider);
        }

        provider = LocationManager.NETWORK_PROVIDER;
        if (mLocationManager.getAllProviders().contains(provider)) {
            mLocationManager.requestLocationUpdates(provider, minTime, minDistance, this);

            if (Constants.DEBUG_MODE)
                Log.d(Constants.TAG, "Tracker service request location updates for " + provider);
        }

        NotificationHelper.showLocationInfo(this);

        // there are no tracks or last track correctly ended
        if (mSharedPreferencesTemp.getString(TRACK_URI, null) == null) {
            startTrack();
            mSharedPreferencesTemp.edit().putString(ConstantsUI.TARGET_CLASS, targetActivity).apply();
        } else {
            // looks like service was killed, restore data
            restoreData();
            targetActivity = mSharedPreferencesTemp.getString(ConstantsUI.TARGET_CLASS, "");
        }

        mLocationSenderThread = createLocationSenderThread(minTime);
        mLocationSenderThread.start();

        initTargetIntent(targetActivity);
        addNotification();
    }

    return START_STICKY;
}
 
Example 19
Source File: Sensors.java    From Multiwii-Remote with Apache License 2.0 4 votes vote down vote up
public void initMOCKLocation() {
	mocLocationProvider = LocationManager.GPS_PROVIDER;
	locationManager.addTestProvider(mocLocationProvider, false, false, false, false, true, true, true, 0, 5);
	locationManager.setTestProviderEnabled(mocLocationProvider, true);
	MockLocationWorking = true;
}
 
Example 20
Source File: ExploreFragment.java    From rox-android with Apache License 2.0 4 votes vote down vote up
private Location getMapCenterLocation() {
    Location location = new Location(LocationManager.GPS_PROVIDER);
    location.setLatitude(googleMap.getCameraPosition().target.latitude);
    location.setLongitude(googleMap.getCameraPosition().target.longitude);
    return location;
}