Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LocationManager requestLocationUpdates and timertask in android

Tags:

android

I have the following code:

if (gps_enabled) {
        Log.e("$$$$$$$$$$$$$$",
                "GPS is enabled requestion location updates... interval value is: "
                        + interval);
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
                locationListenerGps);
    }
    else{
        if (network_enabled) {
            Log.e("$$$$$$$$$$$$$$",
                    "Network is enabled requestion location updates... interval value is: "
                            + interval);
            lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0,
                    0, locationListenerNetwork);
        }
    }

with that code I can get the location(at least using the network provider! (another issue on another post)) I would like to get notifications in a regular interval say every one hour, but passing the parameter to requestLocationUpdates doesn't guarantee that the interval will be maintained (at least that my tests showned,since I expected update every minute but got a lot of updates instead of one!) so i thought of using a timerTask and schedule it, I now have

timer1.scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
            getLocation();
        }

    }, 0, 180000);// 3 minutesr...

where getLocation is the method that I called previously called, but when the timer calls that method then nothing happens, the logs stop at this point

Log.e("$$$$$$$$$$$$$$",
                    "Network is enabled requestion location updates... interval value is: "
                            + interval);

and I never get notified about my location. any Ideas?

like image 377
maxsap Avatar asked Mar 16 '26 14:03

maxsap


1 Answers

Ok took me a while but I have found the solution to this,as the documentation says you can only request location updates from the location manager from a looper thread, that means that when the timer task is called you have to obtain a message and send the message to a handler and the handler would be responsible for requesting location updates.

like image 104
maxsap Avatar answered Mar 18 '26 03:03

maxsap



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!