Better way to query GPS regularly - android

I have a timer that runs every second. Every second I get the GPS location and do other stuffs.
I am wondering which way is better:
1- Request a single location update and then get the last known location
private void timeout(){
String data[] =new String[DATA_LENGTH];
locationManager.requestSingleUpdate(LocationManager.GPS_PROVIDER, this, null);
Location loc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
.
.
.
}
2- Start Location listener and then just get the last known location whenever my timer expire
OnCreate(){
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
}
private void timeout(){
String data[] =new String[DATA_LENGTH];
Location loc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
.
.
.
}
Thank you
PS: Note that battery is not a concern to me as per the requirement of the product

requestSingleUpdate is meant to be single, if you need to query the GPS frequently you should definitely go with option 2.
Keep a global Location object in memory, use it in you other stuff and update it whenever your listener gets an update from the LocationManager.

You can listen for changes via requestLocationUpdates - the code below is a quick-n-dirty example (untested). Remember, you have to have location services turned on to use this.
LocationListener locGPSListener= new LocationListener() {...}
LocationListener locNetworkListener= new LocationListener() {...}
mgr = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
// listens using GPS for location
mgr .requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locGPSListener);
// uses towers for location
mgr .requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locNetworkListener);
...

What approach is better, depends on
Androids GPS behaviour and
your Application.
ad 1. if explicitly getting a location delivers a more recent fix, than this is an advantage, because:
ad 2. if your application don't want the android filtering behaviour, and you can filter it yourself better, then this would be better for your app.
Example: (is for ios, but may apply here too:) if I drive with my car to a traffic signal, and do a harsh breaking, then ios still shows 5 km/h speed, although I am standing still. This I call unwanted filtering.
This has all nothing to do with battery: if you get the location via message or if you query it is the same from battery point of view. It smore a software design issue: (events vs. polling)
A difference would only be if GPS is disabled, but disabling GPS makes only sense if it can be disabled for long time.

Related

onProviderDisabled - need gps in battery saving mode

onProviderDisabled is telling me the gps is disabled. I want to be able to get the lat and lon in the wifi gps mode as well as the full gps mode.
I am using this code to start up
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
3000,
1, this);
It all works with the full GPS on but I need to be able to get the lat and lon in either mode.
You can request updates from more than one provider. Add the following line:
locationManager.requestLocationUpdates(LocationManager. NETWORK_PROVIDER, 3000, 1, this);
The same callback methods, i.e. onLocationChanged(), onProviderDisabled(), etc., will be used to pass the updates from both providers. If you need to know which provider is passing the update, you can use the Location.getProvider() method in onLocationChanged(), and the String provider parameter passed in the others.

requestLocationUpdates vs Location Listener

What is the difference between using a location manager object with requestLocationUpdates() vs just using a LocationListener? do they both do the same thing and work the same way? Whats the advantage of one over the other?
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_provicer, 0, 0, new LocationListener()){
//all appropriate methods here
}
VS.
LocationListener myLocationListener = New LocationListener(){
// all appropriate methods here
}
Your question is technically incorrect. LocationManager registers a listener via requestLocationUpdates. The listener is the callback when your location manager realizes that the timeout OR distance travel has occurred. In your case the location listener will be called immediately.
But beware, location manager request updates with 0,0 parameters are not known to be instantaneous. Sometimes there may be a delay.
So your question on what is the difference is incorrect. Its like asking what is the difference between Print this and System.out.println() :)

GPS Mock - I only can mock one time the location

I have a service to mock GPS location doing a loop between a list of coordinates, but the method onLocationChanged() only detects the first mock of the GPS. I mean, if I launch one GPS program it only detects the first call of my service, doesn't mind if its the first location of the list or the last, just detect the first call it make.
I'm sure that I'm making the calls to change the coordinates in my services because I use a Toast message, but doesn't work..
Mi code.
First I set up the LocationManager
ls = new MyLocationListener();
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
lm.addTestProvider(Context.LOCATION_SERVICE, false, false,
false, false, true, true, true, 0, 5);
lm.requestLocationUpdates(Context.LOCATION_SERVICE, 0, 0, ls);
lm.setTestProviderEnabled(Context.LOCATION_SERVICE, true);
MyLocationListener only has a show message in the method onLocationChanged().
Then with a timer I call periodically the function to mock GPS.
private void setMockLocation(double latitude, double longitude, float accuracy) {
Location newLocation = new Location(Context.LOCATION_SERVICE);
newLocation.setLatitude(latitude);
newLocation.setLongitude(longitude);
newLocation.setAccuracy(accuracy);
newLocation.setTime(System.currentTimeMillis());
lm.setTestProviderEnabled(mocLocationProvider, true);
lm.setTestProviderStatus(Context.LOCATION_SERVICE,
LocationProvider.AVAILABLE,
null,System.currentTimeMillis());
lm.setTestProviderLocation(Context.LOCATION_SERVICE, newLocation);
}
But it only works the first time called, not the next calls.
EDIT
After a few investigating work, I know the code works into 2.2 and 4.2. The version that has the device I need to work on it is 4.0. I don't know why, but doesn't run in that version. Any ideas?
It's late to answer this but if anyone to have the same bug in the future, here how I solved this.
I had the same issue but after setting 'setElapsedRealtimeNanos', it worked for me.
newLocation.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos());

How to update gps location frequently?

I am writing an application that shows your location in a google map...
so far, so good... It does shows my location correctly
The problem is that this gps is not updated if I am moving (or it does, but only after some time)
Does anybody know how to do this in a way like the native google maps (for android) does?
This is, if you click on 'my location' it shows a flashing blue point that changes as you move...
This is my code:
//Initializing the listeners
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 35000, 10, networkLocationListener);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 35000, 10, gpsLocationListener);
//and now, in both 'networkLocationListener' and 'gpsLocationListener'
//I overwrite the method 'onLocationChanged':
#Override
public void onLocationChanged(Location location) {
Log.i("test", "New network location: "+location.getLatitude()+
" , "+location.getLongitude());
myLongitude= location.getLongitude();
myLatitude= location.getLatitude();
}
//and the variables myLongitude and myLatitude are the only ones that I need to display my
//position in the map...
Does anybody knows if I am missing something?
The method call you are using is requestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener), and you're telling it to only give you updates every 35000ms or every 35 seconds. Try lowering that number to whatever suits you. A lower number will mean more battery usage though.
If you want an indicator on a MapView, look into MyLocationOverlay.
You wouldn't want to leave it like this forever, but it might be worth setting the minTime and minDistance values to zero if you haven't given that a shot yet.
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, gpsLocationListener);
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.
-requestLocationUpdates Reference

Android requestLocationUpdates when phone idle

I'd like to track my Location every minute. For that I use a locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 60000, 0, pll);
Here is the log when my phone is idle.
Date Latitude Longitude Accuracy
07:45:35 51.362402459999996 - 6.2174867399999995 (75.0)
07:46:35 51.362402459999996 - 6.2174867399999995 (75.0)
07:47:35 51.362402459999996 - 6.2174867399999995 (75.0)
...
07:50:35 51.362402459999996 - 6.2174867399999995 (75.0) # I'm further than 75m away from my home at that time
I've indeed a new location every minute but it is exactly the same. Just the time is updated. The position is not updated (maybe normal when I don't move enough) but I find it strange that the coordinates are exactly the same. Also as I don't have wireless activated, it should locate me accordingly to the CellId (with an accuracy of ~1000m), here I still have an accuracy of 75.0. It seems, it is the last location recorded using wireless networks.
Any idea how can I record the real last location (even with low accuracy) ?
public void onLocationChanged(Location location) {
System.out.println(new Date(location.getTime())+" "
+location.getLatitude()+" - "+location.getLongitude()+" ("+location.getAccuracy()+")");
callback.addEntry(location);
}
Hey use GPS_PROVIDER instead of NETWORK_PROVIDER as per below code
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000, 0, pll);
NETWORK_PROVIDER will give you only fix tower location and gps will give you exact changing location
You have got nearest GSM tower coordinates.
It seems there is no way to force to compute the position using the LocationManager with the celltower only when there is no wireless connexion.
Maybe another way would be to use the method mentioned in the thread Poor Man GPS : manually compute the position knowing the CellId.
Pseudo code :
onLocationChanged(new_location)
if isConnected()
record(new_location)
else
cell_location = poorManGPS()
record(cell_location)
end
The problem is of course that I cannot query the cell-id database with no internet connection. I see several solutions :
Store the current (and previous) cell id when I have internet and hope I'll stay in this one when I won't have internet
Use the cell id database on the phone (but I think root privileges needed)
Store the cell id for further localization

Categories

Resources