Android 仅当用户';s位置更改

Android 仅当用户';s位置更改,android,location,Android,Location,我的应用程序仅在用户位置更改时才需要用户的位置。当打开wifi/gps时,我可以很容易地知道用户的位置,并且当我对此进行编码时: mLocationRequest.setInterval(LocationUtils.UPDATE_INTERVAL_IN_MILLISECONDS); //10 seconds mLocationRequest.setFastestInterval(LocationUtils.FAST_INTERVAL_CEILING_

我的应用程序仅在用户位置更改时才需要用户的位置。当打开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();
    }
}
@凌驾 未连接的公共空间(捆绑包){ mConnectionStatus.setText(R.string.connected)

已更改位置上的公共无效(位置){


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

找到解决方案了吗??
    if (mUpdatesRequested) {
        startPeriodicUpdates();
    }
}
    // 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);
}