我的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周前就起作用了,我不知道是什么改变了。
发布于 2016-01-15 22:57:08
我怀疑问题出在mRequestingLocationUpdates的某个地方。当设置了mRequestingLocationUpdates时,您的代码中没有提供任何指示。
此外,对于物理设备的位置更新,获取位置修复的时间取决于设备上允许的内容:设置->位置。首先,需要启用位置。该模式将确定是否启用GPS和WiFi或蓝牙定位。WiFi和蓝牙定位应该非常快(几秒钟),只要你在一个有WiFi/蓝牙覆盖的区域,并且你有一个数据连接。GPS非常准确,但可能非常慢(分钟),这取决于天空中卫星的可见度以及您是否有辅助GPS的数据连接。
https://stackoverflow.com/questions/34812845
复制相似问题