First, the forward. This is the first time that I've ever tried to create anything for android. While I do have some experience in Go, C++ and Python on x86, I have never written a line of Java or in my life and have never targeted mobile. I've apparently still got a lot to learn about this side of things.
My intent was to use an infinite loop that started a thread that grabbed the location of the device via GPS and sent it to a process on my home server, then slept for one second. I've got it (almost) working, but the location never updates. Instead, I keep getting the same longitude and latitude sent to my home server no matter where I am, inside or outside of my house (I have wifi access across the entire apartment complex, so it isn't an issue of the GPS not being accurate enough). After reading for quite awhile, I've come to the conclusion that I need to signal the location update to happen before sending again. How would I accomplish this?
Below is the relevant part of the source:
PS: Before anyone calls me out for it; yes, I am aware that sleeping outside of the thread is pretty stupid. This is just a little project to learn Android development and not a legitimate product.
My intent was to use an infinite loop that started a thread that grabbed the location of the device via GPS and sent it to a process on my home server, then slept for one second. I've got it (almost) working, but the location never updates. Instead, I keep getting the same longitude and latitude sent to my home server no matter where I am, inside or outside of my house (I have wifi access across the entire apartment complex, so it isn't an issue of the GPS not being accurate enough). After reading for quite awhile, I've come to the conclusion that I need to signal the location update to happen before sending again. How would I accomplish this?
Below is the relevant part of the source:
Code:
Runnable r = new Runnable() {
public void run() {
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(lm.GPS_PROVIDER);
try {
URL url = new URL("http://192.168.43.252:8081/tracker");
location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double longitude = location.getLongitude();
double latitude = location.getLatitude();
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
httpConn.setRequestMethod("POST");
httpConn.setDoOutput(true);
httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
httpConn.setRequestProperty("User-Agent", "El Diablo");
OutputStreamWriter request = new OutputStreamWriter(httpConn.getOutputStream());
request.write("comment=AndroidApp&long=" + Double.toString(longitude) + "&lat=" + Double.toString(latitude) + "&dateTime=0");
request.flush();
request.close();
}
reader.close();
httpConn.disconnect();
} catch (Exception e) {
Log.i(TAG, "Failed to send. ");
}
}
};
for(;;) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Thread runit = new Thread(r);
runit.start();
}
PS: Before anyone calls me out for it; yes, I am aware that sleeping outside of the thread is pretty stupid. This is just a little project to learn Android development and not a legitimate product.