package de.tudarmstadt.informatik.hostage.logging; import java.util.Timer; import java.util.TimerTask; import android.content.Context; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.util.Log; import android.widget.Toast; public class MyLocationManager { private LocationManager locationManager; private static Location newestLocation; private Context context; public MyLocationManager(Context context){ // Acquire a reference to the system Location Manager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); newestLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); this.context = context; } // Define a listener that responds to location updates LocationListener locationListener = new LocationListener() { public void onLocationChanged(Location location) { Toast.makeText(context, location.getLatitude() + " " + location.getLongitude() + " " + location.getAccuracy(), Toast.LENGTH_SHORT).show(); // Called when a new location is found by the network location provider. if(isBetterLocation(location, newestLocation)){ newestLocation = location; } } public void onStatusChanged(String provider, int status, Bundle extras) {} public void onProviderEnabled(String provider) {} public void onProviderDisabled(String provider) {} }; /** Determines whether one Location reading is better than the current Location fix * @param location The new Location that you want to evaluate * @param currentBestLocation The current Location fix, to which you want to compare the new one */ private boolean isBetterLocation(Location location, Location currentBestLocation) { if (currentBestLocation == null) { // A new location is always better than no location return true; } // Check whether the new location fix is more or less accurate int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy()); boolean isMoreAccurate = accuracyDelta < 0; // Determine location quality using a combination of timeliness and accuracy if (isMoreAccurate) { return true; } return false; } public void startUpdates(){ boolean gpsEnabled = false; boolean networkEnabled = false; //exceptions will be thrown if provider is not permitted. try{ gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); }catch(Exception ex){} try{ networkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); }catch(Exception ex){} //don't start listeners if no provider is enabled if(!gpsEnabled && !networkEnabled) return; // Register the listener with the Location Manager to receive location updates if(gpsEnabled) locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); if(networkEnabled) locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); } public void getUpdates(long time){ startUpdates(); Timer timer1=new Timer(); timer1.schedule(new StopTask() , time); } class StopTask extends TimerTask { public void run() { stopUpdates(); } } public void stopUpdates(){ locationManager.removeUpdates(locationListener); } public static Location getNewestLocation(){ return newestLocation; } }