Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class LocationController{
- @SuppressLint("MissingPermission")
- public void startGPSUpdates(@NonNull final LocationUpdateListener locationUpdateListener) {
- // Acquire a reference to the system Location Manager
- if (mLocationManager == null) {
- mLocationManager = (LocationManager) locationUpdateListener.getContext().getSystemService(LOCATION_SERVICE);
- }
- stopGPSLocationUpdates();
- boolean gpsEnabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
- boolean networkEnabled = mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
- LJ.d(TAG, "Providers - GPS: " + gpsEnabled + " Network: " + networkEnabled);
- // Define a listener that responds to location updates
- mLocationListener = new LocationListener() {
- public void onLocationChanged(Location location) {
- LJ.d(TAG, "Got Location from " + location.getProvider());
- // Called when a new location is found by the network location provider.
- //if (location.getProvider().equals(LocationManager.GPS_PROVIDER)) {
- if (!isMockLocation(locationUpdateListener.getContext(), location)) {
- locationUpdateListener.onLocationUpdate(location);
- } else {
- locationUpdateListener.onMockLocationUpdate(location);
- }
- //}
- }
- public void onStatusChanged(String provider, int status, Bundle extras) {
- LJ.d(TAG, "onStatusChanged " + provider);
- }
- public void onProviderEnabled(String provider) {
- LJ.d(TAG, "onProviderEnabled " + provider);
- }
- public void onProviderDisabled(String provider) {
- LJ.d(TAG, "onProviderDisabled " + provider);
- }
- };
- // Register the listener with the Location Manager to receive location updates
- mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mLocationListener);
- mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, mLocationListener);
- }
- @SuppressWarnings("MissingPermission")
- public void startLocationUpdates(@NonNull final LocationUpdateListener locationUpdateListener, long locationRefreshTime, long locationFastestRefreshTime) {
- try {
- stopFusedLocationUpdates();
- if (mFusedLocationClient == null) {
- mFusedLocationClient = LocationServices.getFusedLocationProviderClient(locationUpdateListener.getContext());
- }
- mLocationCallback = new LocationCallback() {
- @SuppressWarnings("EmptyMethod")
- @Override
- public void onLocationAvailability(LocationAvailability locationAvailability) {
- super.onLocationAvailability(locationAvailability);
- LJ.d(TAG, "Location Availability: " + locationAvailability.toString());
- }
- @Override
- public void onLocationResult(@NonNull LocationResult locationResult) {
- super.onLocationResult(locationResult);
- Location location = locationResult.getLastLocation();
- if (!isMockLocation(locationUpdateListener.getContext(), location)) {
- locationUpdateListener.onLocationUpdate(location);
- } else {
- locationUpdateListener.onMockLocationUpdate(location);
- }
- }
- };
- LocationRequest locationRequest = new LocationRequest();
- locationRequest.setInterval(locationRefreshTime);
- locationRequest.setFastestInterval(locationFastestRefreshTime); //Will Drain a lot of battery
- locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); //This will drain battery too
- mFusedLocationClient.requestLocationUpdates(locationRequest, mLocationCallback, null);
- } catch (SecurityException ex) {
- ex.printStackTrace();
- }
- }
- }
Add Comment
Please, Sign In to add comment