当前位置: 首页 > 知识库问答 >
问题:

位置更新的频率随着背景位置跟踪和FusedLocationApi而降低

马俊
2023-03-14

Google Play Services的融合位置提供程序Api允许您使用位置侦听器或待定意图请求位置更新。我可以通过位置监听器成功地请求位置更新,但我一直在努力复制相同的行为和未决的意图。对于后者,我启动了一个intent服务来处理位置数据。我在测试过程中注意到,位置更新与我在位置请求中设置的间隔相对应。然而,随着时间的推移,更新之间的间隔大大增加,即使位置请求中的间隔保持不变。我在多台设备上多次注意到这种行为。有人知道会发生什么吗?

前台位置跟踪

protected void requestLocationUpdates()
{
    mIsTracking = true;
    LocationRequest locationRequest = mLocationRequests.get(mPreviousDetectedActivity.getType());
    if (locationRequest != null)
    {
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, locationRequest, this);
    }
}

@Override
public void onLocationChanged(Location location)
{
    Log.i(TAG, "onLocationChanged");
    handleLocationChanged(location);
}

背景位置跟踪

protected void requestLocationUpdates()
{
    LocationRequest locationRequest = mLocationRequests.get(mPreviousDetectedActivity.getType());
    if (locationRequest != null)
    {
        mResultCallabackMessage = "Request location updates ";
        PendingIntent pendingIntent = getLocationPendingIntent();
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,
                locationRequest, pendingIntent).setResultCallback(this);
    }
}

protected PendingIntent getLocationPendingIntent()
{
    if (mLocationPendingIntent != null) return mLocationPendingIntent;

    Intent intent = new Intent(this, LocationUpdatesIntentService.class);
    mLocationPendingIntent = PendingIntent.getService(this, 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);
    return mLocationPendingIntent;
}

public class LocationUpdatesIntentService extends IntentService
{

    public LocationUpdatesIntentService()
    {
        // Use the TAG to name the worker thread.
        super(TAG);
    }

    @Override
    public void onCreate()
    {
        super.onCreate();
    }

    @Override
    protected void onHandleIntent(Intent intent)
    {
        Bundle bundle = intent.getExtras();
        Location location = (Location)   bundle.get(FusedLocationProviderApi.KEY_LOCATION_CHANGED);

        handleLocationUpdates(location);
    } 
}

任何帮助都非常感谢。

共有2个答案

步博厚
2023-03-14

我也有同样的问题,但正如@Shiv所说,你应该测试不同的参数:

 mClient.requestLocationUpdates(LocationRequest.create(), mLocationIntent)

试试这个:

 mClient.requestLocationUpdates(createLocationRequest(), mLocationIntent)

private LocationRequest createLocationRequest() {
    LocationRequest locationRequest = new LocationRequest();
    locationRequest.setInterval(UPDATE_INTERVAL);
    locationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL);
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    locationRequest.setMaxWaitTime(MAX_WAIT_TIME);
    return locationRequest;
}
马阳晖
2023-03-14

这可能是因为设备中的其他应用程序必须请求更频繁的更新。请参考此链接了解更多详细信息

这个间隔是不精确的。您可能根本没有收到更新(如果没有可用的位置源),或者接收更新的速度可能比请求的慢。您也可能会比请求更快地接收它们(如果其他应用程序以更快的间隔请求位置)。使用SetFastTestInterval(长)可以控制您接收更新的最快速度

 类似资料:
  • 除了背景背景平铺外,CSS还提供了另一个强大的功能,即背景定位技术,能够精确控制背景在对象中的位置。 默认情况下,背景图像都是从元素 padding 区域的左上角开始出现的,但设计师往往希望背景能够出现在任何位置。通过 background-position属性,可以很轻松的控制背景图像在对象的背景区域中的起始显示位置。其语法格式为: background-position: xpos ypos

  • 问题内容: 我希望每t毫秒定期跟踪一次鼠标光标的位置。因此,从本质上讲,当页面加载时- 该跟踪器应该启动,并且每(例如)每100毫秒启动一次,我应该获取posX和posY的新值并以表格形式打印出来。 我尝试了以下代码-但值没有刷新-表单框中仅显示posX和posY的初始值。关于如何启动和运行它的任何想法? 问题答案: 在事件处理程序接收到的对象上报告鼠标的位置,您可以将其附加到窗口(事件冒泡):

  • 默认情况下,背景图像从元素的 padding 区域的左上角开始显示。然而,有时候却希望背景图像从边框的左上角、或内容区的左上角开始显示。 这时,就可以通过 background-origin属性来设置背景图像的起始显示位置,可选值有 border-box | padding-box | content-box,默认值为 padding-box。 border-box 表示背景图像从 border

  • 我正在Xamarin中实现一个android应用程序。Android平台,该应用程序用于跟踪他旅行的距离和路线。通过使用低版本和高版本的android OS的后台定位服务和前台定位服务。 当用户关闭应用程序时,我的前台服务停止,无法跟踪距离和路线,我在更高版本的Android设备(8.0操作系统版本以上)中面临这个问题。它给了我只有起点和终点的距离与Arial路线。 请给我任何其他方法来解决我的问

  • 问题内容: 如果对http请求的响应是带有cookie的重定向(http代码302), 您如何指示Go客户使用收到的Cookie跟踪新位置? 在CURL中,可以通过以下方式轻松设置客户端: 您如何在Go中做到这一点? 问题答案: 使用Go 1.1,您可以使用。 这是一个工作示例: