Guest User

Untitled

a guest
Apr 1st, 2017
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.80 KB | None | 0 0
  1. package com.hfad.drogomierz;
  2.  
  3. import android.app.Service;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.location.Location;
  7. import android.location.LocationListener;
  8. import android.location.LocationManager;
  9. import android.os.Binder;
  10. import android.os.Bundle;
  11. import android.os.IBinder;
  12.  
  13. public class OdometerService extends Service {
  14.  
  15.     private final IBinder binder = new OdometerBinder();
  16.     private static double distanceInMeters;
  17.     private static Location lastLocation = null;
  18.     private int toAverageCounter = 0;
  19.  
  20.     public class OdometerBinder extends Binder {
  21.         OdometerService getOdometer() {
  22.             return OdometerService.this;
  23.         }
  24.     }
  25.  
  26.     @Override
  27.     public IBinder onBind(Intent intent) {
  28.         return binder;
  29.     }
  30.  
  31.     @Override
  32.     public void onCreate() {
  33.         LocationListener listener = new LocationListener() {
  34.             @Override
  35.             public void onLocationChanged(Location location) {
  36.                 if (lastLocation == null) {
  37.                     lastLocation = location;
  38.                 }
  39.                 distanceInMeters += location.distanceTo(lastLocation);
  40.                 lastLocation = location;
  41.                                
  42.                
  43.             }
  44.  
  45.             @Override
  46.             public void onProviderDisabled(String arg0) {}
  47.  
  48.             @Override
  49.             public void onProviderEnabled(String arg0) {}
  50.  
  51.             @Override
  52.             public void onStatusChanged(String arg0, int arg1, Bundle bundle) {}
  53.         };
  54.         LocationManager locManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
  55.         locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, listener);
  56.     }
  57.  
  58.     public double getDistance() {
  59.         return this.distanceInMeters / 1000;
  60.     }
  61. }
Add Comment
Please, Sign In to add comment