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
Device/version info:
The device is an LG G6 (LG-H872) running Android 8.0.0
And here is the contents of my AndroidManifest.xml file:
<?xml version='1.0' encoding='utf-8'?>
<manifest android:hardwareAccelerated="true" android:versionCode="10000" android:versionName="1.0.0" package="com.company.AppName" xmlns:android="http://schemas.android.com/apk/res/android">
<supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:resizeable="true" android:smallScreens="true" android:xlargeScreens="true" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application android:hardwareAccelerated="true" android:icon="@mipmap/icon" android:label="@string/app_name" android:supportsRtl="true">
<activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/activity_name" android:launchMode="singleTop" android:name="MainActivity" android:screenOrientation="portrait" android:theme="@android:style/Theme.DeviceDefault.NoActionBar" android:windowSoftInputMode="adjustResize">
<intent-filter android:label="@string/launcher_name">
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider android:authorities="${applicationId}.provider" android:exported="false" android:grantUriPermissions="true" android:name="org.apache.cordova.camera.FileProvider">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/camera_provider_paths" />
</provider>
<service android:enabled="true" android:name="com.company.AppName.LocationListenerService" android:permission="android.permission.BIND_JOB_SERVICE" />
<service android:exported="false" android:name="com.company.AppName.AppFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
</application>
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="26" />
</manifest>
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
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 love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With