Android位置API失败

时间:2013-08-28 15:18:16

标签: android geolocation google-play-services

我正在使用Google Play位置API,就像在tutorial中一样。 但在某些情况下似乎失败了。让我解释一下:

首先我们创建一个客户端并连接到它:

    mLocationClient = new LocationClient(mContext, this, this);
    mLocationClient.connect();

然后在连接时,我们请求通过待处理意图的位置更新:

@Override
public void onConnected(Bundle arg0) {
    LocationRequest locationRequest = LocationRequest.create().setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY).setInterval(UPDATE_INTERVAL).setFastestInterval(FASTEST_INTERVAL);
    mPendingIntentLocationUpdated = PendingIntent.getService(mContext, 0, (new Intent(mContext, MyService.class)).setAction(MyService.ACTION_LOCATION_UPDATED), 0);
    mLocationClient.requestLocationUpdates(locationRequest, mPendingIntentLocationUpdated);
}

在更新位置1分钟后,我们通过调用removeLocationUpdates()来停止它。

我们会定期调用整段代码(客户端连接,请求更新),每小时几次。

这在一段时间内有效,但在某些设备上它会在一段时间后失败:LocationClient连接并调用onConnected(),但由于某种原因,pendingIntent不再被触发!

它似乎仅在Android 4.1.2上失败。我没有在Android 4.2或4.3上看到过这个问题。

我真的不知道如何处理这个bug,欢迎任何帮助或建议!

编辑:仅在禁用WiFi时失败。如果启用WiFi并且已连接,则位置更新将再次起作用。似乎位置服务需要WiFi来更新位置,即使3G已打开并连接......但是WiFi不应该是必需的!

1 个答案:

答案 0 :(得分:1)

如果您选择accuracy_fine,则会使用gps并且您不需要任何互联网连接。