Loading...

Monday, 13 August 2012

class GPS (Android)

package test.gps;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;

class GpsMethods {

  private static LocationManager locManager; 
  static Location loc;

  void getLocation(Activity activity) {
      locManager = (LocationManager) activity.getSystemService(Context.LOCATION_SERVICE);
      locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new LocationUpdateHandler());
  }
 
  static Location getLastKnownLocation() {
    String locationProvider = LocationManager.GPS_PROVIDER;
    Location lastLocation = locationManager.getLastKnownLocation(locationProvider);
    return lastLocation;
  }
 
  public class LocationUpdateHandler implements LocationListener {
      public void onLocationChanged(Location loc) {
          makeUseOfNewLocation(loc);
        }
        private void makeUseOfNewLocation(Location loc) {
          location = loc;
          locationManager.removeUpdates(this);
        }

        public void onStatusChanged(String provider, int status, Bundle extras){
            // code
        }
        public void onProviderEnabled(String provider) {
            // code
        }
        public void onProviderDisabled(String provider) {
            // code
        }
   
  }

}

No comments:

Post a Comment