简体   繁体   中英

Android location API fails

I'm using the Google Play location API pretty much as in the tutorial . 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().

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!

It seems to fail only on Android 4.1.2. I haven't seen the problem on Android 4.2 or 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. If you enable WiFi and that it connects, then the location update works again. 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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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