GPS Mock - I only can mock one time the location - android

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

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.

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

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

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