没有调用onLocationChanged [英] onLocationChanged isn't being called

查看:49
本文介绍了没有调用onLocationChanged的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的onLocationChanged根本没有被调用.有什么想法吗?我拥有所有权限.这是我的代码:

My onLocationChanged is not called at all. Any ideas why? I have all the permissions. Here is my code:

 @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周,我不知道发生了什么变化.

I followed android tutorial and it all seems to be the same, but onLocationChanged isn't being called (in this case message is not displayed). Btw, it seemed to have worked about 3 weeks ago, I don't know what changed.

推荐答案

我怀疑问题出在 mRequestingLocationUpdates 上.如果设置了 mRequestingLocationUpdates (如果有的话),则代码中没有任何指示.

I suspect the problem is somewhere with mRequestingLocationUpdates. There is no indication in your code provided when, if at all, mRequestingLocationUpdates is set.

对于物理设备的位置更新,获得位置修复的时间也取决于设备允许的位置:设置"->位置".首先,需要启用位置.该模式将确定是否启用了GPS和WiFi或蓝牙位置.只要您位于具有WiFi/蓝牙覆盖范围的区域中并且您已建立数据连接,WiFi和蓝牙的位置就应该非常快(几秒钟).GPS非常精确,但可能会很慢(几分钟),具体取决于天空中卫星的可见性以及是否有辅助GPS的数据连接.

Also for location updates with physical devices, the time to get a location fix depends on what is allowed on the device: Settings->Location. First, location needs to enabled. The mode will determine whether GPS and WiFi or Bluetooth location are enabled. WiFi and Bluetooth location should be very fast (few seconds) as long as you are in an area with WiFi/Bluetooth coverage and you have a data connection. GPS is very accurate, but can be very slow (minutes), depending on visibility to satellites in the sky and if you have a data connection for assisted GPS.

这篇关于没有调用onLocationChanged的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆