我在使用GoogleApiClient时发现了一些不太清楚的问题。
GoogleApiClient有一个名为onConnected的函数,当客户端连接成功后会运行该函数。
我自己编写了一个名为startLocationListening的函数,最终在GoogleApiClient的onConnected函数中被调用。
因此,我的startLocationListening函数无法在没有GoogleApiClient连接的情况下运行。
代码和日志:
@Override
public void onConnected(Bundle bundle) {
log("Google_Api_Client:connected.");
initLocationRequest();
startLocationListening(); //Exception caught inside this function
}
...
private void startLocationListening() {
log("Starting_location_listening:now");
//Exception caught here below:
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
例外情况是:
03-30 12:23:28.947: E/AndroidRuntime(4936): java.lang.IllegalStateException: GoogleApiClient is not connected yet.
03-30 12:23:28.947: E/AndroidRuntime(4936): at com.google.android.gms.internal.jx.a(Unknown Source)
03-30 12:23:28.947: E/AndroidRuntime(4936): at com.google.android.gms.common.api.c.b(Unknown Source)
03-30 12:23:28.947: E/AndroidRuntime(4936): at com.google.android.gms.internal.nf.requestLocationUpdates(Unknown Source)
03-30 12:23:28.947: E/AndroidRuntime(4936): at hu.company.testproject.service.GpsService.startLocationListening(GpsService.java:169)
03-30 12:23:28.947: E/AndroidRuntime(4936): at hu.company.testproject.service.GpsService.onConnected(GpsService.java:259)
我的调试日志还显示onConnected函数被调用了:
03-30 12:23:28.847: I/Locationing_GpsService(4936): Google_Api_Client:connected.
03-30 12:23:28.857: I/Locationing_GpsService(4936): initLocationRequest:initing_now
03-30 12:23:28.877: I/Locationing_GpsService(4936): initLocationRequest:interval_5000
03-30 12:23:28.897: I/Locationing_GpsService(4936): initLocationRequest:priority_100
03-30 12:23:28.917: I/Locationing_GpsService(4936): Starting_location_listening:now
在此之后,我遇到了异常。
我错过了什么吗?我收到了“已连接”的响应,运行了我的函数,然后出现了“未连接”的错误。这是怎么回事?还有一件很烦人的事情是:我现在已经使用这个位置服务几周了,从来没有遇到过这个错误。
编辑:
我添加了更具体的日志输出,真是让我大吃一惊,看看这个:
@Override
public void onConnected(Bundle bundle) {
if(mGoogleApiClient.isConnected()){
log("Google_Api_Client: It was connected on (onConnected) function, working as it should.");
}
else{
log("Google_Api_Client: It was NOT connected on (onConnected) function, It is definetly bugged.");
}
initLocationRequest();
startLocationListening();
}
在这种情况下的日志输出:
03-30 16:20:00.950: I/Locationing_GpsService(16608): Google_Api_Client:connected.
03-30 16:20:00.960: I/Locationing_GpsService(16608): Google_Api_Client: It was NOT connected on (onConnected) function, It is definetly bugged.
我在onConnected()
方法中得到了mGoogleApiClient.isConnected() == false
,这怎么可能?
编辑:
由于即使通过声望奖励也没有人能够回答这个问题,我决定向谷歌报告此问题。 谷歌官方对我的报告的回答真的让我感到惊讶。
“此网站是用于开发人员使用AOSP Android源代码和开发工具集的开发人员问题,并且不适用于Play服务、GMS或Google API等Google应用程序或服务。很遗憾,似乎没有合适的地方可以报告Play服务的错误。我只能说,这个网站不是,抱歉。请尝试在Google产品论坛上发布。”
完整问题报告在此。(希望他们不会因为这件事而将其删除)
所以,我在谷歌产品论坛上看了一下,但无法找到任何主题来发布此事,所以目前我感到困惑和受阻。
有地球上的任何人能帮助我吗?
编辑:
完整代码在pastebin中。