Guest User

Untitled

a guest
Oct 17th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 KB | None | 0 0
  1. class GPSTracker : Service, ILocationListener
  2. {
  3. private Context context;
  4. bool isGPSEnabled = false;
  5. public bool isNetworkEnabled { get; set; } = false;
  6. public bool canGetLocation { get; set; } = false;
  7. Location location;
  8. public double longitude { get; set; }
  9. public double latitude { get; set; }
  10.  
  11. private const long MIN_DISTANCE = 10;
  12. private const long MIN_TIME = 1000 * 60 * 1;
  13.  
  14. protected LocationManager locationManager;
  15.  
  16. public GPSTracker(Context context)
  17. {
  18. this.context = context;
  19. getLocation();
  20. }
  21.  
  22. public Location getLocation()
  23. {
  24. try
  25. {
  26. locationManager = (LocationManager)context.GetSystemService(Service.LocationService);
  27.  
  28. isGPSEnabled = locationManager.IsProviderEnabled(LocationManager.GpsProvider);
  29.  
  30. isNetworkEnabled = locationManager.IsProviderEnabled(LocationManager.NetworkProvider);
  31.  
  32. if (!isGPSEnabled && !isNetworkEnabled)
  33. {
  34. //Util.CreateNotification(this, "Informacion", "No hay red.", 1);
  35. }
  36. else
  37. {
  38. canGetLocation = true;
  39. if (isNetworkEnabled)
  40. {
  41. locationManager.RequestLocationUpdates(LocationManager.NetworkProvider, MIN_TIME, MIN_DISTANCE, this);
  42. if (locationManager != null)
  43. {
  44. location = locationManager.GetLastKnownLocation(LocationManager.NetworkProvider);
  45. if (location != null)
  46. {
  47. latitude = location.Latitude;
  48. longitude = location.Longitude;
  49. }
  50. }
  51. }
  52. if (isGPSEnabled)
  53. {
  54. if (location == null)
  55. {
  56. locationManager.RequestLocationUpdates(LocationManager.GpsProvider, MIN_TIME, MIN_DISTANCE, this);
  57. if (locationManager != null)
  58. {
  59. location = locationManager.GetLastKnownLocation(LocationManager.GpsProvider);
  60. if (location != null)
  61. {
  62. latitude = location.Latitude;
  63. longitude = location.Longitude;
  64. }
  65. }
  66. }
  67. }
  68. }
  69. }
  70. catch (Exception e)
  71. {
  72.  
  73. }
  74. return location;
  75. }
  76.  
  77. public void StopUsingGPS()
  78. {
  79. if (locationManager != null)
  80. {
  81. locationManager.RemoveUpdates(this);
  82. }
  83. }
  84.  
  85. public override IBinder OnBind(Intent intent)
  86. {
  87. return null;
  88. }
  89.  
  90. public void OnLocationChanged(Location location)
  91. {
  92. //Util.CreateNotification(this, "Informacion", "On location changed.", 1);
  93. }
  94.  
  95. public void OnProviderDisabled(string provider)
  96. {
  97. //Util.CreateNotification(this, "Informacion", "Provider Disabled.", 1);
  98.  
  99. }
  100.  
  101. public void OnProviderEnabled(string provider)
  102. {
  103. //Util.CreateNotification(this, "Informacion", "Provider Enabled.", 1);
  104. }
  105.  
  106. public void OnStatusChanged(string provider, [GeneratedEnum] Availability status, Bundle extras)
  107. {
  108. //Util.CreateNotification(this, "Informacion", "Status Changed.", 1);
  109. }
Add Comment
Please, Sign In to add comment