【问题标题】:onLocationChanged isn't being calledonLocationChanged 没有被调用
【发布时间】: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

标签: android android-studio


【解决方案1】:

我怀疑问题出在mRequestingLocationUpdates 的某个地方。当设置mRequestingLocationUpdates 时(如果有的话),您的代码中没有提供任何指示。

同样对于使用物理设备的位置更新,获取位置修复的时间取决于设备上允许的内容:设置->位置。首先,需要启用位置。该模式将确定是否启用 GPS 和 WiFi 或蓝牙定位。只要您在有 WiFi/蓝牙覆盖的区域并且您有数据连接,WiFi 和蓝牙位置应该非常快(几秒钟)。 GPS 非常准确,但可能非常慢(几分钟),具体取决于天空中卫星的可见性以及您是否有辅助 GPS 的数据连接。

【讨论】:

  • mRequestingLocationUpdates 是一个设置为 true 的全局变量,并且位置服务已启用,因此不幸的是,两者都不是问题
  • 并且您验证了startLocationUpdates() 被调用并且那里使用的所有参数都是正确的?
  • @suue 如果mRequestingLocationUpdates 设置为true,onResume 将不会调用startLocationUpdates
  • 但是 onConnected 应该,对吧? @WarrenFaith startLocationUpdates() 被调用且参数正确
猜你喜欢
  • 2023-03-07
  • 2017-05-14
  • 2015-10-10
  • 2016-09-18
  • 1970-01-01
  • 2019-02-07
  • 1970-01-01
  • 2016-11-14
  • 1970-01-01
相关资源
最近更新 更多