So, yeah, I resumed playing around with android, this time version 2.0.
I’m really tempted to buy the new Motorola Milestone that should come out in Europe sometime between… now… and early next year, so I wanna be ready to create all the crazy stuff I have in mind for it
One of the things I noticed was that it’s not that straightforward to provide sample GPS data. Somehow the emulator’s GPX and KMZ functionality is not working fine for me and, even if it was, I don’t want to load the file everytime (i.e. I’m lazy).
So, after some googling around, here’s my solution to read points from a file and feed them 1 per second to the location manager, so I can finally work with them in my app:
My main activity implements LocationListener, so it can be passed to the LocationManager to receive GPS events. Here’s what I do when creating my activity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
((TextView) this.findViewById(R.id.textView)).setText("Something else");
// LocationManager locationManager = (LocationManager)
// getSystemService(Context.LOCATION_SERVICE);
// locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
// 0, 0, this);
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
String mocLocationProvider = LocationManager.GPS_PROVIDER;
locationManager.addTestProvider(mocLocationProvider, false, false,
false, false, true, true, true, 0, 5);
locationManager.setTestProviderEnabled(mocLocationProvider, true);
locationManager.requestLocationUpdates(mocLocationProvider, 0, 0, this);
try {
List data = new ArrayList();
InputStream is = getAssets().open("data.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line = null;
while ((line = reader.readLine()) != null) {
data.add(line);
}
Log.e(LOG_TAG, data.size() + " lines");
new MockLocationProvider(locationManager, mocLocationProvider, data).start();
} catch (IOException e) {
e.printStackTrace();
}
}
This will basically setup the test location provider and read the points into a list. Then I feed that stuff to my mock location provider (just a normal thread) that will read them 1 per second and trigger the new location back to this activity. Here’s the code for MockLocationProvider:
public class MockLocationProvider extends Thread {
private List data;
private LocationManager locationManager;
private String mocLocationProvider;
private String LOG_TAG = "faren";
public MockLocationProvider(LocationManager locationManager,
String mocLocationProvider, List data) throws IOException {
this.locationManager = locationManager;
this.mocLocationProvider = mocLocationProvider;
this.data = data;
}
@Override
public void run() {
for (String str : data) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Set one position
String[] parts = str.split(",");
Double latitude = Double.valueOf(parts[0]);
Double longitude = Double.valueOf(parts[1]);
Double altitude = Double.valueOf(parts[2]);
Location location = new Location(mocLocationProvider);
location.setLatitude(latitude);
location.setLongitude(longitude);
location.setAltitude(altitude);
Log.e(LOG_TAG, location.toString());
// set the time in the location. If the time on this location
// matches the time on the one in the previous set call, it will be
// ignored
location.setTime(System.currentTimeMillis());
locationManager.setTestProviderLocation(mocLocationProvider,
location);
}
}
}
Notice the location.setTime() call. Read the comment why it is necessary. Took me forever to find this one in google
Peace and great Androiding
Related:
- Double click/tap detection on android’s MapView If there is a cleaner way to do it, please...
- SSH login without password (authorized_keys) I know that a lot of people covered this in...







I used this tutorial and the only thing i changed was the name of the locationprovider. I changed it to test. And now some other applications of mine that use GPS use the test location provider as well, how can i properly delete the locatoinprovider in order to not use the test provider in other applications?
Thanks so much for your reply, Pedro. I implemented the tutorial but LocMgr.addTestLocProvider may have changed from what I was able to gather on the forum. It seems you just need to provide route file pushed onto data/misc/location and it will get pick up by a default mock loc provider. This seems to be only on the emulator. The Adroid Applicatios Settings allow for Mock Loc Provider. So, I am not how to simulate a travel via LocMgr and mock loc provider. I ended up using Handler.postDelay to update loc overlay
Maybe you could write an article about it?
I am not able to get location updates on my listener. My thread runs forever with locMgr.setTestLoc to no avail. I provided perm to MOCK_LOC_UPDDATE. I am using sdk 7. Am I missing a configuration req?
Hi Natasha, i haven’t looked into this in a while now. Maybe the latest updates changed something that makes it not work anymore. I would suggest looking at the release change logs for the SDK since version 2.0 and see if something changed that could affect this
Sorry for not being able to help, but i’m full of work right now
Nice code! Worked great for me,
thanks a lot for sharing it here!
Dave
No problem