我正在开发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 Gap error箭头之间的直线是差距。
我也尝试了另一部智能手机中的应用程序,结果相同
我有什么办法可以改善GPS更新吗?
无法保证您始终拥有GPS接收功能。
没有GPS可能是由几个因素引起的,例如没有正确查看几颗卫星,但也可能是因为您的手机试图节省电池,而您的操作系统决定禁用GPS。关于GPS被禁用的确切规则对于iOS和Android都没有很好的记录,但是有意义的是,没有焦点的后台服务或应用程序往往会更快地禁用GPS。
如果(无论出于何种原因)没有GPS,您的手机可以回退到其他方法,如GSM手机信号塔三角测量,或者知道WiFi位置是否在视野中。这对电池更友好了。
但请注意,如果您的位置由GSM塔确定,您的准确度会下降很多,如果它是附近使用的WiFi路由器,则位置管理器每秒都会返回一个位置,但每次都是相同的坐标,某些东西更改(例如GPS返回,或找到新的WiFi路由器)。
在这种情况下,您还会看到您正在经历的事情。
当然,您还可能因其他原因而遇到操作系统停止运行的应用程序或服务。