Android LocationClient onDisconnect 不会被调用

标签 android location connect location-client

来自GooglePlayServicesClient.ConnectionCallbacks当 LocationClient 断开连接时,应该调用 onDisconnected 文档。根据我的测试,我注意到如果我自己断开 LocationClient,它永远不会被调用。

这是我拥有的最基本的示例代码。

public class MainActivity extends ActionBarActivity implements GooglePlayServicesClient.ConnectionCallbacks,
                                                               GooglePlayServicesClient.OnConnectionFailedListener {

  private LocationClient locationClient;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    locationClient = new LocationClient(getApplicationContext(), this, this);
    locationClient.connect();
  }

  @Override
  public void onConnected(Bundle bundle) {
    Log.d("APP", "Location Client Connected");
    locationClient.disconnect();
    Log.d("APP", "Client is connected " + locationClient.isConnected());
  }

  @Override
  public void onDisconnected() {
    Log.d("APP", "Location Client Disconnected");
  }

  @Override
  public void onConnectionFailed(ConnectionResult connectionResult) {
    Log.d("APP", "Location Client connect failed");
  }
}

我收到了 onConnected() 回调,但没有收到 onDisconnected() 回调。在我打印出客户端连接状态的 onConnected 回调中,它正确地打印出“false”。

我是不是误解了什么,或者 onDisconnected 回调仅由外部事件调用,例如:

  • Activity/服务关闭
  • 定位客户端不可用

最佳答案

我遇到了同样的问题并找到了答案 here :

I guess it's a bit late but I was wondering the same, so I'll reply.

It's normal behaviour. It's not clear from the API documentation, but in the official tutorial at http://developer.android.com/training/location/retrieve-current.html, it says about onDisconnected:

Called by Location Services if the connection to the location client drops because of an error.

这意味着如果您自己调用 disconnect() 则不应调用它。

关于Android LocationClient onDisconnect 不会被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26197842/

相关文章:

java - 为什么 Java ServerSocket 接受方法会为来自客户端的一个连接请求返回两次套接字?

flash as3 在两个圆圈之间画一条线

Android Studio .idea 和 .gradle 文件夹是否保留在源代码管理中?

android - install_referrer Intent 弃用和更新库

python-3.x - python : How to extract addresses from a sentence/paragraph (non-Regex approach)?

java - Android 服务不更新位置

node.js - Node.js Express 是否包含中间件列表?

android - 以编程方式添加 EditText 缺失样式

java - 为什么我无法在 'SettingActivity' 中获取个人资料图片?

android - 无法获取位置