Advertisement
Ankhwatcher

LocationService

Apr 6th, 2012
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.13 KB | None | 0 0
  1. package ie.appz.shortestwalkingroute.gps;
  2.  
  3. import ie.appz.shortestwalkingroute.sqlite.FixOpenHelper;
  4. import ie.appz.shortestwalkingroute.sqlite.FixProvider;
  5. import android.app.Service;
  6. import android.content.ContentResolver;
  7. import android.content.Context;
  8. import android.content.Intent;
  9. import android.location.Location;
  10. import android.location.LocationListener;
  11. import android.location.LocationManager;
  12. import android.net.Uri;
  13. import android.os.Bundle;
  14. import android.os.IBinder;
  15.  
  16. public class LocationService extends Service {
  17.  
  18.     int mRouteNo;
  19.     FixOpenHelper mFixOpenHelper;
  20.     Location lastLocation;
  21.     public static float oldAccuracy = 102;
  22.     LocationManager mLocationManager;
  23.     LocationListener networkListener, gpsListener;
  24.  
  25.     @Override
  26.     public void onCreate() {
  27.         super.onCreate();
  28.         mLocationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
  29.         networkListener = new LocationListener() {
  30.             public void onLocationChanged(Location location) {
  31.                 if (lastLocation == null || (location.getTime() - lastLocation.getTime()) > 3000) {
  32.                     addRow(location);
  33.                 }
  34.             }
  35.  
  36.             public void onStatusChanged(String provider, int status, Bundle extras) {
  37.             }
  38.  
  39.             public void onProviderEnabled(String provider) {
  40.             }
  41.  
  42.             public void onProviderDisabled(String provider) {
  43.             }
  44.         };
  45.         gpsListener = new LocationListener() {
  46.             public void onLocationChanged(Location location) {
  47.                 addRow(location);
  48.             }
  49.  
  50.             public void onStatusChanged(String provider, int status, Bundle extras) {
  51.             }
  52.  
  53.             public void onProviderEnabled(String provider) {
  54.             }
  55.  
  56.             public void onProviderDisabled(String provider) {
  57.             }
  58.         };
  59.         mFixOpenHelper = new FixOpenHelper(this);
  60.     }
  61.  
  62.     @Override
  63.     public void onStart(Intent intent, int startid) {
  64.         mRouteNo = intent.getIntExtra("HIGHESTROUTE", 1);
  65.         /*
  66.          * Register the listener with the Location Manager to receive location
  67.          * updates
  68.          */
  69.         mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 000, 0, networkListener);
  70.         mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 000, 0, gpsListener);
  71.         /*
  72.          * The two integers in this request are the time (ms) and distance (m)
  73.          * intervals of notifications respectively.
  74.          */
  75.     }
  76.  
  77.     @Override
  78.     public IBinder onBind(Intent intent) {
  79.         // TODO Auto-generated method stub
  80.         return null;
  81.     }
  82.  
  83.     protected void addRow(Location location) {
  84.         /*
  85.          * This will reject any GPS fix with very poor accuracy
  86.          */
  87.         if (location.getAccuracy() < 100 && location.getAccuracy() < 2 * oldAccuracy) {
  88.             mFixOpenHelper.addFix(mRouteNo, location);
  89.             lastLocation = location;
  90.             oldAccuracy = (oldAccuracy + location.getAccuracy()) / 2;
  91.             Uri baseUri = Uri.withAppendedPath(FixProvider.CONTENT_URI, FixProvider.ROUTE + "/" + mRouteNo);
  92.  
  93.             ContentResolver contentResolver = this.getContentResolver();
  94.             contentResolver.notifyChange(baseUri, null);
  95.  
  96.         }
  97.  
  98.     }
  99.  
  100.     @Override
  101.     public void onDestroy() {
  102.         super.onDestroy();
  103.         mLocationManager.removeUpdates(networkListener);
  104.         mLocationManager.removeUpdates(gpsListener);
  105.  
  106.         if (mFixOpenHelper != null) {
  107.             mFixOpenHelper.close();
  108.         }
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement