Advertisement
rachmadi

LocationUpdaterService

Feb 10th, 2017
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.35 KB | None | 0 0
  1. public class LocationUpdaterService extends Service
  2. {
  3.     public static final int TWO_MINUTES = 120000; // 120 seconds
  4.     public static Boolean isRunning = false;
  5.  
  6.     public LocationManager mLocationManager;
  7.     public LocationUpdaterListener mLocationListener;
  8.     public Location previousBestLocation = null;
  9.  
  10.     @Nullable
  11.     @Override
  12.     public IBinder onBind(Intent intent) {
  13.         return null;
  14.     }
  15.  
  16.     @Override
  17.     public void onCreate() {
  18.         mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
  19.         mLocationListener = new LocationUpdaterListener();
  20.         super.onCreate();
  21.     }
  22.  
  23.     Handler mHandler = new Handler();
  24.     Runnable mHandlerTask = new Runnable(){
  25.         @Override
  26.         public void run() {
  27.             if (!isRunning) {
  28.                 startListening();
  29.             }
  30.             mHandler.postDelayed(mHandlerTask, TWO_MINUTES);
  31.         }
  32.     };
  33.  
  34.     @Override
  35.     public int onStartCommand(Intent intent, int flags, int startId) {
  36.         mHandlerTask.run();
  37.         return START_STICKY;
  38.     }
  39.  
  40.     @Override
  41.     public void onDestroy() {
  42.         stopListening();
  43.         mHandler.removeCallbacks(mHandlerTask);
  44.         super.onDestroy();
  45.     }
  46.  
  47.     private void startListening() {
  48.         if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
  49.                 || ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
  50.             if (mLocationManager.getAllProviders().contains(LocationManager.NETWORK_PROVIDER))
  51.                 mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, mLocationListener);
  52.  
  53.             if (mLocationManager.getAllProviders().contains(LocationManager.GPS_PROVIDER))
  54.                 mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mLocationListener);
  55.         }
  56.         isRunning = true;
  57.     }
  58.  
  59.     private void stopListening() {
  60.         if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
  61.                 || ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
  62.             mLocationManager.removeUpdates(mLocationListener);
  63.         }
  64.         isRunning = false;
  65.     }
  66.  
  67.     public class LocationUpdaterListener implements LocationListener
  68.     {
  69.         @Override
  70.         public void onLocationChanged(Location location) {
  71.             if (isBetterLocation(location, previousBestLocation)) {
  72.                 previousBestLocation = location;
  73.                 try {
  74.                     // Script to post location data to server..
  75.                 }
  76.                 catch (Exception e) {
  77.                     e.printStackTrace();
  78.                 }
  79.                 finally {
  80.                     stopListening();
  81.                 }
  82.             }
  83.         }
  84.  
  85.         @Override
  86.         public void onProviderDisabled(String provider) {
  87.             stopListening();
  88.         }
  89.  
  90.         @Override
  91.         public void onProviderEnabled(String provider) { }
  92.  
  93.         @Override
  94.         public void onStatusChanged(String provider, int status, Bundle extras) { }
  95.     }
  96.  
  97.     protected boolean isBetterLocation(Location location, Location currentBestLocation) {
  98.         if (currentBestLocation == null) {
  99.             // A new location is always better than no location
  100.             return true;
  101.         }
  102.  
  103.         // Check whether the new location fix is newer or older
  104.         long timeDelta = location.getTime() - currentBestLocation.getTime();
  105.         boolean isSignificantlyNewer = timeDelta > TWO_MINUTES;
  106.         boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES;
  107.         boolean isNewer = timeDelta > 0;
  108.  
  109.         // If it's been more than two minutes since the current location, use the new location
  110.         // because the user has likely moved
  111.         if (isSignificantlyNewer) {
  112.             return true;
  113.             // If the new location is more than two minutes older, it must be worse
  114.         } else if (isSignificantlyOlder) {
  115.             return false;
  116.         }
  117.  
  118.         // Check whether the new location fix is more or less accurate
  119.         int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy());
  120.         boolean isLessAccurate = accuracyDelta > 0;
  121.         boolean isMoreAccurate = accuracyDelta < 0;
  122.         boolean isSignificantlyLessAccurate = accuracyDelta > 200;
  123.  
  124.         // Check if the old and new location are from the same provider
  125.         boolean isFromSameProvider = isSameProvider(location.getProvider(), currentBestLocation.getProvider());
  126.  
  127.         // Determine location quality using a combination of timeliness and accuracy
  128.         if (isMoreAccurate) {
  129.             return true;
  130.         } else if (isNewer && !isLessAccurate) {
  131.             return true;
  132.         } else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) {
  133.             return true;
  134.         }
  135.         return false;
  136.     }
  137.  
  138.     /** Checks whether two providers are the same */
  139.     private boolean isSameProvider(String provider1, String provider2) {
  140.         if (provider1 == null) {
  141.             return provider2 == null;
  142.         }
  143.         return provider1.equals(provider2);
  144.     }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement