Fused Location Provider只有在用户的位置发生变化时才会调用onLocationChanged()

时间:2014-09-16 14:18:41

标签: android location

我的应用仅在用户位置更改时才需要用户的位置。当wifi / gps打开时以及我编码时,我可以很容易地知道用户位置:

mLocationRequest.setInterval(LocationUtils.UPDATE_INTERVAL_IN_MILLISECONDS);    //10 seconds                    
mLocationRequest.setFastestInterval(LocationUtils.FAST_INTERVAL_CEILING_IN_MILLISECONDS);  

通过对此进行编码,即使用户的位置未更改,也会在每10秒后调用onLocationChanged()。为什么? 这是不必要的耗尽电池。我希望当用户移动时,只有我的应用程序应该有更新的位置。 在Google I / O 2013中,据说通过传感器(融合位置提供商),如果用户位于一个地方,则无需跟踪用户的位置。 我评论了以上几行并编码:

    mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
    mLocationRequest.setSmallestDisplacement(50.0f);    //50 meters

我只能看到一次,我走了50米,并且调用了onLocationChanged()。我以前从未见过onLocationChanged()。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Get handles to the UI view objects

    // Create a new global location parameters object
    mLocationRequest = LocationRequest.create();

    /**
     * If below two lines are commented the location change method is not called.
     */
   // mLocationRequest.setInterval(LocationUtils.UPDATE_INTERVAL_IN_MILLISECONDS);                      //Set the update interval
    //mLocationRequest.setFastestInterval(LocationUtils.FAST_INTERVAL_CEILING_IN_MILLISECONDS);         // Set the interval ceiling to one minute

    // Use high accuracy
    mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
    mLocationRequest.setSmallestDisplacement(50.0f);


    /*
     * Create a new location client, using the enclosing class to
     * handle callbacks.
     */
    mLocationClient = new LocationClient(this, this, this);

}

@Override
public void onStart() {

    super.onStart();
    mLocationClient.connect();

}
}

  public void getLocation(View v) {

    // If Google Play Services is available
    if (servicesConnected()) {

        // Get the current location
        Location currentLocation = mLocationClient.getLastLocation();

        // Display the current location in the UI
        mLatLng.append("\nLast Location: "+LocationUtils.getLatLng(this, currentLocation));
    }
}

public void startUpdates(View v) {
    mUpdatesRequested = true;

    if (servicesConnected()) {
        startPeriodicUpdates();
    }
}

@Override
public void onConnected(Bundle bundle) {
    mConnectionStatus.setText(R.string.connected);

    if (mUpdatesRequested) {
        startPeriodicUpdates();
    }
}

@覆盖     public void onConnected(Bundle bundle){         mConnectionStatus.setText(R.string.connected);

    if (mUpdatesRequested) {
        startPeriodicUpdates();
    }
}

public void onLocationChanged(位置位置){

    // Report to the UI that the location was updated
    mConnectionStatus.setText(R.string.location_updated);

    // In the UI, set the latitude and longitude to the value received
    mLatLng.append("\nLoc Changed:"+LocationUtils.getLatLng(this, location));
}

/**
 * In response to a request to start updates, send a request
 * to Location Services
 */
private void startPeriodicUpdates() {

    mLocationClient.requestLocationUpdates(mLocationRequest, this);
    mConnectionState.setText(R.string.location_requested);
}

如何仅在用户位置更改时调用onLocationChanged()。如果用户坐在一个地方,则不应调用此方法。

0 个答案:

没有答案