Last active
October 16, 2017 18:02
-
-
Save graco911/d62ade83c4836cb2c7f86b33534bdb68 to your computer and use it in GitHub Desktop.
GPSTracker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class GPSTracker : Service, ILocationListener | |
{ | |
private Context context; | |
bool isGPSEnabled = false; | |
public bool isNetworkEnabled { get; set; } = false; | |
public bool canGetLocation { get; set; } = false; | |
Location location; | |
public double longitude { get; set; } | |
public double latitude { get; set; } | |
private const long MIN_DISTANCE = 10; | |
private const long MIN_TIME = 1000 * 60 * 1; | |
protected LocationManager locationManager; | |
public GPSTracker(Context context) | |
{ | |
this.context = context; | |
getLocation(); | |
} | |
public Location getLocation() | |
{ | |
try | |
{ | |
locationManager = (LocationManager)context.GetSystemService(Service.LocationService); | |
isGPSEnabled = locationManager.IsProviderEnabled(LocationManager.GpsProvider); | |
isNetworkEnabled = locationManager.IsProviderEnabled(LocationManager.NetworkProvider); | |
if (!isGPSEnabled && !isNetworkEnabled) | |
{ | |
//Util.CreateNotification(this, "Informacion", "No hay red.", 1); | |
} | |
else | |
{ | |
canGetLocation = true; | |
if (isNetworkEnabled) | |
{ | |
locationManager.RequestLocationUpdates(LocationManager.NetworkProvider, MIN_TIME, MIN_DISTANCE, this); | |
if (locationManager != null) | |
{ | |
location = locationManager.GetLastKnownLocation(LocationManager.NetworkProvider); | |
if (location != null) | |
{ | |
latitude = location.Latitude; | |
longitude = location.Longitude; | |
} | |
} | |
} | |
if (isGPSEnabled) | |
{ | |
if (location == null) | |
{ | |
locationManager.RequestLocationUpdates(LocationManager.GpsProvider, MIN_TIME, MIN_DISTANCE, this); | |
if (locationManager != null) | |
{ | |
location = locationManager.GetLastKnownLocation(LocationManager.GpsProvider); | |
if (location != null) | |
{ | |
latitude = location.Latitude; | |
longitude = location.Longitude; | |
} | |
} | |
} | |
} | |
} | |
} | |
catch (Exception e) | |
{ | |
} | |
return location; | |
} | |
public void StopUsingGPS() | |
{ | |
if (locationManager != null) | |
{ | |
locationManager.RemoveUpdates(this); | |
} | |
} | |
public override IBinder OnBind(Intent intent) | |
{ | |
return null; | |
} | |
public void OnLocationChanged(Location location) | |
{ | |
//Util.CreateNotification(this, "Informacion", "On location changed.", 1); | |
} | |
public void OnProviderDisabled(string provider) | |
{ | |
//Util.CreateNotification(this, "Informacion", "Provider Disabled.", 1); | |
} | |
public void OnProviderEnabled(string provider) | |
{ | |
//Util.CreateNotification(this, "Informacion", "Provider Enabled.", 1); | |
} | |
public void OnStatusChanged(string provider, [GeneratedEnum] Availability status, Bundle extras) | |
{ | |
//Util.CreateNotification(this, "Informacion", "Status Changed.", 1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment