I'm starting a LocationService in Xamarin forms during login. And this service runs for every 5 mins, fetches location and stores in local DB. For some unknown reason location service is not working after some hours.
Whenever the app is killed and opened, I'm checking to see if the service has stopped. If it is stopped I'm starting the service again as follows:
In OnStart and OnResume I'm checking for running service as follows:
bool IsLocationServiceRunning = DependencyService.Get<IServiceRunning>().IsServiceRunning();
if (!IsLocationServiceRunning)
DependencyService.Get<IDeviceService>().StartLocationService();
ServiceRunningInterface:
public bool IsServiceRunning()
{
ActivityManager manager = (ActivityManager)Forms.Context.GetSystemService(Context.ActivityService);
Type serviceClass = typeof(LocationService);
foreach (var service in manager.GetRunningServices(int.MaxValue))
{
if (service.Service.ClassName.EndsWith(typeof(LocationService).Name))
{
return true;
}
}
}
return false;
}
Every time I will get Is LocationRunning as true (which is not) and service won't get started.