Advertisement
Guest User

Untitled

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