简体   繁体   English

Android位置API失败

[英]Android location API fails

I'm using the Google Play location API pretty much as in the tutorial . 我在本教程中使用的几乎是Google Play位置API。 But it seems to fail in some situations. 但是在某些情况下它似乎失败了。 Let me explain: 让我解释:

First we create a client and connect to it: 首先,我们创建一个客户端并连接到它:

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

Then when connected, we request location updates passing a pending intent: 然后,在连接后,我们请求通过未决意图的位置更新:

@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);
}

After 1min of location being updated we stop it by calling removeLocationUpdates(). 在更新位置1分钟后,我们通过调用removeLocationUpdates()将其停止。

And we call this whole piece of code (client connection, requesting update) quite regularly, several times per hour. 而且我们非常定期地,每小时几次调用整个代码(客户端连接,请求更新)。

This works at first and for some time, but on some devices it fails after a while: LocationClient gets connected and onConnected() called, but then the pendingIntent is not fired anymore for some reason! 这项功能一开始会工作一段时间,但在某些设备上会在一段时间后失败:LocationClient已连接并调用了onConnected(),但是由于某种原因,不再触发未决的Intent!

It seems to fail only on Android 4.1.2. 它似乎仅在Android 4.1.2上失败。 I haven't seen the problem on Android 4.2 or 4.3. 我尚未在Android 4.2或4.3上看到此问题。

I really don't know how to handle this bug, any help or suggestion is welcome! 我真的不知道该如何处理该错误,欢迎任何帮助或建议!

EDIT: It fails only when WiFi is disabled. 编辑:仅当禁用WiFi时,它才会失败。 If you enable WiFi and that it connects, then the location update works again. 如果启用WiFi并已连接,则位置更新将再次起作用。 It seems that location service requires the WiFi to update the location, even if the 3G is on and connected... But the WiFi shouldn't be necessary! 即使3G已开启并已连接,定位服务似乎也需要WiFi更新位置...但是,WiFi并非必需!

如果您追求precision_fine的位置,则将使用gps,并且不需要任何网络间连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM