4

Im making a real time location listening app and i need to call location updates in every 3 seconds.

I use Fused location.

My interval is set to 3 seconds, however it looks like the minumum value for this interval is 5 seconds.

Code:

public class GpsService extends Service implements ConnectionCallbacks, OnConnectionFailedListener, LocationListener {

private static int UPDATE_INTERVAL = 1000 * 3; // /< location update interval
private static int FASTEST_INTERVAL = 1000 * 3; // /< fastest location update interval
private static int DISPLACE_METERS = 10; // displacement in meters, not used atm
private LocationRequest mLocationRequest;
private LocationWrapper mLastLocation;

...


mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(UPDATE_INTERVAL);
mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

...

@Override
public void onLocationChanged(Location location) {

    mLastLocation = new LocationWrapper(location);
    log("onLocationChanged" + " " + mLastLocation.toString());

}

Logcat:

03-11 14:35:41.559: I/Locationing_GpsService(7410): Lat: 47.513878
03-11 14:35:41.559: I/Locationing_GpsService(7410): Lng: 19.0362011
03-11 14:35:41.559: I/Locationing_GpsService(7410): Accuracy: 21.213 meters
03-11 14:35:41.559: I/Locationing_GpsService(7410): ===================
03-11 14:35:46.554: I/Locationing_GpsService(7410): Lat: 47.5138783
03-11 14:35:46.554: I/Locationing_GpsService(7410): Lng: 19.0362064
03-11 14:35:46.554: I/Locationing_GpsService(7410): Accuracy: 17.32 meters
03-11 14:35:46.554: I/Locationing_GpsService(7410): ===================
03-11 14:35:51.569: I/Locationing_GpsService(7410): Lat: 47.5138752
03-11 14:35:51.569: I/Locationing_GpsService(7410): Lng: 19.0362068
03-11 14:35:51.569: I/Locationing_GpsService(7410): Accuracy: 15.0 meters

You can see the logcat's date, it is in 5 seconds delay: 41, 46, 51 I have read Google's doc about Fused location but i haven't find anything about a minimal value of interval.

Any suggestions?

E D I T:

Added getTime() to log, and parsed it to a Date object, you can see the 5 second delay more clearly.

03-11 15:16:01.851: I/Locationing_GpsService(16747): Lat: 47.5138798
03-11 15:16:01.851: I/Locationing_GpsService(16747): Lng: 19.0362125
03-11 15:16:01.851: I/Locationing_GpsService(16747): Accuracy: 28.713 meters
03-11 15:16:01.851: I/Locationing_GpsService(16747): Time: Wed Mar 11 15:16:01 CET 2015   
03-11 15:16:01.851: I/Locationing_GpsService(16747): =========================================================
03-11 15:16:06.946: I/Locationing_GpsService(16747): Lat: 47.5138765
03-11 15:16:06.946: I/Locationing_GpsService(16747): Lng: 19.0361987
03-11 15:16:06.946: I/Locationing_GpsService(16747): Accuracy: 20.902 meters
03-11 15:16:06.946: I/Locationing_GpsService(16747): Time: Wed Mar 11 15:16:06 CET 2015   
03-11 15:16:06.946: I/Locationing_GpsService(16747): =========================================================
03-11 15:16:11.971: I/Locationing_GpsService(16747): Lat: 47.5138767
03-11 15:16:11.971: I/Locationing_GpsService(16747): Lng: 19.0361916
03-11 15:16:11.971: I/Locationing_GpsService(16747): Accuracy: 17.326 meters
03-11 15:16:11.971: I/Locationing_GpsService(16747): Time: Wed Mar 11 15:16:11 CET 2015   
03-11 15:16:11.971: I/Locationing_GpsService(16747): =========================================================

E D I T 2:

Permissions:

 <uses-permission android:name="android.permission.INTERNET" />
 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Adam Varhegyi
  • 11,307
  • 33
  • 124
  • 222

1 Answers1

6

Running your code in an Activity with the interval set to 0 produces updates at 1 per second on a Nexus 4, 5 and 7... just like using the LocationManager. You may be limited by your devices hardware. Have you just tried requesting the fastest update interval using LocationRequest and LocationManager? If they are both being limited to 5 seconds, I would suspect it is something device specific with the hardware.

Location Request:

    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(0);
    mLocationRequest.setFastestInterval(0);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

Logcat:

03-11 09:50:26.931: D/MainActivity(5623): onLocationChanged Location[fused 0,0 acc=12 et=+1d17h57m7s979ms alt=1505.0 vel=0.0]
03-11 09:50:27.514: D/MainActivity(5623): onLocationChanged Location[fused 0,0, acc=12 et=+1d17h57m8s571ms alt=1505.0 vel=0.0]

Edit: The Docs also tell you that setInterval(long) can have updates at a slower or faster rate that specified.

John
  • 460
  • 1
  • 5
  • 18
Kaleb
  • 1,855
  • 1
  • 18
  • 24
  • Tested on an other device, and it works. Looks like something called hardware restrictions are actually exists. – Adam Varhegyi Mar 17 '15 at 11:30
  • I don't think so you can set `setFasterstInterval(0)` because this will send you the location updates at every instant and you may encounter problems with UI flicker or data overflow. So set this value to a rate greater than the rate in which your app can update its UI. – CopsOnRoad Mar 17 '17 at 15:00
  • i am using fused location provider, when my app is in foreground i am getting frequent location update but same code in background i am getting less location update, please help me.. – Gaurav Singhal Oct 22 '18 at 09:32