Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 823
adalpari Avatar asked Dec 01 '25 12:12

adalpari


1 Answers

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());
like image 136
hiruss Avatar answered Dec 06 '25 19:12

hiruss



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!