I'm trying to fix GPS programmatically when the GPS signal is not available. I want to do this in order to provide mock coordinates to other apps like google maps and others. I tried to use GPS_PROVIDER and NETWORK_PROVIDER but only with the latter I can get a new location valid for google maps. But this works only the first tima and if I want to re-fix more mock locations google maps doesn't see any change... why?? I have to do a service for what I want to do?
public void setLocation(double latitude, double longitude) {
//mocLocationProvider = LocationManager.GPS_PROVIDER;
mocLocationProvider = LocationManager.NETWORK_PROVIDER;
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.clearTestProviderEnabled(mocLocationProvider);
lm.requestLocationUpdates(mocLocationProvider,0,0,new llistener());
lm.addTestProvider(mocLocationProvider, false, false, false, false, false, false, false, 0, 10);
lm.setTestProviderEnabled(mocLocationProvider, true);
Location loc = new Location(mocLocationProvider);
loc.setTime(System.currentTimeMillis());
loc.setLatitude(latitude);
loc.setLongitude(longitude);
loc.setAccuracy(10);
lm.setTestProviderStatus(mocLocationProvider, LocationProvider.AVAILABLE,null, System.currentTimeMillis());
lm.setTestProviderLocation(mocLocationProvider, loc);
}
and location listener
public class llistener implements LocationListener {
@Override
public void onLocationChanged(Location loc) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
}
}
thanks
What mock locations need :
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION">
Also in the android phone settings make sure you have the "Allow mock locations" checkbox ticked
locationManager.addTestProvider(mocLocationProvider, false, false,
false, false, true, true, true, 0, 5);
locationManager.setTestProviderEnabled(mocLocationProvider, true);
. Now whenever you want to send a location, call this code:
Location mockLocation = new Location(mocLocationProvider); // a string
mockLocation.setLatitude(location.getLatitude()); // double
mockLocation.setLongitude(location.getLongitude());
mockLocation.setAltitude(location.getAltitude());
mockLocation.setTime(System.currentTimeMillis());
locationManager.setTestProviderLocation( mocLocationProvider, mockLocation);
.
But this works only the first time
Because you called it one time.
I have to do a service for what I want to do
You can do this, even an AsyncTask will do
do this or not: lm.requestLocationUpdates()
Yes you have to with the mocLocationProvider
I already call more times the method setLocation(latitude,longitude) but it works(location updateding) only first time for google maps
There is no method called setLocation()
, use setTestLocation()
What actually I have to do in asynckTask
You can use a Thread, a TimerTask or anything you like. The idea is to inject location every second into the Framework.