我有一个原型应用程序,它使用AndroidStudio中设置的Geofencing,并且能够在Android模拟器中成功测试它。因为我需要应用程序也是iOS我已经将原型移植到Xamarin / Visual Studio 2017以确保它在该环境中工作,因此我可以避免在Android和iOS中编写应用程序的核心逻辑。但是,我无法在同一个模拟器上使用基于Xamarin的应用程序启动Geofences。有没有人在Xamarin中使用过这项技术?是否有特定的设置需要更改Xamarin才能使其工作?
答案 0 :(得分:0)
问题可能来自清单。
在Xamarin中,当您创建服务(或意图服务)时,应使用属性[Service]
对其进行标记,而不是手动将其添加到清单中。
您还应该在处理意图时检查错误(如果您尚未执行此操作):
[Service]
public class GeofenceTransitionsIntentService : IntentService, IEnableDatabaseLogger
{
public GeofenceTransitionsIntentService()
: base(nameof(GeofenceTransitionsIntentService)) { }
protected override void OnHandleIntent(Intent intent)
{
base.OnHandleIntent(intent);
this.Log().Info("Intent received");
var geofencingEvent = GeofencingEvent.FromIntent(intent);
if (geofencingEvent.HasError)
{
var errorMessage = GeofenceErrorMessages.GetErrorString(this, geofencingEvent.ErrorCode);
this.Log().Error(errorMessage);
return;
}
var geofenceTransition = geofencingEvent.GeofenceTransition;
var geofences = geofencingEvent.TriggeringGeofences;
var location = geofencingEvent.TriggeringLocation;
if (geofenceTransition == Geofence.GeofenceTransitionEnter)
{
foreach (var geofence in geofences)
this.Log().Info($"Entered {geofence.RequestId} at {location.Latitude}/{location.Longitude}");
// do something
}
else if (geofenceTransition == Geofence.GeofenceTransitionExit)
{
foreach (var geofence in geofences)
this.Log().Info($"Exited {geofence.RequestId} at {location.Latitude}/{location.Longitude}");
// do something
}
else
{
this.Log().Error($"Geofence transition invalid type: {geofenceTransition}");
}
}
}
这是我最近做的一个演示(工作)项目:https://github.com/xleon/geofencing-playground