gpt4 book ai didi

Android LocationClient onDisconnect 不会被调用

转载 作者:行者123 更新时间:2023-12-02 01:44:57 32 4
gpt4 key购买 nike

来自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/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com