简体   繁体   English

通过后台位置跟踪和FusedLocationApi,位置更新的频率降低

[英]Frequency of location updates decreases with background location tracking and FusedLocationApi

Google Play Services' Fused Location Provider Api lets you request location updates with location listeners or pending intents. Google Play服务的“融合的位置提供者Api”可让您使用位置侦听器或待处理的意图请求位置更新。 I can successfully request location updates with the location listener but I have been struggling to replicate the same behaviour with pending intents. 我可以使用位置侦听器成功请求位置更新,但是我一直在努力通过未决的意图复制相同的行为。 For the latter, I launch an intent service which handles the location data. 对于后者,我启动了一个意图服务来处理位置数据。 What I have noticed during testing is that the location updates correspond with the interval I set in the location request. 我在测试过程中注意到,位置更新与我在位置请求中设置的间隔相对应。 However, as time goes by the interval between updates increases tremendously even though the interval in the location request has remained constant. 但是,随着时间的流逝,即使位置请求中的间隔保持不变,更新之间的间隔也会大大增加。 I have noticed this behaviour on several occasions with multiple devices. 我在多个设备上多次注意到此行为。 Does anyone have an idea what could be going on? 有谁知道会发生什么?

Foreground location tracking 前景位置跟踪

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);
}

Background location tracking 后台位置跟踪

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);
    } 
}

Any help is greatly appreciated. 任何帮助是极大的赞赏。

It could be because some other application in the device must have requested for more frequent updates. 可能是因为设备中的某些其他应用程序必须请求更频繁的更新。 Please refer this link for more details 请参考此链接以获取更多详细信息

This interval is inexact. 此间隔不精确。 You may not receive updates at all (if no location sources are available), or you may receive them slower than requested. 您可能根本不会收到更新(如果没有可用的位置来源),或者收到的更新速度可能比要求的要慢。 You may also receive them faster than requested (if other applications are requesting location at a faster interval). 您可能还会比要求的接收速度更快(如果其他应用程序以更快的间隔请求定位)。 The fastest rate that that you will receive updates can be controlled with setFastestInterval(long) 可以通过setFastestInterval(long)控制您将收到的更新的最快速率

I'm having the same problem too, but as @Shiv said you should be testing different parameters: 我也遇到同样的问题,但是正如@Shiv所说的,您应该测试不同的参数:

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

try this: 尝试这个:

 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM