MyLocationManager.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package de.tudarmstadt.informatik.hostage.logging;
  2. import java.util.Timer;
  3. import java.util.TimerTask;
  4. import android.content.Context;
  5. import android.location.Location;
  6. import android.location.LocationListener;
  7. import android.location.LocationManager;
  8. import android.os.Bundle;
  9. import android.util.Log;
  10. import android.widget.Toast;
  11. public class MyLocationManager {
  12. private LocationManager locationManager;
  13. private static Location newestLocation;
  14. private Context context;
  15. public MyLocationManager(Context context){
  16. // Acquire a reference to the system Location Manager
  17. locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
  18. newestLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
  19. this.context = context;
  20. }
  21. // Define a listener that responds to location updates
  22. LocationListener locationListener = new LocationListener() {
  23. public void onLocationChanged(Location location) {
  24. Toast.makeText(context, location.getLatitude() + " " + location.getLongitude() + " " + location.getAccuracy(), Toast.LENGTH_SHORT).show();
  25. // Called when a new location is found by the network location provider.
  26. if(isBetterLocation(location, newestLocation)){
  27. newestLocation = location;
  28. }
  29. }
  30. public void onStatusChanged(String provider, int status, Bundle extras) {}
  31. public void onProviderEnabled(String provider) {}
  32. public void onProviderDisabled(String provider) {}
  33. };
  34. /** Determines whether one Location reading is better than the current Location fix
  35. * @param location The new Location that you want to evaluate
  36. * @param currentBestLocation The current Location fix, to which you want to compare the new one
  37. */
  38. private boolean isBetterLocation(Location location, Location currentBestLocation) {
  39. if (currentBestLocation == null) {
  40. // A new location is always better than no location
  41. return true;
  42. }
  43. // Check whether the new location fix is more or less accurate
  44. int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy());
  45. boolean isMoreAccurate = accuracyDelta < 0;
  46. // Determine location quality using a combination of timeliness and accuracy
  47. if (isMoreAccurate) {
  48. return true;
  49. }
  50. return false;
  51. }
  52. public void startUpdates(){
  53. boolean gpsEnabled = false;
  54. boolean networkEnabled = false;
  55. //exceptions will be thrown if provider is not permitted.
  56. try{
  57. gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
  58. }catch(Exception ex){}
  59. try{
  60. networkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
  61. }catch(Exception ex){}
  62. //don't start listeners if no provider is enabled
  63. if(!gpsEnabled && !networkEnabled)
  64. return;
  65. // Register the listener with the Location Manager to receive location updates
  66. if(gpsEnabled)
  67. locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
  68. if(networkEnabled)
  69. locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
  70. }
  71. public void getUpdates(long time){
  72. startUpdates();
  73. Timer timer1=new Timer();
  74. timer1.schedule(new StopTask() , time);
  75. }
  76. class StopTask extends TimerTask {
  77. public void run() {
  78. stopUpdates();
  79. }
  80. }
  81. public void stopUpdates(){
  82. locationManager.removeUpdates(locationListener);
  83. }
  84. public static Location getNewestLocation(){
  85. return newestLocation;
  86. }
  87. }