我正在开发一个GPS监控应用程序,但是当应用程序在后台运行时,我在接收GPS更新时遇到问题。 在路线期间,GPS 停止更新,仅在一段时间后再次开始运行。
我正在使用具有以下参数的 FusedLocationProviderClient api:
return new LocationRequest()
.SetInterval( 2000 )
.SetFastestInterval( 500 )
.SetSmallestDisplacement(1)
.SetPriority( LocationRequest.PriorityHighAccuracy );
我使用混合服务来跟踪 GPS 更新。 当我启动应用程序时,我使用它绑定:
public PortalCorridaServiceConnection BindService() {
if( ServiceConnection == null ) {
ServiceConnection = new PortalCorridaServiceConnection( Activity );
Intent intentService = new Intent( Activity, typeof( PortalCorridaService ) );
Activity.BindService( intentService, ServiceConnection, Bind.AutoCreate );
}
return ServiceConnection;
}
当按下"开始跟踪按钮"时,我像这样在前台启动我的服务
public void StartForegroundService() {
MakeToast( "Servico iniciado em foreground" );
Intent intent = new Intent( Activity, typeof( Engine.PortalCorridaService ) );
if( Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.O ) {
Activity.StartForegroundService( intent );
} else {
Activity.StartService( intent );
}
}
public void RegisterForegroundService() {
// Instantiate the builder and set notification elements:
NotificationCompat.Builder builder = new NotificationCompat.Builder( this, "PORTALCORRIDA" )
.SetContentTitle( "Portal da Corrida" )
.SetContentText( "Estamos monitorando sua corrida" )
.SetSmallIcon( Resource.Drawable.ic_media_play_light );
// Build the notification:
MainNotification = builder.Build();
// Get the notification manager:
NotificationManager notificationManager = GetSystemService( Context.NotificationService ) as NotificationManager;
// Publish the notification:
//const int notificationId = 0;
//notificationManager.Notify( notificationId, notification );
// Enlist this instance of the service as a foreground service
StartForeground( SERVICE_RUNNING_NOTIFICATION_ID, MainNotification );
}
然后我打电话
fusedLocationProviderClient.RequestLocationUpdatesAsync( CreateLocationRequest(), locationCallback );
全球定位系统间隙错误 箭头之间的直线是间隙。
我也在另一部智能手机上尝试了该应用程序,结果相同
我能做些什么来改进GPS更新吗?
不能保证您始终拥有 GPS 接收。
没有 GPS 可能是由多种因素引起的,例如无法正确查看多颗卫星,但也可能是因为您的手机试图节省电池电量,并且您的操作系统决定禁用 GPS。iOS和Android禁用GPS的确切规则并没有很好的记录,但是没有重点的后台服务或应用程序往往会更快地禁用GPS,这是有道理的。
如果(无论出于何种原因)没有GPS,您的手机可以回退到其他方法,例如GSM蜂窝塔三角测量,或者知道WiFi位置(如果它们在视野中)。这更方便电池。
但请注意,如果您的位置是由GSM信号塔确定的,那么您的精度会下降很多,如果使用的是附近的WiFi路由器,则位置管理器每秒都会返回一个位置,但每次都会是相同的坐标,有些东西会发生变化(例如GPS回来了,或者找到了新的WiFi路由器)。
在这种情况下,您还将看到您正在经历的事情。
当然,您也可能遇到操作系统因其他原因而停止您的应用程序或服务的情况。