Wednesday, October 17, 2018

Android location manager error 'Exiting with error onLocationChanged line 152 “1”'

1 comment

I have a location manager in my Android app that sometimes works. Sometimes I'll run the app and it will get location updates (with some errors). Sometimes I'll run it and it will just throw this error every couple seconds without receiving any location updates:

E/IzatSvc_PassiveLocListener: Exiting with error onLocationChanged line 152 "1" 

Here is my class for managing location events:

package com.company.AppName;  import android.app.job.JobParameters; import android.app.job.JobService; import android.content.Context; import android.content.Intent; import android.location.Criteria; import android.location.Location; import android.location.LocationManager; import android.os.Bundle; import android.util.Log;  public class LocationListenerService extends JobService {   private static final String TAG = "LocationListenerService";   private LocationManager locationManager = null;   private LocationListener locationListener = null;   private String locationProvider = null;    public LocationListenerService() {}    @Override   public int onStartCommand(Intent intent, int flags, int startId) {     return START_STICKY;   }    @Override   public boolean onStartJob(JobParameters params) {     Log.i(TAG, "onStartJob");     startLocationManager(params);     return true;   }    @Override   public boolean onStopJob(JobParameters params) {     Log.i(TAG, "onStopJob");     return false;   }    public void startLocationManager(JobParameters params) {     if(locationManager != null) return;      Criteria criteria = new Criteria();     criteria.setAccuracy(Criteria.ACCURACY_FINE);   //    criteria.setPowerRequirement(Criteria.POWER_LOW);     criteria.setAltitudeRequired(false);     criteria.setBearingRequired(false);      locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);     locationProvider = locationManager.getBestProvider(criteria, true);     locationListener = new LocationListener();      if (locationProvider != null) {       Log.v(TAG, "Location provider: " + locationProvider);     } else {       Log.e(TAG, "Location provider is null. Location events will not work.");       return;     }      if (locationListener == null) {       Log.e(TAG, "Location listener is null. Location events will not work.");       return;     }      // Finish job after first time updating location with the server     NativeApp.shared().getLocationData((NativeApp app, String response) -> {       Log.i(TAG, "Received location data response. Finishing job.");       jobFinished(params, true);     });      try {       locationManager.requestLocationUpdates(locationProvider, 0, 0, locationListener);     } catch (java.lang.SecurityException ex) {       Log.e(TAG, "fail to request location update, ignore", ex);     } catch (IllegalArgumentException ex) {       Log.e(TAG, "network provider does not exist, " + ex.getMessage());     }   }    private class LocationListener implements android.location.LocationListener {     @Override     public void onLocationChanged(Location location) {       if(location == null) {         Log.w(TAG, "onLocationChanged skipped: null location");         return;       }       Log.i(TAG, "onLocationChanged: " + location.toString());       NativeApp.shared().updateLocation(location);     }      @Override     public void onProviderDisabled(String provider) {       Log.i(TAG, "onProviderDisabled: " + provider);     }      @Override     public void onProviderEnabled(String provider) {       Log.i(TAG, "onProviderEnabled: " + provider);     }      @Override     public void onStatusChanged(String provider, int status, Bundle extras) {       Log.i(TAG, "onStatusChanged: " + provider);     }   } } 

Why is this happening? Why do location updates work sometimes but not other times?

EDIT: After giving up for a few hours and rerunning, the app is still throwing the error repeatedly, but after about 10 seconds logs this and starts receiving location updates:

E/XTCC-6.1.2.10: [FDAL_OSListener] handleLocationUpdate:  failed: 2 D/LocationManagerService: incoming location: gps I/LgeGnssLocationProvider: Intent - android.location.GPS_FIX_CHANGE D/LgeGnssLocationProvider: GPS_FIX_CHANGE_ACTION! , mGpsNavigating =true D/LocationManagerService: incoming location: gps 

2 Answers

Answers 1

long milliseconds = 5000; // 5 seconds float minimusDistance = 5.5; // 5.5m distance from current location locationManager.requestLocationUpdates(locationProvider, milliseconds, minimusDistance, locationListener); 

Try this snippet. Hope it will solve your problem.

This is happening only, for this reason, you're not providing a minimum interval and minimum distance to get location updates. That's why you're getting this error.

Here is the link check it out - requestLocationUpdates

Answers 2

This location update gets varies as per the android version. Recently Android introduce background location limits in oreo which says:

While your app is in the foreground, you should receive location updates as frequently as you requested. When your app goes in the background, your app will receive location updates only a few times each hour (the location update interval may be adjusted in the future based on system impact and feedback from developers).

Android recommend to use fused location APIs for below reason, you must consider it for your scenario which says:

If your app needs access to location history that contains time-frequent updates, use the batched version of the Fused Location Provider API elements, such as the FusedLocationProviderApi interface. When your app is running in the background, this API receives the user's location more frequently than the non-batched API. Keep in mind, however, that your app still receives updates in batches only a few times each hour.

Please refer below links for and better approach:

https://github.com/googlesamples/android-play-location/tree/master/LocationUpdatesForegroundService/app

https://codelabs.developers.google.com/codelabs/background-location-updates-android-o/index.html#0

Note: The "O" background location limits only kick in when your app is no longer in the foreground.

Please let me know if above two links not working for you.

If You Enjoyed This, Take 5 Seconds To Share It

1 comment: