【发布时间】:2016-01-15 14:07:20
【问题描述】:
我的 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();
}
}
我遵循了 android 教程,一切似乎都一样,但没有调用 onLocationChanged(在这种情况下,不显示消息)。 顺便说一句,它似乎在大约 3 周前起作用了,我不知道发生了什么变化。
【问题讨论】:
-
您要等多久才能更新位置?您可能还想设置
mLocationRequest.setSmallestDisplacement(desiredResolutionInMeters)。 -
我想够长了,即使几分钟后仍然没有结果。当它起作用时,它立即完成。
-
您期待
onLocationChanged的确切反馈是什么?你还没有实现displayMessage。