Is there any way to specify the time intervals that the Location Manager broadcasts the current location?
I am using a method called startListening:
public void startListening() {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
0,
0,
this
);
}
Thanks
public void requestLocationUpdates (String provider, long minTime, float minDistance, LocationListener listener, Looper looper)
Registers the current activity to be notified periodically by the named provider. Periodically, the supplied LocationListener will be called with the current Location or with status updates.
It may take a while to receive the most recent location. If an immediate location is required, applications may use the getLastKnownLocation(String) method.
In case the provider is disabled by the user, updates will stop, and the onProviderDisabled(String) method will be called. As soon as the provider is enabled again, the onProviderEnabled(String) method will be called and location updates will start again.
The frequency of notification may be controlled using the minTime and minDistance parameters. If minTime is greater than 0, the LocationManager could potentially rest for minTime milliseconds between location updates to conserve power. If minDistance is greater than 0, a location will only be broadcasted if the device moves by minDistance meters. To obtain notifications as frequently as possible, set both parameters to 0.
Background services should be careful about setting a sufficiently high minTime so that the device doesn't consume too much power by keeping the GPS or wireless radios on all the time. In particular, values under 60000ms are not recommended.
The supplied Looper is used to implement the callback mechanism.
Parameters
provider the name of the provider
with which to register
minTime the
minimum time interval for
notifications, in milliseconds. This
field is only used as a hint to
conserve power, and actual time
between location updates may be
greater or lesser than this value.
minDistance the minimum distance
interval for notifications, in meters
listener a whose onLocationChanged(Location)
method will be called for each
location update
looper a Looper
object whose message queue will be
used to implement the callback
mechanism.
Throws
IllegalArgumentException if provider is null or doesn't exist
IllegalArgumentException if listener is null
IllegalArgumentException if looper is null
SecurityException if no suitable permission is present for the provider.
minTime : the minimum time interval for notifications, in milliseconds. This field is only used as a hint to conserve power, and actual time between location updates may be greater or lesser than this value.
minDistance: the minimum distance interval for notifications, in meters
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 9000, 500,locListener);
// 60000 Is 1 Minute and 100 is KM
Above you have 9000 (Nine Seconds) and 500 which is if the location has moved more than 500KM
Related
I have an operational Android app which reports users location within a background service. I want to integrate a feature which will notify the user when GPS signal has been lost.
Our current implementation to commence location updates is:
mLocationListener = LocationListener(LocationManager.GPS_PROVIDER, gpsDeviceCallback)
if (handlerThread?.isAlive == true) {
handlerThread?.quit()
}
handlerThread = HandlerThread("GpsLocationHandler")
handlerThread!!.start()
mLocationManager?.requestLocationUpdates(
LocationManager.GPS_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE.toFloat(),
mLocationListener, handlerThread!!.looper)
with LOCATION_INTERVAL set to 1000 ms and LOCATION_DISTANCE set to 10m.
We get the expected onLocationChanged callbacks under normal operation. However, I would appreciate advice on how to detect a situation where there is a loss of GPS or handset cannot obtain an adequate GPS signal.
We have implemented a solution where we run a time task and determine if onLocationChanged is called during the timer period. The problem with this solution is that if the user handset is stationary during this time then no onLocationChanged callbacks will happen. So this approach will not work as a means of detecting no\inadequate GPS signal.
The user of onStatusChanged as a method of LocationListener is now deprecated so this is not an option either.
Is there some standard solution to this problem ? Perhaps some method which could be called in the case where no onLocationChanged callbacks happen when our check timer expires to test current GPS status?
Thanks!
you can check the current connected satellites and approximate the accuracy see :https://stackoverflow.com/a/10589949
I have an app that tracks user location continously. Now, this works as follows:
I have foreground service that schedules repeating alarm via AlarmManager. Alarm Receiver in onReceive method starts service again. Inside service Im receiving current location from Locator (singleton class that gets location form GP Services). Alarm manager fires alarm once in a 12 minutes, and I want to set repeating time to 1 minute or less. But i`m aware of too frequenlty waking up device - I think it will consump battery very fast.
This foreground service works always.
Are there any other, better way to track location continously?
You can use LocationListener.
google location listner
Checkout answer on this thread: Getting location
In above answer, while creating object for LocationRequest call these two methods, setFastestInterval(long interval) and setInterval(long interval) which will fetch location after some interval.
I'm using Android's LocationManager and its method requestLocationUpdates like this:
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 10, this);
As I found out both the conditions of minTime and minDistance have to be met for location update, but I need to get update every 3 seconds or 10 meters.
I tried to put 2 requests like this:
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 0, this);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 10, this);
but it doesn't work, I only get update every 10 meters with this code, not every 3 seconds.
I'm working with API19 if that matters.
The Documentation on requestLocationUpdate() says :
requestLocationUpdates(String provider, long minTime, float
minDistance, LocationListener listener)
Register for location updates using the named provider, and a pending intent.
So you should be calling it like locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 10, this);
But If you set minTime to 0, it will be called once when it first receives a location update, then it won't be called until you change your position in minDistance meters.
Documentation Link for Reference
EDIT
As per the Discussion with #Matej I need to get update every 10 meters even if it happened in less than 3 seconds, and update every 3 seconds even if the location didn't change by more than 10 meters
If you want to regularly requestLocationUpdates, you should use Timer and TimerTask and have requestLocationUpdates run once every 3 seconds
schedule(TimerTask task, long delay, long period)
Schedules the specified task for repeated fixed-delay execution,
beginning after the specified delay.
I read this discussion for a similar problem.
I had to update location every second, but setting:
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, this);
I didn't achieve my goal.
In the official documentation I read that also if you fix a timerate for updates, it could be not keeped so rigid as setted. (I can't find anymore the developper page which talk about).
To have a fixed and sure timerate for updates I found this post.
My problem was that Location updates went to update (by observers) the activity and I got error using Timer (as suggested).
the error was:
Only the original thread that created a view hierarchy can touch its views.
For anyone has similar problem I suggest to use Handler:
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
notifyLocationObservers();
}
}, 1000);
I'm using the following method (taken from the android beacon library)
public void didRangeBeaconsInRegion(final Collection<Beacon> collection, Region region)
This callback method "detects" beacons every 1.1 seconds. To be more precise, it fires every 1.1 seconds. My beacon device (Kontakt i.o). Sends packets every 200ms. I've found out that there isn't a way to somehow decrease the firing interval of the above method so my question is, is there an alternative for this method so that I can do my distance calculations more often.
Basically I'm providing the RSSI and TxPower as arguments and doing some more calculation to make the distancing more accurate so I would like to get my RSSI every 200ms not every second...
Create an instance of the BeaconManager, you can set a couple of methods regarding the scanning:
BeaconManager manager = BeaconManager.getInstanceForApplication(context);
manager.setBackgroundScanPeriod(milliseconds); //defaults to 10
manager.setBackgroundBetweenScanPeriod(miliseconds); //Period between scans
manager.setForegroundBetweenScanPeriod(miliseconds); //Period between scans
manager.setForegroundScanPeriod(milliseconds); //defaults to 1.1
You can find some more methods when you look at the BeaconManager source.
I want to get my coordinates every minute, even if a user is not moving. So i used requestLocationUpdates with the following parameters:
locMgr.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
60000, // minTime in ms
0, // minDistance in meters
PosljiLokacijo.this);
But when i test it on my HTC Hero (2.1 Android) and i am not moving, onLocationChanged() is never called. If i change minDistance to 1, then i get a few updates at start but after that, they come in very unregular intervals.
How should i choose these 2 parameters (minTime and minDistance) to receive coordinates every minute?
I don't think "minTime" or "minDistance" will help in this case, unless: save the last location into variables and output them every minute. Let the locationManager overwrite those variables so after a minute, the location change becomes visible. That way there's no need to wait for an update as your variables always hold the actual position.
You can also retrieve the location from the locationManager and request the location again after one minute (or whatever you'd like your interval to be) without using requestLocationUpdates.
e.g. take a look at the Timer to set a repeated call to your method that gets the location:
http://developer.android.com/reference/java/util/Timer.html
That way you should also save battery as the system won't be looking for updates all the time.
According to the documentation:
minTime the minimum time interval for notifications, in milliseconds. This field is only used as a hint to conserve power, and actual time between location updates may be greater or lesser than this value.
The only way I see to do what you want would be to set minTime to 0 and throw out values that are too frequent. You'd still not be guaranteed to get updates as often as you'd like, and be prepared for your app to be a battery killer as well.