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