Advertisement
Guest User

Untitled

a guest
Dec 27th, 2010
462
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. public class MyLocation {
  2. Timer timer1;
  3. LocationManager lm;
  4. LocationResult locationResult;
  5. boolean gps_enabled = false;
  6. boolean network_enabled = false;
  7.  
  8. public boolean getLocation(Context context, LocationResult result) {
  9. // I use LocationResult callback class to pass location value from
  10. // MyLocation to user code.
  11. locationResult = result;
  12. if (lm == null)
  13. lm = (LocationManager) context
  14. .getSystemService(Context.LOCATION_SERVICE);
  15.  
  16. // exceptions will be thrown if provider is not permitted.
  17. try {
  18. gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
  19. } catch (Exception ex) {
  20. }
  21. try {
  22. network_enabled = lm
  23. .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
  24. } catch (Exception ex) {
  25. }
  26.  
  27. // don't start listeners if no provider is enabled
  28. if (!gps_enabled && !network_enabled)
  29. return false;
  30.  
  31. if (gps_enabled)
  32. lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
  33. locationListenerGps);
  34. if (network_enabled)
  35. lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
  36. locationListenerNetwork);
  37. timer1 = new Timer();
  38. timer1.schedule(new GetLastLocation(), 20000);
  39. return true;
  40. }
  41.  
  42. LocationListener locationListenerGps = new LocationListener() {
  43. public void onLocationChanged(Location location) {
  44. timer1.cancel();
  45. locationResult.gotLocation(location);
  46. lm.removeUpdates(this);
  47. lm.removeUpdates(locationListenerNetwork);
  48. }
  49.  
  50. public void onProviderDisabled(String provider) {
  51. }
  52.  
  53. public void onProviderEnabled(String provider) {
  54. }
  55.  
  56. public void onStatusChanged(String provider, int status, Bundle extras) {
  57. }
  58. };
  59.  
  60. LocationListener locationListenerNetwork = new LocationListener() {
  61. public void onLocationChanged(Location location) {
  62. timer1.cancel();
  63. locationResult.gotLocation(location);
  64. lm.removeUpdates(this);
  65. lm.removeUpdates(locationListenerGps);
  66. }
  67.  
  68. public void onProviderDisabled(String provider) {
  69. }
  70.  
  71. public void onProviderEnabled(String provider) {
  72. }
  73.  
  74. public void onStatusChanged(String provider, int status, Bundle extras) {
  75. }
  76. };
  77.  
  78. class GetLastLocation extends TimerTask {
  79. @Override
  80. public void run() {
  81. lm.removeUpdates(locationListenerGps);
  82. lm.removeUpdates(locationListenerNetwork);
  83.  
  84. Location net_loc = null, gps_loc = null;
  85. if (gps_enabled)
  86. gps_loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
  87. if (network_enabled)
  88. net_loc = lm
  89. .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
  90.  
  91. // if there are both values use the latest one
  92. if (gps_loc != null && net_loc != null) {
  93. if (gps_loc.getTime() > net_loc.getTime())
  94. locationResult.gotLocation(gps_loc);
  95. else
  96. locationResult.gotLocation(net_loc);
  97. return;
  98. }
  99.  
  100. if (gps_loc != null) {
  101. locationResult.gotLocation(gps_loc);
  102. return;
  103. }
  104. if (net_loc != null) {
  105. locationResult.gotLocation(net_loc);
  106. return;
  107. }
  108. locationResult.gotLocation(null);
  109. }
  110. }
  111.  
  112. public static abstract class LocationResult {
  113. public abstract void gotLocation(Location location);
  114. }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement