• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Attempt at location update fails miserably. (Or, I suck at this...)

afdozerman

Lurker
Oct 23, 2018
2
0
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:

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.
 
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.

Yeah but still, do things right ;) I think you probably realise that the sleep statement should be inside the thread. And the above code is really inefficient, because it opens and closes a network connection every second.

But you don't have to create your own thread, because the SDK provides a mechanism to request location updates, using a callback pattern. You can request periodic location updates, which will notify you via method onLocationResult()

https://developer.android.com/training/location/receive-location-updates
 
Last edited by a moderator:
Upvote 0
Yeah but still, do things right ;) I think you probably realise that the sleep statement should be inside the thread. And the above code is really inefficient, because it opens and closes a network connection every second.

But you don't have to create your own thread, because the SDK provides a mechanism to request location updates, using a callback pattern. You can request periodic location updates, which will notify you via method onLocationResult()

https://developer.android.com/training/location/receive-location-updates

Alright. I'll just "go back to formula". I think I just got a bit exited.
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones