我在这是怎么回事,而他们的工作,即使应用程序被关闭跟踪用户Xamarin项目。基本上,这是一个为我们的客户(我们有ERP)的应用程序,用于跟踪他们的卖家。
为此,我实现了一个非常简单的BroadcastReceiver来启动服务(LocationService),该服务将获取用户的位置并将其保存在数据库中或将其发布到某些API中。
的问题是,当我不使用的LocationManager,它工作正常(I关闭该应用程序,并将其保持烘烤消息例如)。但是,如果我使用LocationManager,则应用程序关闭后服务会“崩溃”并停止跟踪。难道我做错了什么?该项目是完全配置用于GPS跟踪(正常工作时的应用程序是打开或悬浮)。
PS:这是我第一次使用Xamarin.Android。之前,我刚刚与Xamarin.Forms工作。
这里是LogCat。触发AlarmManager并尝试获取位置时似乎会发生这种情况。
这是我的代码:
开始跟踪
private void StartTrackingAlarm(int period)
{
Intent intent = new Intent(this, typeof(TrackingService));
PendingIntent pendingIntent = PendingIntent.GetBroadcast(this, 0, intent, PendingIntentFlags.CancelCurrent);
WriteLine($"Started tracking with period of {period} seconds.");
AlarmManager manager = (AlarmManager)GetSystemService(AlarmService);
manager.SetRepeating(AlarmType.RtcWakeup, SystemClock.ElapsedRealtime(), period * 1000, pendingIntent);
}
private void StopTrackingAlarm()
{
Intent intent = new Intent(this, typeof(TrackingService));
PendingIntent pendingIntent = PendingIntent.GetBroadcast(this, 0, intent, 0);
WriteLine($"Stopped tracking at {DateTime.Now.ToLongTimeString()} seconds.");
AlarmManager manager = (AlarmManager)GetSystemService(AlarmService);
manager.Cancel(pendingIntent);
}
广播接收器
[BroadcastReceiver(Enabled = true)]
[IntentFilter(new string[] { "android.intent.action.BOOT_COMPLETED" }, Priority = (int)IntentFilterPriority.HighPriority)]
public class TrackingService : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
Intent intentToStart = new Intent(context, typeof(LocationService));
context.StartService(intentToStart);
}
}
LocationService
[Service]
public class LocationService : IntentService, ILocationListener
{
private string LogTag => $"[{nameof(LocationService)}]";
private LocationManager LocManager { get; set; }
private string LocationProvider { get; set; }
protected override void OnHandleIntent(Intent intent)
{
Log.WriteLine(LogPriority.Debug, LogTag, $"Handling intent at {DateTime.Now.ToLongTimeString()}");
InitializeLocationManager();
if (LocManager.IsProviderEnabled(LocationProvider))
{
LocManager.RequestSingleUpdate(LocationProvider, this, null);
Location location = LocManager.GetLastKnownLocation(LocationProvider);
Log.WriteLine(LogPriority.Debug, LogTag, $"Last Position: [{location.Latitude}, {location.Longitude}].");
}
}
private void InitializeLocationManager()
{
LocManager = (LocationManager)GetSystemService(LocationService);
Criteria criteriaForLocationService = new Criteria { Accuracy = Accuracy.Fine };
IList<string> acceptableLocationProviders = LocManager.GetProviders(criteriaForLocationService, true);
if (acceptableLocationProviders.Any())
LocationProvider = acceptableLocationProviders.First();
else
LocationProvider = string.Empty;
Log.Debug(LogTag, $"Using {LocationProvider} at {DateTime.Now}.");
}
#region ILocationListener
public void OnLocationChanged(Location location) { }
public void OnProviderDisabled(string provider) { }
public void OnProviderEnabled(string provider) { }
public void OnStatusChanged(string provider, [GeneratedEnum] Availability status, Bundle extras) { }
#endregion
}