GpsUtils contains code snippets for GPS.
//package backend.snippets;
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;
/**
* GpsUtils contains code snippets for GPS.
* @author Maurya
*
*/
class GpsUtils {
static Location loc; // CLEAN THIS THING!
private static LocationManager locationManager;
void getLocation(Activity activity) {
// Acquire a reference to the system Location Manager
locationManager = (LocationManager) activity.getSystemService(Context.LOCATION_SERVICE);
// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new LocationUpdateHandler());
}
static Location getLastKnownLocation() {
String locationProvider = LocationManager.GPS_PROVIDER;
// Or use LocationManager.GPS_PROVIDER
Location lastKnownLocation = locationManager.getLastKnownLocation(locationProvider);
return lastKnownLocation;
}
public class LocationUpdateHandler implements LocationListener {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
makeUseOfNewLocation(location);
}
private void makeUseOfNewLocation(Location location) {
loc = location;
// Remove the listener you previously added
locationManager.removeUpdates(this);
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
}
}
Related examples in the same category