我正在登录期间以Xamarin形式启动LocationService。 并且此服务每5分钟运行一次,获取位置并将其存储在本地数据库中。 由于某些未知原因,位置服务在几个小时后无法正常工作。
每当该应用被杀死并打开时,我都会检查服务是否已停止。如果停止了,我将再次按以下方式启动服务:
在OnStart和OnResume中,我正在检查正在运行的服务,如下所示:
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;
}
每次我都会得到Is LocationRunning为true(不是)并且服务将无法启动。