Last active
May 1, 2017 00:37
-
-
Save Foovanadil/5e939d9b31377ac35183 to your computer and use it in GitHub Desktop.
Xamarin Android ILocationListener
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
[Service] | |
[IntentFilter(new[] { "com.sdtig.Todo.START_LOCATION", "com.sdtig.Todo.SET_GEOFENCE" })] | |
public class GeofencingHelper : Service, Android.Locations.ILocationListener | |
{ | |
public override Android.OS.IBinder OnBind (Intent intent) | |
{ | |
return null; | |
} | |
public override void OnStart (Intent intent, int startId) | |
{ | |
if (intent.Action == "com.sdtig.Todo.START_LOCATION") | |
{ | |
//Elided | |
} | |
if (intent.Action == "com.sdtig.Todo.SET_GEOFENCE") | |
{ | |
SetFence(); | |
} | |
} | |
public void SetFence() | |
{ | |
bool isWithinRadius = false; | |
foreach (var fence in fences) | |
{ | |
float[] results = new float[1]; | |
Location.DistanceBetween(fence.Latitude, fence.Longitude, currentLatitude, currentLongitude, results); | |
float distanceInMeters = results[0]; | |
if (distanceInMeters < radiusInMeters) | |
{ | |
isWithinRadius = true; | |
break; | |
} | |
} | |
if (!isWithinRadius) | |
{ | |
var intent = new Intent(CustomActions.TODO_WITHIN_PROXIMITY); | |
PendingIntent pendingIntent = PendingIntent.GetService(this, 0, intent, | |
PendingIntentFlags.UpdateCurrent); | |
_locationManager.AddProximityAlert(currentLatitude, currentLongitude, | |
radiusInMeters, -1, pendingIntent); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment