我的onLocationChanged根本不被調用。任何想法爲什麼?我擁有所有權限。這裏是我的代碼:onLocationChanged未被調用
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_locate_me);
createLocationRequest();
buildGoogleApiClient();
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
MapFragment mapFragment = ((MapFragment) getFragmentManager().findFragmentById(R.id.map));
mapFragment.setRetainInstance(true);
}
@Override
protected void onStart() {
super.onStart();
if (mGoogleApiClient != null) {
mGoogleApiClient.connect();
}
}
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
protected void createLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(10000);
mLocationRequest.setFastestInterval(5000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
@Override
public void onConnected(Bundle bundle) {
if (mRequestingLocationUpdates) {
startLocationUpdates();
}
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onLocationChanged(Location location) {
mCurrentLocation = location;
displayMessage("message","my position changed");
}
protected void startLocationUpdates() {
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, this);
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
private void displayMessage(String title, String message) {
//displaying a message
}
@Override
protected void onPause() {
super.onPause();
stopLocationUpdates();
}
protected void stopLocationUpdates() {
LocationServices.FusedLocationApi.removeLocationUpdates(
mGoogleApiClient, this);
}
@Override
public void onResume() {
super.onResume();
if (mGoogleApiClient.isConnected() && !mRequestingLocationUpdates) {
startLocationUpdates();
}
}
我跟着機器人的教程,這一切似乎是相同的,但onLocationChanged不會被調用(在這種情況下,消息不顯示)。 順便說一句,它似乎在3個星期前工作過,我不知道是什麼改變了。
您等待位置更新需要多長時間?你可能還想設置'mLocationRequest.setSmallestDisplacement(desiredResolutionInMeters)'。 – mattm
足夠長的時間我認爲,即使在幾分鐘後仍然沒有結果。當它工作時,它立即完成。 – suue
你對「onLocationChanged」的期待是什麼?你還沒有實現'displayMessage'。 – mattm