【发布时间】:2015-06-03 08:00:10
【问题描述】:
所以我发现了一些关于 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
在这之后我得到了异常。
我在这里遗漏了什么吗?我得到了“已连接”的响应我运行了我的 func,但我得到了错误“未连接”这是什么?另外一件烦人的事情是:我使用这个定位服务已经好几个星期了,从来没有出现过这个错误。
编辑:
我添加了一个更具体的日志输出,只是让我大吃一惊,看看这个:
@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,这怎么可能?
编辑:
由于即使有声誉赏金也没有人能回答这个问题,我决定将此作为一个错误报告给 Google。接下来发生的事情让我感到非常惊讶。 谷歌对我的报告的官方回答:
“此网站用于解决 AOSP Android 源代码的开发者问题 代码和开发人员工具集,而不是 Google 应用或服务,例如 播放服务、GMS 或 Google API。不幸的是,似乎没有 是报告 Play 服务错误的合适场所。尽我所能 说的是这个网站不是它,对不起。尝试在 Google 上发帖 而是产品论坛。 "
完整的问题report here.(我希望他们不会因为它很傻而删除它)
所以是的,我查看了 Google 产品论坛,但找不到任何主题可以发布此内容,所以目前我感到困惑和卡住。
地球上有人可以帮我解决这个问题吗?
编辑:
完整代码在pastebin
【问题讨论】:
-
您能否在 onConnectionSuspended() 中添加一些日志记录,以查看是否随时调用?
-
@AntiVeeranna 我在 onConnectionSuspended() 上也有记录,因为该函数从未被调用过。
-
您能否也发布创建 mGoogleApiClient 的代码?
-
@AntiVeeranna 在 pastebin 中添加代码
-
@Dima 正如 iheanyi 所说:“尝试将您的 googleApiClient 创建移动到 onCreate,看看您是否得到相同的行为。”它确保只有一个实例会从 googleApiClient 生成,并且您的服务将不再持有错误的引用。
标签: android google-play-services google-api-client android-googleapiclient