I\'ve got a location tracking service that I\'m trying to unit test. I\'m trying to use the locationManager.addTestProvider and setTestProviderLocation methods to achieve this.
I was able to get @ingsaurabh's example working in a unit test after making the following modifications:
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.Socket;
import java.net.UnknownHostException;
public final class TestLocationProvider {
static void sendLocation(double latitude, double longitude) {
try {
Socket socket = new Socket("10.0.2.2", 5554); // usually 5554
socket.setKeepAlive(true);
String str = "geo fix " + longitude + " " + latitude ;
Writer w = new OutputStreamWriter(socket.getOutputStream());
w.write(str + "\r\n");
w.flush();
}
catch (UnknownHostException e) {
throw new RuntimeException(e);
}
catch (IOException e) {
throw new RuntimeException(e);
}
}
}
The important detail is the IP address. The emulator does not recognize 'localhost' as a valid hostname.