1

I can get location using LocationManager taking minDistance and minTime.

But how can I get location every 15 secs, for example? (even if an user didn't move)

        if (Utils.hasProvider(LocationManager.GPS_PROVIDER)) {
            mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 15000, 100f, this);
        }

        if (Utils.hasProvider(LocationManager.NETWORK_PROVIDER)) {
            mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 15000, 100f, this);
        }
djtoro
  • 15
  • 2

3 Answers3

3

Google docs say that

The minDistance parameter can also be used to control the frequency of location updates. If it is greater than 0 then the location provider will only send your application an update when the location has changed by at least minDistance meters, AND at least minTime milliseconds have passed. However it is more difficult for location providers to save power using the minDistance parameter, so minTime should be the primary tool to conserving battery life.

Thus there is an AND condition involved. So you should set your distance parameter to 0. In that case you will get location update every 15 seconds.

varunkr
  • 5,364
  • 11
  • 50
  • 99
0

Use AlarmManager to do this.See this answer.Replace the time to 5 minutes.

Or else,you can use

         Runnable runnable = new Runnable() {

         public void run() 
                {
                    //Your code to fetch location
                }
            };

            Handler handler1 = new Handler();
            handler1.postDelayed(runnable, 15000);

Here 15000 is 15 seconds.

Community
  • 1
  • 1
Jas
  • 3,207
  • 2
  • 15
  • 45
0

You can use IntentService and on onstartcommand call runnable to post you location, like this

   Handler handler.postDelayed(postGpsRunnable, 15000);
Vitaliy Ptitsin
  • 329
  • 1
  • 5
  • 14