Search code examples
javaandroidlocationandroid-gpsuser-location

Get current location every n minutes in android


I want to get the latitude and longitude every n minutes and then use them in my app for setting the user in an area. The only problem is that I don't know how to get the location every -let's say- 2 minutes.

PS: I'm using both GPS and WiFi


Solution

  • just adjust it according to your need.

        Timer timer = new Timer ();
        TimerTask hourlyTask = new TimerTask () {
        @Override
        public void run () {
            // check if GPS enabled
            GPSTracker gpsTracker = new GPSTracker(this);
            if (gpsTracker.getIsGPSTrackingEnabled()){
                String stringLatitude = String.valueOf(gpsTracker.latitude);
                String stringLongitude = String.valueOf(gpsTracker.longitude);
                }
            }
        };
    
    // schedule the task to RUN every hour
    timer.schedule (hourlyTask, 0l, 1000*60*60);   // 1000*10*60 every 10 minut