requestLocationUpdates vs Location Listener - android

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() :)

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.

Better way to query GPS regularly

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.

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());

Location coordinates does not change when using LocationListener

I am using LocationListener on my application. However, whenever my location changes or onLocationChanged(Location location) method is executed, the coordinates of my location does not change at all. I am riding inside a car and had traveled several meters to kilometers already without my location changing. What could be the problem?
My code:
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
String bestProvider = locationManager.getBestProvider(new Criteria(), true);
{LocationListener locationListener = new LocationListener()
{
public void onLocationChanged(Location location)
{
//location.getLatitude(), location.getLongtitude()
//location coordinates does not change at all everytime onLocationChanged is called even after I already move several meters or kilometers.
}
//...
}
locationManager.requestLocationUpdates(bestProvider, 0, 0, locationListener);
Try this
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
locationListener);
You might be using the network provider. The listener is normally removed in the activty onPause. And you can set the listener in the onResume event of the activity.
Im not sure what is the logic behind finding the best provider by android. But GPS is always considered to be the most accurate compared to the network provider. But there are conditions when getting a location lock by the GPS is not possible. Thats when the network provider comes into use.
I normally create a simple class that calls a function to get location coords. Inside the function, I first request the location using GPS. I wait for 30 seconds for the location, if it is not received, then I request it using the network provider. This way I can always ensure that I am getting the GPS value as my first preference.
My guess is that your location is changing but you are overlooking the decimal part of latitude and longitude. Note the last digits of the decimal part before and after location change.
Try this way in your code
if(isInternetEnabled==true)//check for internet connection here
{
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
locationListener);
}
else
{
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
locationListener);
}
Because it happens with me that GPS_PROVIDER doesn't give quick response to location update then NETWORK_PROVIDER.

What does the following code do if there is no GPS fix..?

If there is no GPS FIX (because the person is in a metal building or something)....does it just stay in the Looper..?? OR does it keep trying for a fix via requestLocationUpdates..??
If I do have a good GPS FIX....my code works fine...and in onLocationChanged()...I update the current location to the database.
Also...when is onLocationChanged() called..?? Is it only called when there is a GPS FIX..??
Just wondering..
public void run()
{
Looper.prepare();
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener ll = new mylocationlistener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
Looper.loop();
}
Umm, is this in production code? Or are you just curious. This is absolutely not acceptable android code. Take a look at this for an idea on how to correctly implement a locationlistener
http://hejp.co.uk/android/android-gps-example/
when is onLocationChanged() called
Onlocationchanged is called when the parameters that you pass into the requestLocationUpdates(String provider, long minTime, float minDistance, PendingIntent intent) method are met. That is, whenever minTime passes or the device moves minDistance. (Note those are just hints, not exact values)
I found out (after alot of testing) that of the GPS fails....it will through an error and crash my service. Instead of killing the app...I just restart the service and try it again.

Categories

Resources