Advanced location provider - onLocationChanged stops calling

In our application, we used the Fused Location Provider (FLP) provider to track location. We noticed that sometimes an application can get into a state when a callback request for a location ceases to be called. This application is mainly used on two tablets (nexus 7 and LG G-pad 8.3). We saw that the problem occurs on both devices. Typically, resetting the device seems to alleviate the problem. We believe that we adhere to most of the best practices for using FLP. But just in case, we've put together this sample code that illustrates how we use FLP.

Get the google api client and call connect:

m_googleApiClient = builder .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .addApi(LocationServices.API) .build(); m_googleApiClient.connect() 

After connecting, we start listening to location callbacks:

 @Override public void onConnected(Bundle bundle) { m_fusedLocationProviderApi.requestLocationUpdates(m_googleApiClient, m_locationRequest, this); } 

The location request is as follows:

  LocationRequest locationRequest = LocationRequest.create(); locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); locationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS); locationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS); 

Then we implement the callback as follows:

  @Override public void onLocationChanged(Location location) { // set location member var and record timestamp. } 

When problems arise, we do not receive any callbacks. We also tried calling reset () on the google api client when we find that we have not received GPS for a long time. We also control the availability of the location using a timer every 2 seconds (using getLocationAvailability). As a rule, we find that having a user reset their device (turning it on, turning it off) fixes the problem.

Our questions:

  • Has anyone else noticed this problem with FLP?
  • Is there anything else you can do to solve the problem without having user reset? Would you delete location update help?
  • Is there more information we can / should collect to diagnose the problem?
+7
android google-play-services fusedlocationproviderapi gps location-services
source share
2 answers

I worked a lot with FusedLocationProvider on a wide range of devices, both in low-end and high-end, and I saw similar problems when GPS data had bad areas between . Because of this, I made a few changes related to these reliability and reliability issues (as indicated below), and did not see these problems after that.

In my experience, possible solutions to such problems:

  • Play Services Update: I used versions of Android Play services in the range 7.x - 8.x when I had these problems.

    Sol: In the recent past, updating permissions on version 10.x helped solve problems. We have not seen that these issues are published by the update, so this could definitely be one of the reasons in your case.

  • Ensuring reliable and reliable operation of the Android service: This is one of the main reasons why we observed a power outage in the GPS data, and when you restart the device or initialize the installation, we usually restart the service. And therefore, starting the service again can resume updating the location to normal.

    Sol: I use the Foreground service instead of the background service, and this is by far the most reliable solution here, but this is due to the sticky user interface notification for the user. If not for this, then it might also be a good option to test the health (start or kill) for the background service.

  • Reconnecting GoogleAPIClient to the connection / connection error I have seen situations where the GoogleAPIClient connection is suspended or returns an error due to poor network conditions or other reasons. Although I could not confirm the direct effect of this on the loss of location data, this could very well be the reason in this case. To support this, resetting location updates will fail if this problem is related to GoogleAPIClient, and this is what you noticed.

    Sol: Just reconnecting the GoogleAPIClient when paused or failed can solve this problem.

In addition, there is another reason that may be responsible for this problem:

GPS device fix lost: There are times when a device loses its GPS device, which leads to the lack of GPS data until the GPS fix is โ€‹โ€‹restored. There is absolutely no way to determine if a place was lost due to a GPS fix. Resetting the deviceโ€™s location affects the device trying to fix the GPS, which may also be the reason here.

DISCLAIMER . I am an Android @ HyperTrack developer, and we are creating a location stack for developers who create their location functions in their applications, and these are the problems that we solve. It would be great if you tested similar scenarios using our SDK , and if you still see the problem, we will be happy to help you achieve your root cause and fix the problem.

+3
source share

You can try setting LocationSettings as follows:

  request = LocationRequest.create(); LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder().addLocationRequest(request); builder.setAlwaysShow(true); PendingResult<LocationSettingsResult> result = LocationServices.SettingsApi.checkLocationSettings(apiClient, builder.build()); result.setResultCallback(new ResultCallback<LocationSettingsResult>() { @Override public void onResult(@NonNull LocationSettingsResult result) { final Status status = result.getStatus(); switch (status.getStatusCode()) { case LocationSettingsStatusCodes.SUCCESS: LocationServices.FusedLocationApi.requestLocationUpdates(apiClient, request, locationListener); break; case LocationSettingsStatusCodes.RESOLUTION_REQUIRED: try { status.startResolutionForResult(context, REQUEST_CODE); } catch (IntentSender.SendIntentException e) {} break; case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE: break; } } }); 

then process the settings request in onActivityResult

 switch (requestCode) { case REQUEST_CODE: switch (resultCode) { case Activity.RESULT_OK: // All required changes were successfully made LocationServices.FusedLocationApi.requestLocationUpdates(apiClient, request, locationListener); break; case Activity.RESULT_CANCELED: // The user was asked to change settings, but chose not to break; default: break; } break; } 

If this does not help, try changing the version of google-play-services.

-2
source share

All Articles