开发者

LocationManager requestLocationUpdates and timertask in 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 does开发者_JAVA百科n'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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜