Creator

Location Update Android

Dec 11th, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.16 KB | None | 0 0
  1. class LocationController{
  2.     @SuppressLint("MissingPermission")
  3.     public void startGPSUpdates(@NonNull final LocationUpdateListener locationUpdateListener) {
  4.         // Acquire a reference to the system Location Manager
  5.         if (mLocationManager == null) {
  6.             mLocationManager = (LocationManager) locationUpdateListener.getContext().getSystemService(LOCATION_SERVICE);
  7.         }
  8.  
  9.         stopGPSLocationUpdates();
  10.  
  11.         boolean gpsEnabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
  12.         boolean networkEnabled = mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
  13.  
  14.         LJ.d(TAG, "Providers - GPS: " + gpsEnabled + " Network: " + networkEnabled);
  15.  
  16.  
  17.         // Define a listener that responds to location updates
  18.         mLocationListener = new LocationListener() {
  19.             public void onLocationChanged(Location location) {
  20.                 LJ.d(TAG, "Got Location from " + location.getProvider());
  21.                 // Called when a new location is found by the network location provider.
  22.                 //if (location.getProvider().equals(LocationManager.GPS_PROVIDER)) {
  23.                 if (!isMockLocation(locationUpdateListener.getContext(), location)) {
  24.                     locationUpdateListener.onLocationUpdate(location);
  25.                 } else {
  26.                     locationUpdateListener.onMockLocationUpdate(location);
  27.                 }
  28.                 //}
  29.             }
  30.  
  31.             public void onStatusChanged(String provider, int status, Bundle extras) {
  32.                 LJ.d(TAG, "onStatusChanged " + provider);
  33.             }
  34.  
  35.             public void onProviderEnabled(String provider) {
  36.                 LJ.d(TAG, "onProviderEnabled " + provider);
  37.             }
  38.  
  39.             public void onProviderDisabled(String provider) {
  40.                 LJ.d(TAG, "onProviderDisabled " + provider);
  41.             }
  42.         };
  43.  
  44.         // Register the listener with the Location Manager to receive location updates
  45.         mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mLocationListener);
  46.         mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, mLocationListener);
  47.     }
  48.  
  49.     @SuppressWarnings("MissingPermission")
  50.     public void startLocationUpdates(@NonNull final LocationUpdateListener locationUpdateListener, long locationRefreshTime, long locationFastestRefreshTime) {
  51.         try {
  52.             stopFusedLocationUpdates();
  53.             if (mFusedLocationClient == null) {
  54.                 mFusedLocationClient = LocationServices.getFusedLocationProviderClient(locationUpdateListener.getContext());
  55.             }
  56.             mLocationCallback = new LocationCallback() {
  57.                 @SuppressWarnings("EmptyMethod")
  58.                 @Override
  59.                 public void onLocationAvailability(LocationAvailability locationAvailability) {
  60.                     super.onLocationAvailability(locationAvailability);
  61.                     LJ.d(TAG, "Location Availability: " + locationAvailability.toString());
  62.                 }
  63.  
  64.                 @Override
  65.                 public void onLocationResult(@NonNull LocationResult locationResult) {
  66.                     super.onLocationResult(locationResult);
  67.                     Location location = locationResult.getLastLocation();
  68.                     if (!isMockLocation(locationUpdateListener.getContext(), location)) {
  69.                         locationUpdateListener.onLocationUpdate(location);
  70.                     } else {
  71.                         locationUpdateListener.onMockLocationUpdate(location);
  72.                     }
  73.                 }
  74.             };
  75.  
  76.             LocationRequest locationRequest = new LocationRequest();
  77.             locationRequest.setInterval(locationRefreshTime);
  78.             locationRequest.setFastestInterval(locationFastestRefreshTime); //Will Drain a lot of battery
  79.             locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); //This will drain battery too
  80.             mFusedLocationClient.requestLocationUpdates(locationRequest, mLocationCallback, null);
  81.         } catch (SecurityException ex) {
  82.             ex.printStackTrace();
  83.         }
  84.     }
  85. }
Add Comment
Please, Sign In to add comment